OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/clean/chrome/browser/ui/commands/command_dispatcher.h" | 5 #import "ios/clean/chrome/browser/ui/commands/command_dispatcher.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 @end | 109 @end |
110 | 110 |
111 #pragma mark - Tests | 111 #pragma mark - Tests |
112 | 112 |
113 // Tests handler methods with no arguments. | 113 // Tests handler methods with no arguments. |
114 TEST(CommandDispatcherTest, SimpleTarget) { | 114 TEST(CommandDispatcherTest, SimpleTarget) { |
115 id dispatcher = [[CommandDispatcher alloc] init]; | 115 id dispatcher = [[CommandDispatcher alloc] init]; |
116 CommandDispatcherTestSimpleTarget* target = | 116 CommandDispatcherTestSimpleTarget* target = |
117 [[CommandDispatcherTestSimpleTarget alloc] init]; | 117 [[CommandDispatcherTestSimpleTarget alloc] init]; |
118 | 118 |
119 [dispatcher registerTarget:target forSelector:@selector(show)]; | 119 [dispatcher startDispatchingToTarget:target forSelector:@selector(show)]; |
120 [dispatcher registerTarget:target forSelector:@selector(hide)]; | 120 [dispatcher startDispatchingToTarget:target forSelector:@selector(hide)]; |
121 | 121 |
122 [dispatcher show]; | 122 [dispatcher show]; |
123 EXPECT_TRUE(target.showCalled); | 123 EXPECT_TRUE(target.showCalled); |
124 EXPECT_FALSE(target.hideCalled); | 124 EXPECT_FALSE(target.hideCalled); |
125 | 125 |
126 [target resetProperties]; | 126 [target resetProperties]; |
127 [dispatcher hide]; | 127 [dispatcher hide]; |
128 EXPECT_FALSE(target.showCalled); | 128 EXPECT_FALSE(target.showCalled); |
129 EXPECT_TRUE(target.hideCalled); | 129 EXPECT_TRUE(target.hideCalled); |
130 } | 130 } |
131 | 131 |
132 // Tests handler methods that take arguments. | 132 // Tests handler methods that take arguments. |
133 TEST(CommandDispatcherTest, TargetWithArguments) { | 133 TEST(CommandDispatcherTest, TargetWithArguments) { |
134 id dispatcher = [[CommandDispatcher alloc] init]; | 134 id dispatcher = [[CommandDispatcher alloc] init]; |
135 CommandDispatcherTestTargetWithArguments* target = | 135 CommandDispatcherTestTargetWithArguments* target = |
136 [[CommandDispatcherTestTargetWithArguments alloc] init]; | 136 [[CommandDispatcherTestTargetWithArguments alloc] init]; |
137 | 137 |
138 [dispatcher registerTarget:target forSelector:@selector(methodWithInt:)]; | 138 [dispatcher startDispatchingToTarget:target |
139 [dispatcher registerTarget:target forSelector:@selector(methodWithObject:)]; | 139 forSelector:@selector(methodWithInt:)]; |
140 [dispatcher registerTarget:target | 140 [dispatcher startDispatchingToTarget:target |
141 forSelector:@selector(methodToAddFirstArgument:toSecond:)]; | 141 forSelector:@selector(methodWithObject:)]; |
| 142 [dispatcher |
| 143 startDispatchingToTarget:target |
| 144 forSelector:@selector(methodToAddFirstArgument:toSecond:)]; |
142 | 145 |
143 const int int_argument = 4; | 146 const int int_argument = 4; |
144 [dispatcher methodWithInt:int_argument]; | 147 [dispatcher methodWithInt:int_argument]; |
145 EXPECT_TRUE(target.intMethodCalled); | 148 EXPECT_TRUE(target.intMethodCalled); |
146 EXPECT_FALSE(target.objectMethodCalled); | 149 EXPECT_FALSE(target.objectMethodCalled); |
147 EXPECT_EQ(int_argument, target.intArgument); | 150 EXPECT_EQ(int_argument, target.intArgument); |
148 | 151 |
149 [target resetProperties]; | 152 [target resetProperties]; |
150 NSObject* object_argument = [[NSObject alloc] init]; | 153 NSObject* object_argument = [[NSObject alloc] init]; |
151 [dispatcher methodWithObject:object_argument]; | 154 [dispatcher methodWithObject:object_argument]; |
(...skipping 10 matching lines...) Expand all Loading... |
162 | 165 |
163 // Tests that messages are routed to the proper handler when multiple targets | 166 // Tests that messages are routed to the proper handler when multiple targets |
164 // are registered. | 167 // are registered. |
165 TEST(CommandDispatcherTest, MultipleTargets) { | 168 TEST(CommandDispatcherTest, MultipleTargets) { |
166 id dispatcher = [[CommandDispatcher alloc] init]; | 169 id dispatcher = [[CommandDispatcher alloc] init]; |
167 CommandDispatcherTestSimpleTarget* showTarget = | 170 CommandDispatcherTestSimpleTarget* showTarget = |
168 [[CommandDispatcherTestSimpleTarget alloc] init]; | 171 [[CommandDispatcherTestSimpleTarget alloc] init]; |
169 CommandDispatcherTestSimpleTarget* hideTarget = | 172 CommandDispatcherTestSimpleTarget* hideTarget = |
170 [[CommandDispatcherTestSimpleTarget alloc] init]; | 173 [[CommandDispatcherTestSimpleTarget alloc] init]; |
171 | 174 |
172 [dispatcher registerTarget:showTarget forSelector:@selector(show)]; | 175 [dispatcher startDispatchingToTarget:showTarget forSelector:@selector(show)]; |
173 [dispatcher registerTarget:hideTarget forSelector:@selector(hide)]; | 176 [dispatcher startDispatchingToTarget:hideTarget forSelector:@selector(hide)]; |
174 | 177 |
175 [dispatcher show]; | 178 [dispatcher show]; |
176 EXPECT_TRUE(showTarget.showCalled); | 179 EXPECT_TRUE(showTarget.showCalled); |
177 EXPECT_FALSE(hideTarget.showCalled); | 180 EXPECT_FALSE(hideTarget.showCalled); |
178 | 181 |
179 [showTarget resetProperties]; | 182 [showTarget resetProperties]; |
180 [dispatcher hide]; | 183 [dispatcher hide]; |
181 EXPECT_FALSE(showTarget.hideCalled); | 184 EXPECT_FALSE(showTarget.hideCalled); |
182 EXPECT_TRUE(hideTarget.hideCalled); | 185 EXPECT_TRUE(hideTarget.hideCalled); |
183 } | 186 } |
184 | 187 |
185 // Tests that handlers are no longer forwarded messages after deregistration. | 188 // Tests that handlers are no longer forwarded messages after deregistration. |
186 TEST(CommandDispatcherTest, Deregistration) { | 189 TEST(CommandDispatcherTest, Deregistration) { |
187 id dispatcher = [[CommandDispatcher alloc] init]; | 190 id dispatcher = [[CommandDispatcher alloc] init]; |
188 CommandDispatcherTestSimpleTarget* showTarget = | 191 CommandDispatcherTestSimpleTarget* showTarget = |
189 [[CommandDispatcherTestSimpleTarget alloc] init]; | 192 [[CommandDispatcherTestSimpleTarget alloc] init]; |
190 CommandDispatcherTestSimpleTarget* hideTarget = | 193 CommandDispatcherTestSimpleTarget* hideTarget = |
191 [[CommandDispatcherTestSimpleTarget alloc] init]; | 194 [[CommandDispatcherTestSimpleTarget alloc] init]; |
192 | 195 |
193 [dispatcher registerTarget:showTarget forSelector:@selector(show)]; | 196 [dispatcher startDispatchingToTarget:showTarget forSelector:@selector(show)]; |
194 [dispatcher registerTarget:hideTarget forSelector:@selector(hide)]; | 197 [dispatcher startDispatchingToTarget:hideTarget forSelector:@selector(hide)]; |
195 | 198 |
196 [dispatcher show]; | 199 [dispatcher show]; |
197 EXPECT_TRUE(showTarget.showCalled); | 200 EXPECT_TRUE(showTarget.showCalled); |
198 EXPECT_FALSE(hideTarget.showCalled); | 201 EXPECT_FALSE(hideTarget.showCalled); |
199 | 202 |
200 [dispatcher stopDispatchingForTarget:showTarget]; | 203 [dispatcher stopDispatchingToTarget:showTarget]; |
201 bool exception_caught = false; | 204 bool exception_caught = false; |
202 @try { | 205 @try { |
203 [dispatcher show]; | 206 [dispatcher show]; |
204 } @catch (NSException* exception) { | 207 } @catch (NSException* exception) { |
205 EXPECT_EQ(NSInvalidArgumentException, [exception name]); | 208 EXPECT_EQ(NSInvalidArgumentException, [exception name]); |
206 exception_caught = true; | 209 exception_caught = true; |
207 } | 210 } |
208 EXPECT_TRUE(exception_caught); | 211 EXPECT_TRUE(exception_caught); |
209 | 212 |
210 [dispatcher hide]; | 213 [dispatcher hide]; |
211 EXPECT_FALSE(showTarget.hideCalled); | 214 EXPECT_FALSE(showTarget.hideCalled); |
212 EXPECT_TRUE(hideTarget.hideCalled); | 215 EXPECT_TRUE(hideTarget.hideCalled); |
213 } | 216 } |
214 | 217 |
215 // Tests that an exception is thrown when there is no registered handler for a | 218 // Tests that an exception is thrown when there is no registered handler for a |
216 // given selector. | 219 // given selector. |
217 TEST(CommandDispatcherTest, NoTargetRegisteredForSelector) { | 220 TEST(CommandDispatcherTest, NoTargetRegisteredForSelector) { |
218 id dispatcher = [[CommandDispatcher alloc] init]; | 221 id dispatcher = [[CommandDispatcher alloc] init]; |
219 CommandDispatcherTestSimpleTarget* target = | 222 CommandDispatcherTestSimpleTarget* target = |
220 [[CommandDispatcherTestSimpleTarget alloc] init]; | 223 [[CommandDispatcherTestSimpleTarget alloc] init]; |
221 | 224 |
222 [dispatcher registerTarget:target forSelector:@selector(show)]; | 225 [dispatcher startDispatchingToTarget:target forSelector:@selector(show)]; |
223 | 226 |
224 bool exception_caught = false; | 227 bool exception_caught = false; |
225 @try { | 228 @try { |
226 [dispatcher hide]; | 229 [dispatcher hide]; |
227 } @catch (NSException* exception) { | 230 } @catch (NSException* exception) { |
228 EXPECT_EQ(NSInvalidArgumentException, [exception name]); | 231 EXPECT_EQ(NSInvalidArgumentException, [exception name]); |
229 exception_caught = true; | 232 exception_caught = true; |
230 } | 233 } |
231 | 234 |
232 EXPECT_TRUE(exception_caught); | 235 EXPECT_TRUE(exception_caught); |
233 } | 236 } |
OLD | NEW |