コード例 #1
0
        public void EventFired_SourceObjectAndSourceNameSet_ListensOnlyToSourceObject()
        {
            EventTrigger eventTrigger = CreateEventTrigger();

            Grid      grid             = CreateGrid();
            Rectangle host             = CreateNamedElement <Rectangle>("rect");
            Button    sourceButton     = CreateNamedElement <Button>("sourcebutton");
            Button    sourceNameButton = CreateNamedElement <Button>("sourceNameButton");

            AddChildElement(grid, host);
            AddChildElement(grid, sourceButton);
            AddChildElement(grid, sourceNameButton);

            using (StubWindow window = new StubWindow(grid))
            {
                StubAction action = AttachTriggerToObject(eventTrigger, host);
                eventTrigger.EventName    = "Click";
                eventTrigger.SourceName   = "sourceNameButton";
                eventTrigger.SourceObject = sourceButton;
                System.Windows.RoutedEventArgs args = CreateClickEvent();

                sourceNameButton.RaiseEvent(args);
                Assert.AreEqual(action.InvokeCount, 0, "Click on source name button when SourceObject is set to another object should not invoke the action.");
                sourceButton.RaiseEvent(args);
                Assert.AreEqual(action.InvokeCount, 1, "Click on source object button should invoke the action.");
            }
        }
コード例 #2
0
        public void TestLoadedEvent()
        {
            Rectangle    rectangle       = new Rectangle();
            EventTrigger eventTrigger1   = new EventTrigger("Loaded");
            EventTrigger eventTrigger2   = new EventTrigger("MouseLeftButtonDown");
            StubAction   loadedAction    = new StubAction();
            StubAction   mouseDownAction = new StubAction();

            eventTrigger1.Actions.Add(loadedAction);
            eventTrigger1.Attach(rectangle);

            using (StubWindow window = new StubWindow(rectangle))
            {
                Assert.AreEqual(loadedAction.InvokeCount, 1, "Loaded action was invoked");
                eventTrigger1.EventName = "GotMouseCapture";
                rectangle.CaptureMouse();
                Assert.AreEqual(loadedAction.InvokeCount, 2, "GotMouseCapture action was invoked");
                rectangle.ReleaseMouseCapture();
                eventTrigger1.EventName = "Loaded";
                rectangle.CaptureMouse();
                Assert.AreEqual(loadedAction.InvokeCount, 2, "GotMouseCapture action was not invoked");
                rectangle.ReleaseMouseCapture();
            }
            eventTrigger1.Detach();
            Assert.IsNull(((IAttachedObject)eventTrigger1).AssociatedObject, "Trigger was detached");
        }
コード例 #3
0
        public void EventFired_SourceObjectChangesToNull_ListensToSourceName()
        {
            EventTrigger eventTrigger = CreateEventTrigger();

            Grid      grid             = CreateGrid();
            Rectangle host             = CreateNamedElement <Rectangle>("rect");
            Button    sourceButton     = CreateNamedElement <Button>("sourcebutton");
            Button    sourceNameButton = CreateNamedElement <Button>("sourceNameButton");

            AddChildElement(grid, host);
            AddChildElement(grid, sourceButton);
            AddChildElement(grid, sourceNameButton);

            using (StubWindow window = new StubWindow(grid))
            {
                StubAction action = AttachTriggerToObject(eventTrigger, host);
                eventTrigger.SourceObject = sourceButton;
                eventTrigger.SourceName   = "sourceNameButton";
                eventTrigger.EventName    = "Click";
                eventTrigger.ClearValue(EventTriggerBase.SourceObjectProperty);
                System.Windows.RoutedEventArgs args = CreateClickEvent();

                sourceButton.RaiseEvent(args);
                Assert.AreEqual(action.InvokeCount, 0, "Click on source object button should not invoke the action, as the SourceObject has been cleared.");
                sourceNameButton.RaiseEvent(args);
                Assert.AreEqual(action.InvokeCount, 1, "Click on source name button when SourceObject has been cleared should invoke the action.");
            }
        }
コード例 #4
0
ファイル: ActionTest.cs プロジェクト: zys529/XamlBehaviorsWpf
        public void ReparentActionTest()
        {
            SysWindows.Shapes.Rectangle hostRectangle = new SysWindows.Shapes.Rectangle();
            // try parenting an action more than once; should throw
            StubAction  action   = new StubAction();
            StubTrigger trigger1 = new StubTrigger();
            StubTrigger trigger2 = new StubTrigger();

            trigger1.Attach(hostRectangle);
            trigger2.Attach(hostRectangle);
            trigger1.Actions.Add(action);
            try
            {
                trigger2.Actions.Add(action);
                Debug.Fail("Expected InvalidOperationException to be thrown after adding an action to a second trigger.");
            }
            catch (InvalidOperationException)
            {
            }

            // now try the same, properly unhooking before reparenting
            action   = new StubAction();
            trigger1 = new StubTrigger();
            trigger2 = new StubTrigger();
            trigger1.Actions.Add(action);
            trigger1.Actions.Remove(action);
            trigger2.Actions.Add(action);
            Assert.AreEqual(((IAttachedObject)action).AssociatedObject, trigger2.HostObject, "action.AssociatedObject == trigger2.Host");
            Assert.AreEqual(trigger2.Actions.Count, 1, "trigger2.Actions.Count == 1");
        }
コード例 #5
0
        public void InvokeTriggerWithConditionalBehavior_TwoConditionsBothNotMetForwardChainingOr()
        {
            ConditionalExpression conditionalExpression = null;
            StubTrigger           trigger = null;
            StubAction            action  = null;

            SetupTriggerActionConditionBehavior(out conditionalExpression, out trigger, out action);

            // Resetting an non met condition
            conditionalExpression.Conditions.Add(new ComparisonCondition());
            conditionalExpression.Conditions[0].LeftOperand  = BehaviorTestUtilities.IntegerOperand4;
            conditionalExpression.Conditions[0].RightOperand = BehaviorTestUtilities.IntegerOperand5;
            conditionalExpression.Conditions[0].Operator     = ComparisonConditionType.GreaterThan;

            // Add the second condition
            conditionalExpression.Conditions.Add(new ComparisonCondition());
            Assert.IsTrue(conditionalExpression.Conditions.Count == 2, "We should have 2 conditions");

            // Creating a new condition
            conditionalExpression.Conditions[1].LeftOperand  = BehaviorTestUtilities.IntegerOperand5;
            conditionalExpression.Conditions[1].RightOperand = BehaviorTestUtilities.IntegerOperand6;
            conditionalExpression.Conditions[1].Operator     = ComparisonConditionType.GreaterThan;

            // Firing the trigger, forward chaining changed to OR.
            conditionalExpression.ForwardChaining = ForwardChaining.Or;
            trigger.FireStubTrigger();
            Assert.AreEqual(action.InvokeCount, 0, "action.InvokeCount == 0, both conditons are not met, forward chaining was Or");
        }
コード例 #6
0
ファイル: ActionTest.cs プロジェクト: zys529/XamlBehaviorsWpf
        public void TestCreateInstanceCore()
        {
            StubAction action = new StubAction();

            SysWindows.Freezable freezable = action.GetCreateInstanceCore();
            Assert.AreEqual(freezable.GetType(), typeof(StubAction), "freezable.GetType() == typeof(StubAction)");
        }
コード例 #7
0
        public void InvokeTriggerWithConditionalBehavior_TwoConditionsOneNotMet()
        {
            ConditionalExpression conditionalExpression = null;
            StubTrigger           trigger = null;
            StubAction            action  = null;

            SetupTriggerActionConditionBehavior(out conditionalExpression, out trigger, out action);

            // Resetting an non met condition
            conditionalExpression.Conditions.Add(new ComparisonCondition());
            conditionalExpression.Conditions[0].LeftOperand  = BehaviorTestUtilities.IntegerOperand4;
            conditionalExpression.Conditions[0].RightOperand = BehaviorTestUtilities.IntegerOperand5;
            conditionalExpression.Conditions[0].Operator     = ComparisonConditionType.GreaterThan;

            // Add the second condition
            conditionalExpression.Conditions.Add(new ComparisonCondition());
            Assert.IsTrue(conditionalExpression.Conditions.Count == 2, "We should have 2 conditions");

            // Creating a new condition
            conditionalExpression.Conditions[1].LeftOperand  = BehaviorTestUtilities.IntegerOperand5;
            conditionalExpression.Conditions[1].RightOperand = BehaviorTestUtilities.IntegerOperand6;
            conditionalExpression.Conditions[1].Operator     = ComparisonConditionType.LessThan;


            // Firing the trigger, with one of the condition not met
            trigger.FireStubTrigger();
            Assert.AreEqual(action.InvokeCount, 0, "action.InvokeCount == 0, one conditon not met");
        }
コード例 #8
0
        private static StubAction AttachActionToDataTrigger(DataTrigger dataTrigger, SysWindows.DependencyObject hostObject)
        {
            StubAction stubAction = CreateStubAction();

            dataTrigger.Actions.Add(stubAction);
            Interaction.GetTriggers(hostObject).Add(dataTrigger);
            return(stubAction);
        }
コード例 #9
0
        private static StubAction AttachTriggerToObject(EventTrigger eventTrigger, SysWindows.DependencyObject host)
        {
            StubAction eventAction = new StubAction();

            eventTrigger.Actions.Add(eventAction);
            eventTrigger.Attach(host);
            return(eventAction);
        }
コード例 #10
0
        public void SetComparison_DoesNotSatisfyCondition_DoesNotCauseReevaluation()
        {
            DataTrigger dataTrigger   = CreateDataTrigger(0, ComparisonConditionType.NotEqual, 0);
            Rectangle   hostRectangle = CreateRectangle();
            StubAction  stubAction    = AttachActionToDataTrigger(dataTrigger, hostRectangle);

            dataTrigger.Comparison = ComparisonConditionType.LessThan;
            Assert.AreEqual(stubAction.InvokeCount, 0, "The trigger should not have been invoked.");
        }
コード例 #11
0
        public void SetComparison_ToSatisfyCondition_CausesReevaluation()
        {
            DataTrigger dataTrigger   = CreateDataTrigger("Foo", ComparisonConditionType.NotEqual, "Foo");
            Rectangle   hostRectangle = CreateRectangle();
            StubAction  stubAction    = AttachActionToDataTrigger(dataTrigger, hostRectangle);

            dataTrigger.Comparison = ComparisonConditionType.Equal;
            Assert.AreEqual(stubAction.InvokeCount, 1, "The trigger should have been invoked once.");
        }
コード例 #12
0
        public void EventFired_SimpleEvent_Fires()
        {
            EventTrigger    eventTrigger = CreateEventTrigger("StubEvent");
            EventObjectStub eventStub    = CreateEventObjectStub();
            StubAction      eventAction  = AttachTriggerToObject(eventTrigger, eventStub);

            eventStub.FireStubEvent();
            Assert.AreEqual(eventAction.InvokeCount, 1, "Action was invoked in response to event.");
        }
コード例 #13
0
ファイル: ActionTest.cs プロジェクト: zys529/XamlBehaviorsWpf
        public void AttachDetachTest()
        {
            StubAction stubAction = new StubAction();

            BehaviorTestUtilities.TestIAttachedObject <Button>((IAttachedObject)stubAction);

            StubTargetedTriggerAction stubTargetedTriggerAction = new StubTargetedTriggerAction();

            BehaviorTestUtilities.TestIAttachedObject <Button>((IAttachedObject)stubTargetedTriggerAction);
        }
コード例 #14
0
        public void DataStoreChangeTrigger_SetBindingValue_ValueChangedWithSameValue()
        {
            StubDataStore           dataStore = this.CreateDataStore();
            DataStoreChangedTrigger trigger   = CreateTrigger <DataStoreChangedTrigger>(dataStore, "Foo");
            StubAction stubAction             = CreateStubAction();

            trigger.Actions.Add(stubAction);

            this.Test_SetBindingValue_ValueChangedWithSameValue(trigger, stubAction, dataStore);
        }
コード例 #15
0
        public void PropertyChangedTrigger_SetBindingValue_ValueChangedTwice()
        {
            StubDataStore          dataStore  = this.CreateDataStore();
            PropertyChangedTrigger trigger    = CreateTrigger <PropertyChangedTrigger>(dataStore, "Foo");
            StubAction             stubAction = CreateStubAction();

            trigger.Actions.Add(stubAction);

            this.Test_SetBindingValue_ValueChangedTwice(trigger, stubAction, dataStore);
        }
コード例 #16
0
        public void SourceObjectEventFired_EventNameMatchesFiredEvent_Fires()
        {
            Button          host         = CreateButton();
            EventObjectStub eventSource  = CreateEventObjectStub();
            EventTrigger    eventTrigger = CreateEventTrigger("StubEvent");
            StubAction      action       = AttachTriggerToObject(eventTrigger, host);

            eventTrigger.SourceObject = eventSource;
            eventSource.FireStubEvent();

            Assert.AreEqual(action.InvokeCount, 1, "Trigger should be invoked when source object fires the event it is listening to.");
        }
コード例 #17
0
ファイル: ActionTest.cs プロジェクト: zys529/XamlBehaviorsWpf
        public void ParentTriggerTest()
        {
            StubTrigger trigger = new StubTrigger();
            StubAction  action  = new StubAction();

            trigger.Actions.Add(action);
            Assert.AreEqual(((IAttachedObject)action).AssociatedObject, trigger.HostObject, "After adding action to trigger, action.AssociatedObject should equal trigger.Host");
            Assert.AreEqual(trigger.Actions.Count, 1, "trigger.Actions.Count == 1");

            trigger.Actions.Remove(action);
            Assert.IsNull(((IAttachedObject)action).AssociatedObject, "After removing action from trigger, action.AssociatedObject should be null");
            Assert.AreEqual(trigger.Actions.Count, 0, "trigger.Actions.Count == 0");
        }
コード例 #18
0
        public void NonDependencyObjectEventSource_EventFired_InvokesActions()
        {
            EventTrigger      eventTrigger  = CreateEventTrigger();
            ClrEventClassStub clrEventClass = CreateClrEventClassStub();
            Button            host          = CreateButton();

            eventTrigger.SourceObject = clrEventClass;
            eventTrigger.EventName    = ClrEventClassStub.EventName;
            StubAction action = AttachTriggerToObject(eventTrigger, host);

            clrEventClass.Fire();
            Assert.AreEqual(action.InvokeCount, 1, "Event firing on a source CLR object should fire the EventTrigger.");
        }
コード例 #19
0
        public void EventFired_OldEventFiredAfterEventNameChanged_DoesNotFires()
        {
            Button          host         = CreateButton();
            EventObjectStub eventSource  = CreateEventObjectStub();
            EventTrigger    eventTrigger = CreateEventTrigger("StubEvent");
            StubAction      action       = AttachTriggerToObject(eventTrigger, host);

            eventTrigger.SourceObject = eventSource;
            eventTrigger.EventName    = "StubEvent2";
            eventSource.FireStubEvent();

            Assert.AreEqual(action.InvokeCount, 0, "Trigger should not be invoked when source object fires its event it is not listening to.");
        }
コード例 #20
0
        public void EventFired_OldSourceObjectFiresEvent_DoesNotFire()
        {
            Button          host           = CreateButton();
            EventObjectStub oldEventSource = CreateEventObjectStub();
            EventObjectStub newEventSource = CreateEventObjectStub();
            EventTrigger    eventTrigger   = CreateEventTrigger("StubEvent");
            StubAction      action         = AttachTriggerToObject(eventTrigger, host);

            eventTrigger.SourceObject = oldEventSource;
            eventTrigger.SourceObject = newEventSource;
            oldEventSource.FireStubEvent();

            Assert.AreEqual(action.InvokeCount, 0, "Trigger should not be invoked when an old source object fires the event it is listening to.");
        }
コード例 #21
0
ファイル: ActionTest.cs プロジェクト: zys529/XamlBehaviorsWpf
        public void InvokeTest()
        {
            StubAction  action  = new StubAction();
            StubTrigger trigger = new StubTrigger();

            Assert.AreEqual(action.InvokeCount, 0, "action.InvokeCount == 0");
            action.StubInvoke();
            Assert.AreEqual(action.InvokeCount, 1, "After invoking action, action.InvokeCount == 1");
            trigger.Actions.Add(action);
            action.IsEnabled = false;
            trigger.FireStubTrigger();
            Assert.AreEqual(action.InvokeCount, 1, "action.InvokeCount == 1");
            action.IsEnabled = true;
            trigger.FireStubTrigger();
            Assert.AreEqual(action.InvokeCount, 2, "action.InvokeCount == 2");
        }
コード例 #22
0
        public void InvokeTriggerWithConditionalBehavior_OneMetCondition()
        {
            ConditionalExpression conditionalExpression = null;
            StubTrigger           trigger = null;
            StubAction            action  = null;

            SetupTriggerActionConditionBehavior(out conditionalExpression, out trigger, out action);

            conditionalExpression.Conditions.Add(new ComparisonCondition());
            conditionalExpression.Conditions[0].LeftOperand  = BehaviorTestUtilities.IntegerOperand4;
            conditionalExpression.Conditions[0].RightOperand = BehaviorTestUtilities.IntegerOperand5;
            conditionalExpression.Conditions[0].Operator     = ComparisonConditionType.LessThan;

            // Firing trigger with condition behavior met
            trigger.FireStubTrigger();
            Assert.AreEqual(action.InvokeCount, 1, "action.InvokeCount == 1, conditon met");
        }
コード例 #23
0
        private static void SetupTriggerActionConditionBehavior(out ConditionalExpression conditionalExpression, out StubTrigger trigger, out StubAction action)
        {
            ConditionBehavior conditionBehavior = new ConditionBehavior();

            conditionalExpression = new ConditionalExpression();
            // Hook the conditional expresison to the condition behavior
            conditionBehavior.Condition = conditionalExpression;

            trigger = new StubTrigger();
            BehaviorCollection behaviorCollection = Interaction.GetBehaviors(trigger);

            // Add the condition behavior to the trigger
            behaviorCollection.Add(conditionBehavior);

            // Addting an action to the trigger
            action = new StubAction();
            trigger.Actions.Add(action);
        }
コード例 #24
0
        public void InvokeTriggerWithConditionalBehavior_TwoConditionsMet()
        {
            ConditionalExpression conditionalExpression = null;
            StubTrigger           trigger = null;
            StubAction            action  = null;

            SetupTriggerActionConditionBehavior(out conditionalExpression, out trigger, out action);

            conditionalExpression.Conditions.Add(new ComparisonCondition());
            conditionalExpression.Conditions[0].LeftOperand  = BehaviorTestUtilities.IntegerOperand5;
            conditionalExpression.Conditions[0].RightOperand = BehaviorTestUtilities.IntegerOperand5;
            conditionalExpression.Conditions[0].Operator     = ComparisonConditionType.Equal;
            conditionalExpression.Conditions.Add(new ComparisonCondition());
            conditionalExpression.Conditions[1].LeftOperand  = BehaviorTestUtilities.StringOperandLoremIpsum;
            conditionalExpression.Conditions[1].RightOperand = BehaviorTestUtilities.StringOperandNuncViverra;
            conditionalExpression.Conditions[1].Operator     = ComparisonConditionType.NotEqual;

            // Firing trigger with 2 conditions, two conditions are met
            trigger.FireStubTrigger();
            Assert.AreEqual(action.InvokeCount, 1, "action.InvokeCount == 1, both conditons met");
        }
コード例 #25
0
        public void EventFired_EventFiredOnAssociatedObjectWhenSourceNameObjectIsDifferent_DoesNotFire()
        {
            EventTrigger eventTrigger = CreateEventTrigger();

            Grid      grid   = CreateGrid();
            Rectangle rect   = CreateNamedElement <Rectangle>("rect");
            Button    button = CreateNamedElement <Button>("button");

            AddChildElement(grid, rect);
            AddChildElement(grid, button);

            using (StubWindow window = new StubWindow(grid))
            {
                StubAction action = AttachTriggerToObject(eventTrigger, rect);
                eventTrigger.SourceName = "button";
                eventTrigger.EventName  = "Click";
                System.Windows.RoutedEventArgs args = CreateClickEvent();

                rect.RaiseEvent(args);
                Assert.AreEqual(action.InvokeCount, 0, "Click on rect while Source=button should not invoke the action.");
            }
        }
コード例 #26
0
        public void InvokeTriggerTest()
        {
            Button                 parameter       = new Button();
            Rectangle              parameter2      = new Rectangle();
            StubAction             action1         = new StubAction();
            StubAction             action2         = new StubAction();
            ParameterTriggerAction parameterAction = new ParameterTriggerAction();
            StubTrigger            trigger         = new StubTrigger();

            trigger.Actions.Add(action1);
            trigger.FireStubTrigger();
            Assert.AreEqual(action1.InvokeCount, 1, "action1.InvokeCount == 1");
            Assert.AreEqual(action2.InvokeCount, 0, "action2.InvokeCount == 0");
            Assert.AreEqual(parameterAction.InvokeCount, 0, "action3.InvokeCount == 0");

            // Adding action 2
            trigger.Actions.Add(action2);
            trigger.FireStubTrigger();
            Assert.AreEqual(action1.InvokeCount, 2, "action1.InvokeCount == 2");
            Assert.AreEqual(action2.InvokeCount, 1, "action2.InvokeCount == 1");
            Assert.AreEqual(parameterAction.InvokeCount, 0, "action3.InvokeCount == 0");

            // Adding action 3
            trigger.Actions.Add(parameterAction);
            trigger.FireStubTrigger(parameter);
            Assert.AreEqual(action1.InvokeCount, 3, "action1.InvokeCount == 3");
            Assert.AreEqual(action2.InvokeCount, 2, "action2.InvokeCount == 2");
            Assert.AreEqual(parameterAction.InvokeCount, 1, "parameterAction.InvokeCount == 1");
            Assert.AreEqual(parameterAction.Parameter, parameter, "parameterAction.Parameter == parameter");

            // Firing with parameter 2
            trigger.FireStubTrigger(parameter2);
            Assert.AreEqual(action1.InvokeCount, 4, "action1.InvokeCount == 4");
            Assert.AreEqual(action2.InvokeCount, 3, "action2.InvokeCount == 3");
            Assert.AreEqual(parameterAction.InvokeCount, 2, "parameterAction.InvokeCount == 2");
            Assert.AreEqual(parameterAction.Parameter, parameter2, "parameterAction.Parameter == parameter2");
        }
コード例 #27
0
 private void Test_SetBindingValue_ValueChanged(TriggerBase <DependencyObject> trigger, StubAction stubAction, StubDataStore dataStore)
 {
     using (StubWindow window = new StubWindow(null))
     {
         // Change the value on the data store.
         dataStore.Foo = "foo";
         // Force the Data binding phase
         DispatcherHelper.ForceDataBinding();
         Assert.AreEqual(stubAction.InvokeCount, 1, "The trigger should have been invoked once.");
     }
 }