public void Test_Invoke_NoCommand() { // 呼び出しコマンドが設定されていないとき // コマンドのモック var cmdMock = new TestCommand(); cmdMock.Setup_CanExecute(true); // パラメータ用データ var argParam = new object(); var propParam = new object(); // テスト対象の準備 (Command未設定) var target = new InvokeCommandAction(); target.CommandParameterMode = InvokeCommandAction.CommandParameterModeKind.Auto; target.CommandParameter = propParam; // テスト対象のアクションを呼び出すためのトリガ作成 var element = new DependencyObject(); var trigger = new TestTrigger(); trigger.Attach(element); trigger.Actions.Add(target); // アクションを呼び出すためにトリガ実行 new Action(() => trigger.Invoke(argParam)).Should().NotThrow(); }
private static InvokeCommandAction CreateInvokeCommandActionWithCommandName(string commandName) { InvokeCommandAction invokeCommandAction1 = CreateInvokeCommandAction(); invokeCommandAction1.CommandName = commandName; return(invokeCommandAction1); }
private InvokeCommandAction CreateInvokeCommandActionWithCommand(ICommand command) { InvokeCommandAction action = CreateInvokeCommandAction(); action.Command = command; return(action); }
public void InvokeCommandActionTest() { var textBox = new TextBox(); var eventTrigger = new EventTrigger(nameof(textBox.TextChanged)); var action = new InvokeCommandAction(); bool invoked = false; action.Command = new RelayCommand <bool>(b => invoked = b); action.CommandParameter = true; eventTrigger.Actions.Add(action); eventTrigger.Attach(textBox); textBox.RaiseEvent(new TextChangedEventArgs(TextBoxBase.TextChangedEvent, UndoAction.None)); Assert.IsTrue(invoked); // for code coverage action = new InvokeCommandAction("test1") { Command = null, CommandName = "test" }; action.CommandName = "test"; eventTrigger.Actions.Add(action); Assert.AreEqual(action.CommandName, "test"); textBox.RaiseEvent(new TextChangedEventArgs(TextBoxBase.TextChangedEvent, UndoAction.None)); action.IsEnabled = false; textBox.RaiseEvent(new TextChangedEventArgs(TextBoxBase.TextChangedEvent, UndoAction.None)); action.IsEnabled = true; action.Detach(); textBox.RaiseEvent(new TextChangedEventArgs(TextBoxBase.TextChangedEvent, UndoAction.None)); }
private void CreateMap(int width, int height, int cellSize) { var beginState = Game.CreateMap(width, height); appViewModel.Game.SetMap(beginState); for (int j = 0; j < height; j++) { Map.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(cellSize) }); } for (int i = 0; i < width; i++) { Map.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(cellSize) }); } Map.Width = width * cellSize; Map.Height = height * cellSize; Height = Map.Height + 40 + ToolBar.Height; Width = Map.Width + 15; CenterWindow(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int x = i; int y = j; var shape = new Ellipse() { Width = cellSize, Height = cellSize, }; Map.Children.Add(shape); Grid.SetRow(shape, x); Grid.SetColumn(shape, y); var trigger = new Microsoft.Xaml.Behaviors.EventTrigger("MouseDown"); var commandAction = new InvokeCommandAction() { Command = appViewModel.ToggleCommand, CommandParameter = beginState[x, y], }; trigger.Actions.Add(commandAction); Interaction.GetTriggers(shape).Add(trigger); var binding = new Binding { Source = beginState[x, y], Path = new PropertyPath("State") }; shape.SetBinding(Shape.FillProperty, binding); } } }
public void Test_Invoke_ParamModeAuto2() { // Mode=Auto, プロパティ設定なし時 // コマンドのモック var cmdMock = new TestCommand(); cmdMock.Setup_CanExecute(true); // パラメータ用データ var argParam = new object(); var propParam = new object(); // テスト対象の準備 var target = new InvokeCommandAction(); target.CommandParameterMode = InvokeCommandAction.CommandParameterModeKind.Auto; target.Command = cmdMock.Object; //target.CommandParameter = propParam; // プロパティ設定なし // テスト対象のアクションを呼び出すためのトリガ作成 var element = new DependencyObject(); var trigger = new TestTrigger(); trigger.Attach(element); trigger.Actions.Add(target); // アクションを呼び出すためにトリガ実行 trigger.Invoke(argParam); // 呼び出し結果の検証 cmdMock.Verify(c => c.Execute(argParam), Times.Once()); cmdMock.Verify(c => c.Execute(propParam), Times.Never()); }
private InvokeCommandAction CreateInvokeCommandActionWithCommandNameAndParameter(string p, object parameter) { InvokeCommandAction action = CreateInvokeCommandActionWithCommandName(p); action.CommandParameter = parameter; return(action); }
public void Test_AutoEnable_Restore_by_ClearCommand() { // コマンドのモック var cmdMock = new TestCommand(); cmdMock.Setup_CanExecute(false); // リンク先要素 var element = new UIElement(); element.IsEnabled.Should().Be(true); // 初期状態を一応確認 // テスト対象の準備 var target = new InvokeCommandAction(); target.AutoEnable = true; // テスト対象のアクションを呼び出すためのトリガ作成 var trigger = new TestTrigger(); trigger.Attach(element); trigger.Actions.Add(target); // コマンド設定 target.Command = cmdMock.Object; // コマンド実行可能状態変更が反映されて状態適用されるはず element.IsEnabled.Should().Be(false); // コマンドクリア target.Command = null; // 元の有効状態に戻ること element.IsEnabled.Should().Be(true); }
public void Invoke_LegalCommandName_InvokesCommand() { InvokeCommandAction invokeCommandAction = CreateInvokeCommandActionWithCommandName("StubCommand"); StubBehavior stubBehavior = CreateStubBehavior(); StubTrigger trigger = AttachActionToObject(invokeCommandAction, stubBehavior); trigger.FireStubTrigger(); Assert.AreEqual(stubBehavior.ExecutionCount, 1, "stubBehavior.ExecutionCount == 1"); }
private static StubTrigger AttachActionToObject(InvokeCommandAction invokeCommandAction, SysWindows.DependencyObject dependencyObject) { TriggerCollection triggersCollection = Interaction.GetTriggers(dependencyObject); StubTrigger stubTrigger = CreateTrigger(); triggersCollection.Add(stubTrigger); stubTrigger.Actions.Add(invokeCommandAction); return(stubTrigger); }
public void Test_Construct() { // プロパティデフォルト値検証 var target = new InvokeCommandAction(); target.AutoEnable.Should().Be(true); target.Command.Should().BeNull(); target.CommandParameter.Should().BeNull(); target.CommandParameterMode.Should().Be(InvokeCommandAction.CommandParameterModeKind.Auto); }
public void Invoke_WithCommand_InvokesCommand() { CommandHelper commandHelper = CreateCommandHelper(); InvokeCommandAction invokeCommandAction = CreateInvokeCommandActionWithCommand(commandHelper.SuccessCommand); Button button = CreateButton(); StubTrigger trigger = AttachActionToObject(invokeCommandAction, button); trigger.FireStubTrigger(); Assert.IsTrue(commandHelper.Successful, "Command should have been invoked."); }
public void Invoke_ActionWithCommandParameter_UsesParameterCorrectly() { Rectangle triggerParameter = CreateRectangle(); Button behaviorParameter = CreateButton(); InvokeCommandAction invokeCommandAction = CreateInvokeCommandActionWithCommandNameAndParameter("StubCommandWithParameter", behaviorParameter); StubBehavior stubBehavior = CreateStubBehavior(); StubTrigger trigger = AttachActionToObject(invokeCommandAction, stubBehavior); trigger.FireStubTrigger(triggerParameter); Assert.AreEqual(stubBehavior.LastParameter, behaviorParameter, "stubBehavior.LastParameter == button"); }
public void Test_AutoEnable_Disable_to_Enable() { // AutoEnable=Disable -> Enable -> Disable // コマンドのモック var cmdMock = new TestCommand(); cmdMock.Setup_CanExecute(false); // リンク先要素 var element = new UIElement(); element.IsEnabled.Should().Be(true); // 初期状態を一応確認 // テスト対象の準備 var target = new InvokeCommandAction(); target.AutoEnable = false; // テスト対象のアクションを呼び出すためのトリガ作成&アタッチ var trigger = new TestTrigger(); trigger.Attach(element); trigger.Actions.Add(target); // コマンド設定 target.Command = cmdMock.Object; // コマンド実行可能状態変更(→true)して、変化無いことを確認 cmdMock.Setup_CanExecute(true); cmdMock.Raise_CanExecuteChanged(); element.IsEnabled.Should().Be(true); // 初期状態から変化無いこと // コマンド実行可能状態変更(→false)して、変化無いことを確認 cmdMock.Setup_CanExecute(false); cmdMock.Raise_CanExecuteChanged(); element.IsEnabled.Should().Be(true); // 初期状態から変化無いこと // リンク無効なので呼び出されないことの検証 cmdMock.Verify(c => c.CanExecute(It.IsAny <object>()), Times.Never()); // リンク有効化。状態反映される確認 target.AutoEnable = true; element.IsEnabled.Should().Be(false); // コマンド状態にリンク cmdMock.Verify(c => c.CanExecute(It.IsAny <object>()), Times.Once()); // 有効状態での状態変化のリンク cmdMock.Setup_CanExecute(true); cmdMock.Raise_CanExecuteChanged(); element.IsEnabled.Should().Be(true); // リンクして変化すること cmdMock.Setup_CanExecute(false); cmdMock.Raise_CanExecuteChanged(); element.IsEnabled.Should().Be(false); // リンクして変化すること }
public void WhenCommandIsSetAndThenBehaviorIsAttached_ThenCommandsCanExecuteIsCalledOnce() { var someControl = new TextBox(); var command = new MockCommand(); var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.Attach(someControl); Assert.Equal(1, command.CanExecuteTimesCalled); }
public void Test_AutoEnable_Enable_SwitchCommand() { // AutoEnable=Enable -> Attach Element -> Set Command -> Switch Command // コマンドのモック var cmdMock = new TestCommand(); cmdMock.Setup_CanExecute(false); // リンク先要素 var element = new UIElement(); element.IsEnabled.Should().Be(true); // 初期状態を一応確認 // テスト対象の準備 var target = new InvokeCommandAction(); target.AutoEnable = true; // テスト対象のアクションを呼び出すためのトリガ作成 var trigger = new TestTrigger(); trigger.Attach(element); trigger.Actions.Add(target); // コマンド設定 target.Command = cmdMock.Object; using (var elementMon = element.Monitor()) { // 同じ状態の別コマンドのモック var otherCcmd1Mock = new TestCommand(); otherCcmd1Mock.Setup_CanExecute(false); // コマンド差し替え target.Command = otherCcmd1Mock.Object; // 状態は維持されるし、一時的な変化も生じていないこと element.IsEnabled.Should().Be(false); elementMon.Should().NotRaise(nameof(element.IsEnabledChanged)); // 別状態の別コマンドのモック var otherCmd2Mock = new TestCommand(); otherCmd2Mock.Setup_CanExecute(true); // コマンド差し替え target.Command = otherCmd2Mock.Object; // 差し替え後の状態を反映していること element.IsEnabled.Should().Be(true); elementMon.Should().Raise(nameof(element.IsEnabledChanged)); } }
private static void BindCommandToEvent(FrameworkElement element, string commandName, string eventName) { var binding = new Binding(commandName); binding.Source = element.DataContext; var cmdAction = new InvokeCommandAction(); BindingOperations.SetBinding(cmdAction, InvokeCommandAction.CommandProperty, binding); var trigger = new System.Windows.Interactivity.EventTrigger(eventName); trigger.Actions.Add(cmdAction); Interaction.GetTriggers(element).Add(trigger); }
/// <summary> /// This method exists as a hack to get the XAML assemblies loaded in the plugin. /// </summary> public static void LoadAssemblies() { var formatter = FormatterAssemblyStyle.Full; var action = new i.InvokeCommandAction(); action.CommandName = "Loaded"; var window = new Window(); ViewModelLocator.SetAutoWireViewModel(window, false); var action2 = new InvokeCommandAction(); action.CommandName = "Loaded"; }
protected override void OnAttached() { var triggers = Interaction_ .GetTriggers(AssociatedObject) .OfType <EventTrigger>(); var actions = triggers .First(t => t.EventName == "Closing") .Actions .OfType <InvokeCommandAction>(); _commandAction = actions.First(); AssociatedObject.Closing += _windowClosingHandler; }
public void WhenCommandAndCommandParameterAreSetPriorToBehaviorBeingAttached_ThenCommandIsExecutedCorrectlyOnInvoke() { var someControl = new TextBox(); var command = new MockCommand(); var parameter = new object(); var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.CommandParameter = parameter; commandAction.Attach(someControl); commandAction.InvokeAction(null); Assert.IsTrue(command.ExecuteCalled); }
public void WhenChangingProperty_ThenUpdatesCommand() { var someControl = new TextBox(); var oldCommand = new MockCommand(); var newCommand = new MockCommand(); var commandAction = new InvokeCommandAction(); commandAction.Attach(someControl); commandAction.Command = oldCommand; commandAction.Command = newCommand; commandAction.Invoke(); Assert.IsTrue(newCommand.ExecuteCalled); Assert.IsFalse(oldCommand.ExecuteCalled); }
public void Invoke_WithCommandNameAndCommand_InvokesCommand() { CommandHelper commandHelper = CreateCommandHelper(); InvokeCommandAction invokeCommandAction = CreateInvokeCommandActionWithCommandName("Command"); invokeCommandAction.Command = commandHelper.SuccessCommand; Button button = CreateButton(); button.Command = commandHelper.FailCommand; StubTrigger trigger = AttachActionToObject(invokeCommandAction, button); trigger.FireStubTrigger(); Assert.IsTrue(commandHelper.Successful, "Command should have been invoked, CommandName should not have been invoked."); }
public void Test_AutoEnable_Enable_AttachElem_SetCommand() { // AutoEnable=Enable -> Attach Element -> Set Command // コマンドのモック var cmdMock = new TestCommand(); cmdMock.Setup_CanExecute(true); // リンク先要素 var element = new UIElement(); element.IsEnabled.Should().Be(true); // 初期状態を一応確認 // テスト対象の準備 var target = new InvokeCommandAction(); target.AutoEnable = true; // テスト対象のアクションを呼び出すためのトリガ作成 var trigger = new TestTrigger(); trigger.Attach(element); trigger.Actions.Add(target); // コマンド設定 target.Command = cmdMock.Object; // リンク有効なのでアタッチ時点で呼び出しがあるはず cmdMock.Verify(c => c.CanExecute(It.IsAny <object>()), Times.AtLeastOnce()); // コマンド実行可能状態変更(→false)して、連携することを確認 cmdMock.Invocations.Clear(); cmdMock.Setup_CanExecute(false); cmdMock.Raise_CanExecuteChanged(); element.IsEnabled.Should().Be(false); // リンク有効なので呼び出しがあるはず cmdMock.Verify(c => c.CanExecute(It.IsAny <object>()), Times.AtLeastOnce()); // コマンド実行可能状態変更(→false)して、連携することを確認 cmdMock.Invocations.Clear(); cmdMock.Setup_CanExecute(true); cmdMock.Raise_CanExecuteChanged(); element.IsEnabled.Should().Be(true); // リンク有効なので呼び出しがあるはず cmdMock.Verify(c => c.CanExecute(It.IsAny <object>()), Times.AtLeastOnce()); }
public void WhenCommandAndCommandParameterAreSetPriorToBehaviorBeingAttached_ThenCommandIsExecutedCorrectlyOnInvoke() { var someControl = new TextBox(); var command = new MockCommand(); var parameter = new object(); var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.CommandParameter = parameter; commandAction.Attach(someControl); commandAction.InvokeAction(null); Assert.True(command.ExecuteCalled); }
public void WhenCommandParameterNotSet_ThenEventArgsPassed() { var eventArgs = new TestEventArgs(null); var someControl = new TextBox(); var command = new MockCommand(); var parameter = new object(); var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.Attach(someControl); commandAction.InvokeAction(eventArgs); Assert.IsType <TestEventArgs>(command.ExecuteParameter); }
public void WhenCommandPropertyIsSet_ThenHooksUpCommandBehavior() { var someControl = new TextBox(); var commandAction = new InvokeCommandAction(); var command = new MockCommand(); commandAction.Attach(someControl); commandAction.Command = command; Assert.IsFalse(command.ExecuteCalled); commandAction.Invoke(); Assert.IsTrue(command.ExecuteCalled); Assert.AreSame(command, commandAction.GetValue(InvokeCommandAction.CommandProperty)); }
public void WhenChangingProperty_ThenUpdatesCommand() { var someControl = new TextBox(); var oldCommand = new MockCommand(); var newCommand = new MockCommand(); var commandAction = new InvokeCommandAction(); commandAction.Attach(someControl); commandAction.Command = oldCommand; commandAction.Command = newCommand; commandAction.InvokeAction(null); Assert.True(newCommand.ExecuteCalled); Assert.False(oldCommand.ExecuteCalled); }
public void WhenAttachedAfterCommandPropertyIsSetAndInvoked_ThenInvokesCommand() { var someControl = new TextBox(); var commandAction = new InvokeCommandAction(); var command = new MockCommand(); commandAction.Command = command; commandAction.Attach(someControl); Assert.IsFalse(command.ExecuteCalled); commandAction.Invoke(); Assert.IsTrue(command.ExecuteCalled); Assert.AreSame(command, commandAction.GetValue(InvokeCommandAction.CommandProperty)); }
public void WhenCommandParameterNotSetAndEventArgsParameterPathSet_ThenPathedValuePassed() { var eventArgs = new TestEventArgs("testname"); var someControl = new TextBox(); var command = new MockCommand(); var parameter = new object(); var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.TriggerParameterPath = "Thing1.Thing2.Name"; commandAction.Attach(someControl); commandAction.InvokeAction(eventArgs); Assert.Equal("testname", command.ExecuteParameter); }
public void WhenCommandPropertyIsSet_ThenHooksUpCommandBehavior() { var someControl = new TextBox(); var commandAction = new InvokeCommandAction(); var command = new MockCommand(); commandAction.Attach(someControl); commandAction.Command = command; Assert.False(command.ExecuteCalled); commandAction.InvokeAction(null); Assert.True(command.ExecuteCalled); Assert.Same(command, commandAction.GetValue(InvokeCommandAction.CommandProperty)); }
public void WhenAttachedAfterCommandPropertyIsSetAndInvoked_ThenInvokesCommand() { var someControl = new TextBox(); var commandAction = new InvokeCommandAction(); var command = new MockCommand(); commandAction.Command = command; commandAction.Attach(someControl); Assert.False(command.ExecuteCalled); commandAction.InvokeAction(null); Assert.True(command.ExecuteCalled); Assert.Same(command, commandAction.GetValue(InvokeCommandAction.CommandProperty)); }
public void WhenAttachedAndCanExecuteReturnsFalse_ThenEnabledUIElementIsDisabled() { var someControl = new TextBox(); someControl.IsEnabled = true; var command = new MockCommand(); command.CanExecuteReturnValue = false; var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.Attach(someControl); Assert.False(someControl.IsEnabled); }
public void WhenAutoEnableIsFalse_ThenEnabledUIElementRemainsEnabled() { var someControl = new TextBox(); someControl.IsEnabled = true; var command = new MockCommand(); command.CanExecuteReturnValue = false; var commandAction = new InvokeCommandAction(); commandAction.AutoEnable = false; commandAction.Command = command; commandAction.Attach(someControl); Assert.IsTrue(someControl.IsEnabled); }
public void Invoke_ActionWithPassEventArgsToCommandFalse_DoesNotPassEventArgsToCommand() { StubBehavior stubBehavior = CreateStubBehavior(); InvokeCommandAction invokeCommandAction = CreateInvokeCommandAction(); invokeCommandAction.Command = stubBehavior.StubCommandWithParameter; invokeCommandAction.PassEventArgsToCommand = false; StubTrigger trigger = AttachActionToObject(invokeCommandAction, stubBehavior); var args = new EventArgsMock(); trigger.FireStubTrigger(args); Assert.IsNull(stubBehavior.LastParameter); }
public void WhenInvokedWithCommandParameter_ThenPassesCommandParaeterToExecute() { var someControl = new TextBox(); var command = new MockCommand(); var parameter = new object(); var commandAction = new InvokeCommandAction(); commandAction.Attach(someControl); commandAction.Command = command; commandAction.CommandParameter = parameter; Assert.IsNull(command.ExecuteParameter); commandAction.Invoke(); Assert.IsTrue(command.ExecuteCalled); Assert.IsNotNull(command.ExecuteParameter); Assert.AreSame(parameter, command.ExecuteParameter); }
public void WhenCanExecuteChanged_ThenUpdatesIsEnabledState() { var someControl = new TextBox(); var command = new MockCommand(); var parameter = new object(); var commandAction = new InvokeCommandAction(); commandAction.Attach(someControl); commandAction.Command = command; commandAction.CommandParameter = parameter; Assert.IsTrue(someControl.IsEnabled); command.CanExecuteReturnValue = false; command.RaiseCanExecuteChanged(); Assert.IsNotNull(command.CanExecuteParameter); Assert.AreSame(parameter, command.CanExecuteParameter); Assert.IsFalse(someControl.IsEnabled); }
public void WhenCommandParameterNotSetAndEventArgsParameterPathSet_ThenPathedValuePassed() { var eventArgs = new TestEventArgs("testname"); var someControl = new TextBox(); var command = new MockCommand(); var parameter = new object(); var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.TriggerParameterPath = "Thing1.Thing2.Name"; commandAction.Attach(someControl); commandAction.InvokeAction(eventArgs); Assert.AreEqual("testname", command.ExecuteParameter); }
public void WhenAutoEnableIsUpdated_ThenEnabledUIElementIsDisabled() { var someControl = new TextBox(); someControl.IsEnabled = true; var command = new MockCommand(); command.CanExecuteReturnValue = false; var commandAction = new InvokeCommandAction(); commandAction.AutoEnable = false; commandAction.Command = command; commandAction.Attach(someControl); Assert.IsTrue(someControl.IsEnabled); commandAction.AutoEnable = true; Assert.IsFalse(someControl.IsEnabled); }
public void WhenAttachedAndCanExecuteReturnsTrue_ThenDisabledUIElementIsEnabled() { var someControl = new TextBox(); someControl.IsEnabled = false; var command = new MockCommand(); command.CanExecuteReturnValue = true; var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.Attach(someControl); Assert.IsTrue(someControl.IsEnabled); }
public void WhenCommandIsSetAndThenBehaviorIsAttached_ThenCommandsCanExecuteIsCalledOnce() { var someControl = new TextBox(); var command = new MockCommand(); var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.Attach(someControl); Assert.AreEqual(1, command.CanExecuteTimesCalled); }
public void WhenDetatched_ThenSetsCommandAndCommandParameterToNull() { var someControl = new TextBox(); var command = new MockCommand(); var parameter = new object(); var commandAction = new InvokeCommandAction(); commandAction.Attach(someControl); commandAction.Command = command; commandAction.CommandParameter = parameter; Assert.IsNotNull(commandAction.Command); Assert.IsNotNull(commandAction.CommandParameter); commandAction.Detach(); Assert.IsNull(commandAction.Command); Assert.IsNull(commandAction.CommandParameter); }
public void WhenCommandParameterNotSet_ThenEventArgsPassed() { var eventArgs = new TestEventArgs(null); var someControl = new TextBox(); var command = new MockCommand(); var parameter = new object(); var commandAction = new InvokeCommandAction(); commandAction.Command = command; commandAction.Attach(someControl); commandAction.InvokeAction(eventArgs); Assert.IsInstanceOfType(command.ExecuteParameter, typeof(TestEventArgs)); }