4
4
using System . Windows . Forms ;
5
5
using ReactiveUI . Winforms ;
6
6
using Xunit ;
7
+ using System . ComponentModel ;
7
8
8
9
namespace ReactiveUI . Tests . Winforms
9
10
{
@@ -25,14 +26,12 @@ public void CommandBinderBindsToButton()
25
26
commandExecuted = true ;
26
27
} ) ;
27
28
28
- var disp = fixture . BindCommandToObject ( cmd , input , Observable . Return ( ( object ) 5 ) ) ;
29
-
30
- input . PerformClick ( ) ;
31
-
32
- Assert . True ( commandExecuted ) ;
33
- Assert . NotNull ( ea ) ;
34
- disp . Dispose ( ) ;
29
+ using ( var disp = fixture . BindCommandToObject ( cmd , input , Observable . Return ( ( object ) 5 ) ) ) {
30
+ input . PerformClick ( ) ;
35
31
32
+ Assert . True ( commandExecuted ) ;
33
+ Assert . NotNull ( ea ) ;
34
+ }
36
35
}
37
36
38
37
[ Fact ]
@@ -51,14 +50,36 @@ public void CommandBinderBindsToCustomControl()
51
50
commandExecuted = true ;
52
51
} ) ;
53
52
54
- var disp = fixture . BindCommandToObject ( cmd , input , Observable . Return ( ( object ) 5 ) ) ;
53
+ using ( var disp = fixture . BindCommandToObject ( cmd , input , Observable . Return ( ( object ) 5 ) ) ) {
54
+ input . PerformClick ( ) ;
55
55
56
- input . PerformClick ( ) ;
56
+ Assert . True ( commandExecuted ) ;
57
+ Assert . NotNull ( ea ) ;
58
+ }
59
+ }
57
60
58
- Assert . True ( commandExecuted ) ;
59
- Assert . NotNull ( ea ) ;
60
- disp . Dispose ( ) ;
61
+ [ Fact ]
62
+ public void CommandBinderBindsToCustomComponent ( )
63
+ {
64
+ var fixture = new CreatesWinformsCommandBinding ( ) ;
65
+ var cmd = ReactiveCommand . Create ( ) ;
66
+ var input = new CustomClickableComponent { } ;
61
67
68
+ Assert . True ( fixture . GetAffinityForObject ( input . GetType ( ) , true ) > 0 ) ;
69
+ Assert . True ( fixture . GetAffinityForObject ( input . GetType ( ) , false ) > 0 ) ;
70
+ bool commandExecuted = false ;
71
+ object ea = null ;
72
+ cmd . Subscribe ( o => {
73
+ ea = o ;
74
+ commandExecuted = true ;
75
+ } ) ;
76
+
77
+ using ( var disp = fixture . BindCommandToObject ( cmd , input , Observable . Return ( ( object ) 5 ) ) ) {
78
+ input . PerformClick ( ) ;
79
+
80
+ Assert . True ( commandExecuted ) ;
81
+ Assert . NotNull ( ea ) ;
82
+ }
62
83
}
63
84
64
85
[ Fact ]
@@ -73,16 +94,44 @@ public void CommandBinderAffectsEnabledState()
73
94
74
95
using ( var disp = fixture . BindCommandToObject ( cmd , input , Observable . Return ( ( object ) 5 ) ) ) {
75
96
canExecute . OnNext ( true ) ;
76
-
77
97
Assert . True ( input . Enabled ) ;
78
98
79
99
canExecute . OnNext ( false ) ;
100
+ Assert . False ( input . Enabled ) ;
101
+ }
102
+ }
80
103
104
+ [ Fact ]
105
+ public void CommandBinderAffectsEnabledStateForComponents ( )
106
+ {
107
+ var fixture = new CreatesWinformsCommandBinding ( ) ;
108
+ var canExecute = new Subject < bool > ( ) ;
109
+ canExecute . OnNext ( true ) ;
110
+
111
+ var cmd = ReactiveCommand . Create ( canExecute ) ;
112
+ var input = new ToolStripButton { } ; // ToolStripButton is a Component, not a Control
113
+
114
+ using ( var disp = fixture . BindCommandToObject ( cmd , input , Observable . Return ( ( object ) 5 ) ) ) {
115
+ canExecute . OnNext ( true ) ;
116
+ Assert . True ( input . Enabled ) ;
117
+
118
+ canExecute . OnNext ( false ) ;
81
119
Assert . False ( input . Enabled ) ;
82
120
}
83
121
}
84
122
}
85
123
124
+ public class CustomClickableComponent : Component
125
+ {
126
+ public event EventHandler Click ;
127
+
128
+ public void PerformClick ( )
129
+ {
130
+ if ( Click != null )
131
+ Click ( this , EventArgs . Empty ) ;
132
+ }
133
+ }
134
+
86
135
public class CustomClickableControl : Control
87
136
{
88
137
public void PerformClick ( )
@@ -99,7 +148,6 @@ public void RaiseMouseUpEvent(System.Windows.Forms.MouseEventArgs args)
99
148
{
100
149
this . OnMouseUp ( args ) ;
101
150
}
102
-
103
151
}
104
152
105
153
public class CommandBindingImplementationTests
0 commit comments