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/shared/chrome/browser/ui/commands/command_dispatcher.h" | 5 #import "ios/shared/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" |
11 | 11 |
12 #if !defined(__has_feature) || !__has_feature(objc_arc) | 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
13 #error "This file requires ARC support." | 13 #error "This file requires ARC support." |
14 #endif | 14 #endif |
15 | 15 |
16 #pragma mark - Test handlers | 16 #pragma mark - Test handlers |
17 | 17 |
| 18 @protocol ShowProtocol<NSObject> |
| 19 - (void)show; |
| 20 - (void)showMore; |
| 21 @end |
| 22 |
18 // A handler with methods that take no arguments. | 23 // A handler with methods that take no arguments. |
19 @interface CommandDispatcherTestSimpleTarget : NSObject | 24 @interface CommandDispatcherTestSimpleTarget : NSObject<ShowProtocol> |
20 | 25 |
21 // Will be set to YES when the |-show| method is called. | 26 // Will be set to YES when the |-show| method is called. |
22 @property(nonatomic, assign) BOOL showCalled; | 27 @property(nonatomic, assign) BOOL showCalled; |
23 | 28 |
| 29 // Will be set to YES when the |-showMore| method is called. |
| 30 @property(nonatomic, assign) BOOL showMoreCalled; |
| 31 |
24 // Will be set to YES when the |-hide| method is called. | 32 // Will be set to YES when the |-hide| method is called. |
25 @property(nonatomic, assign) BOOL hideCalled; | 33 @property(nonatomic, assign) BOOL hideCalled; |
26 | 34 |
27 // Resets the above properties to NO. | 35 // Resets the above properties to NO. |
28 - (void)resetProperties; | 36 - (void)resetProperties; |
29 | 37 |
30 // Handler methods. | 38 // Handler methods. |
31 - (void)show; | |
32 - (void)hide; | 39 - (void)hide; |
33 | 40 |
34 @end | 41 @end |
35 | 42 |
36 @implementation CommandDispatcherTestSimpleTarget | 43 @implementation CommandDispatcherTestSimpleTarget |
37 | 44 |
38 @synthesize showCalled = _showCalled; | 45 @synthesize showCalled = _showCalled; |
| 46 @synthesize showMoreCalled = _showMoreCalled; |
39 @synthesize hideCalled = _hideCalled; | 47 @synthesize hideCalled = _hideCalled; |
40 | 48 |
41 - (void)resetProperties { | 49 - (void)resetProperties { |
42 self.showCalled = NO; | 50 self.showCalled = NO; |
| 51 self.showMoreCalled = NO; |
43 self.hideCalled = NO; | 52 self.hideCalled = NO; |
44 } | 53 } |
45 | 54 |
46 - (void)show { | 55 - (void)show { |
47 self.showCalled = YES; | 56 self.showCalled = YES; |
48 } | 57 } |
49 | 58 |
| 59 - (void)showMore { |
| 60 self.showMoreCalled = YES; |
| 61 } |
| 62 |
50 - (void)hide { | 63 - (void)hide { |
51 self.hideCalled = YES; | 64 self.hideCalled = YES; |
52 } | 65 } |
53 | 66 |
54 @end | 67 @end |
55 | 68 |
56 // A handler with methods that take various types of arguments. | 69 // A handler with methods that take various types of arguments. |
57 @interface CommandDispatcherTestTargetWithArguments : NSObject | 70 @interface CommandDispatcherTestTargetWithArguments : NSObject |
58 | 71 |
59 // Set to YES when |-methodWithInt:| is called. | 72 // Set to YES when |-methodWithInt:| is called. |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 [dispatcher show]; | 190 [dispatcher show]; |
178 EXPECT_TRUE(showTarget.showCalled); | 191 EXPECT_TRUE(showTarget.showCalled); |
179 EXPECT_FALSE(hideTarget.showCalled); | 192 EXPECT_FALSE(hideTarget.showCalled); |
180 | 193 |
181 [showTarget resetProperties]; | 194 [showTarget resetProperties]; |
182 [dispatcher hide]; | 195 [dispatcher hide]; |
183 EXPECT_FALSE(showTarget.hideCalled); | 196 EXPECT_FALSE(showTarget.hideCalled); |
184 EXPECT_TRUE(hideTarget.hideCalled); | 197 EXPECT_TRUE(hideTarget.hideCalled); |
185 } | 198 } |
186 | 199 |
| 200 // Tests handlers registered via protocols. |
| 201 TEST(CommandDispatcherTest, ProtocolRegistration) { |
| 202 id dispatcher = [[CommandDispatcher alloc] init]; |
| 203 CommandDispatcherTestSimpleTarget* target = |
| 204 [[CommandDispatcherTestSimpleTarget alloc] init]; |
| 205 |
| 206 [dispatcher startDispatchingToTarget:target |
| 207 forProtocol:@protocol(ShowProtocol)]; |
| 208 |
| 209 [dispatcher show]; |
| 210 EXPECT_TRUE(target.showCalled); |
| 211 [dispatcher showMore]; |
| 212 EXPECT_TRUE(target.showCalled); |
| 213 } |
| 214 |
187 // Tests that handlers are no longer forwarded messages after selector | 215 // Tests that handlers are no longer forwarded messages after selector |
188 // deregistration. | 216 // deregistration. |
189 TEST(CommandDispatcherTest, SelectorDeregistration) { | 217 TEST(CommandDispatcherTest, SelectorDeregistration) { |
190 id dispatcher = [[CommandDispatcher alloc] init]; | 218 id dispatcher = [[CommandDispatcher alloc] init]; |
191 CommandDispatcherTestSimpleTarget* target = | 219 CommandDispatcherTestSimpleTarget* target = |
192 [[CommandDispatcherTestSimpleTarget alloc] init]; | 220 [[CommandDispatcherTestSimpleTarget alloc] init]; |
193 | 221 |
194 [dispatcher startDispatchingToTarget:target forSelector:@selector(show)]; | 222 [dispatcher startDispatchingToTarget:target forSelector:@selector(show)]; |
195 [dispatcher startDispatchingToTarget:target forSelector:@selector(hide)]; | 223 [dispatcher startDispatchingToTarget:target forSelector:@selector(hide)]; |
196 | 224 |
(...skipping 10 matching lines...) Expand all Loading... |
207 EXPECT_EQ(NSInvalidArgumentException, [exception name]); | 235 EXPECT_EQ(NSInvalidArgumentException, [exception name]); |
208 exception_caught = true; | 236 exception_caught = true; |
209 } | 237 } |
210 EXPECT_TRUE(exception_caught); | 238 EXPECT_TRUE(exception_caught); |
211 | 239 |
212 [dispatcher hide]; | 240 [dispatcher hide]; |
213 EXPECT_FALSE(target.showCalled); | 241 EXPECT_FALSE(target.showCalled); |
214 EXPECT_TRUE(target.hideCalled); | 242 EXPECT_TRUE(target.hideCalled); |
215 } | 243 } |
216 | 244 |
| 245 // Tests that handlers are no longer forwarded messages after protocol |
| 246 // deregistration. |
| 247 TEST(CommandDispatcherTest, ProtocolDeregistration) { |
| 248 id dispatcher = [[CommandDispatcher alloc] init]; |
| 249 CommandDispatcherTestSimpleTarget* target = |
| 250 [[CommandDispatcherTestSimpleTarget alloc] init]; |
| 251 |
| 252 [dispatcher startDispatchingToTarget:target |
| 253 forProtocol:@protocol(ShowProtocol)]; |
| 254 [dispatcher startDispatchingToTarget:target forSelector:@selector(hide)]; |
| 255 |
| 256 [dispatcher show]; |
| 257 EXPECT_TRUE(target.showCalled); |
| 258 EXPECT_FALSE(target.showMoreCalled); |
| 259 EXPECT_FALSE(target.hideCalled); |
| 260 [target resetProperties]; |
| 261 [dispatcher showMore]; |
| 262 EXPECT_FALSE(target.showCalled); |
| 263 EXPECT_TRUE(target.showMoreCalled); |
| 264 EXPECT_FALSE(target.hideCalled); |
| 265 |
| 266 [target resetProperties]; |
| 267 [dispatcher stopDispatchingForProtocol:@protocol(ShowProtocol)]; |
| 268 bool exception_caught = false; |
| 269 @try { |
| 270 [dispatcher show]; |
| 271 } @catch (NSException* exception) { |
| 272 EXPECT_EQ(NSInvalidArgumentException, [exception name]); |
| 273 exception_caught = true; |
| 274 } |
| 275 EXPECT_TRUE(exception_caught); |
| 276 exception_caught = false; |
| 277 @try { |
| 278 [dispatcher showMore]; |
| 279 } @catch (NSException* exception) { |
| 280 EXPECT_EQ(NSInvalidArgumentException, [exception name]); |
| 281 exception_caught = true; |
| 282 } |
| 283 EXPECT_TRUE(exception_caught); |
| 284 |
| 285 [dispatcher hide]; |
| 286 EXPECT_FALSE(target.showCalled); |
| 287 EXPECT_FALSE(target.showMoreCalled); |
| 288 EXPECT_TRUE(target.hideCalled); |
| 289 } |
| 290 |
217 // Tests that handlers are no longer forwarded messages after target | 291 // Tests that handlers are no longer forwarded messages after target |
218 // deregistration. | 292 // deregistration. |
219 TEST(CommandDispatcherTest, TargetDeregistration) { | 293 TEST(CommandDispatcherTest, TargetDeregistration) { |
220 id dispatcher = [[CommandDispatcher alloc] init]; | 294 id dispatcher = [[CommandDispatcher alloc] init]; |
221 CommandDispatcherTestSimpleTarget* showTarget = | 295 CommandDispatcherTestSimpleTarget* showTarget = |
222 [[CommandDispatcherTestSimpleTarget alloc] init]; | 296 [[CommandDispatcherTestSimpleTarget alloc] init]; |
223 CommandDispatcherTestSimpleTarget* hideTarget = | 297 CommandDispatcherTestSimpleTarget* hideTarget = |
224 [[CommandDispatcherTestSimpleTarget alloc] init]; | 298 [[CommandDispatcherTestSimpleTarget alloc] init]; |
225 | 299 |
226 [dispatcher startDispatchingToTarget:showTarget forSelector:@selector(show)]; | 300 [dispatcher startDispatchingToTarget:showTarget forSelector:@selector(show)]; |
(...skipping 30 matching lines...) Expand all Loading... |
257 bool exception_caught = false; | 331 bool exception_caught = false; |
258 @try { | 332 @try { |
259 [dispatcher hide]; | 333 [dispatcher hide]; |
260 } @catch (NSException* exception) { | 334 } @catch (NSException* exception) { |
261 EXPECT_EQ(NSInvalidArgumentException, [exception name]); | 335 EXPECT_EQ(NSInvalidArgumentException, [exception name]); |
262 exception_caught = true; | 336 exception_caught = true; |
263 } | 337 } |
264 | 338 |
265 EXPECT_TRUE(exception_caught); | 339 EXPECT_TRUE(exception_caught); |
266 } | 340 } |
OLD | NEW |