OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/clean/chrome/browser/ui/commands/command_dispatcher.h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 13 #error "This file requires ARC support." |
| 14 #endif |
| 15 |
| 16 #pragma mark - Test handlers |
| 17 |
| 18 // A handler with methods that take no arguments. |
| 19 @interface CommandDispatcherTestSimpleTarget : NSObject |
| 20 |
| 21 // Will be set to YES when the |-show| method is called. |
| 22 @property(nonatomic, assign) BOOL showCalled; |
| 23 |
| 24 // Will be set to YES when the |-hide| method is called. |
| 25 @property(nonatomic, assign) BOOL hideCalled; |
| 26 |
| 27 // Resets the above properties to NO. |
| 28 - (void)resetProperties; |
| 29 |
| 30 // Handler methods. |
| 31 - (void)show; |
| 32 - (void)hide; |
| 33 |
| 34 @end |
| 35 |
| 36 @implementation CommandDispatcherTestSimpleTarget |
| 37 |
| 38 @synthesize showCalled = _showCalled; |
| 39 @synthesize hideCalled = _hideCalled; |
| 40 |
| 41 - (void)resetProperties { |
| 42 self.showCalled = NO; |
| 43 self.hideCalled = NO; |
| 44 } |
| 45 |
| 46 - (void)show { |
| 47 self.showCalled = YES; |
| 48 } |
| 49 |
| 50 - (void)hide { |
| 51 self.hideCalled = YES; |
| 52 } |
| 53 |
| 54 @end |
| 55 |
| 56 // A handler with methods that take various types of arguments. |
| 57 @interface CommandDispatcherTestTargetWithArguments : NSObject |
| 58 |
| 59 // Set to YES when |-methodWithInt:| is called. |
| 60 @property(nonatomic, assign) BOOL intMethodCalled; |
| 61 |
| 62 // The argument passed to the most recent call of |-methodWithInt:|. |
| 63 @property(nonatomic, assign) int intArgument; |
| 64 |
| 65 // Set to YES when |-methodWithObject:| is called. |
| 66 @property(nonatomic, assign) BOOL objectMethodCalled; |
| 67 |
| 68 // The argument passed to the most recent call of |-methodWithObject:|. |
| 69 @property(nonatomic, strong) NSObject* objectArgument; |
| 70 |
| 71 // Resets the above properties to NO or nil. |
| 72 - (void)resetProperties; |
| 73 |
| 74 // Handler methods. |
| 75 - (void)methodWithInt:(int)arg; |
| 76 - (void)methodWithObject:(NSObject*)arg; |
| 77 - (int)methodToAddFirstArgument:(int)first toSecond:(int)second; |
| 78 |
| 79 @end |
| 80 |
| 81 @implementation CommandDispatcherTestTargetWithArguments |
| 82 |
| 83 @synthesize intMethodCalled = _intMethodCalled; |
| 84 @synthesize intArgument = _intArgument; |
| 85 @synthesize objectMethodCalled = _objectMethodCalled; |
| 86 @synthesize objectArgument = _objectArgument; |
| 87 |
| 88 - (void)resetProperties { |
| 89 self.intMethodCalled = NO; |
| 90 self.intArgument = 0; |
| 91 self.objectMethodCalled = NO; |
| 92 self.objectArgument = nil; |
| 93 } |
| 94 |
| 95 - (void)methodWithInt:(int)arg { |
| 96 self.intMethodCalled = YES; |
| 97 self.intArgument = arg; |
| 98 } |
| 99 |
| 100 - (void)methodWithObject:(NSObject*)arg { |
| 101 self.objectMethodCalled = YES; |
| 102 self.objectArgument = arg; |
| 103 } |
| 104 |
| 105 - (int)methodToAddFirstArgument:(int)first toSecond:(int)second { |
| 106 return first + second; |
| 107 } |
| 108 |
| 109 @end |
| 110 |
| 111 #pragma mark - Tests |
| 112 |
| 113 // Tests handler methods with no arguments. |
| 114 TEST(CommandDispatcherTest, SimpleTarget) { |
| 115 id dispatcher = [[CommandDispatcher alloc] init]; |
| 116 CommandDispatcherTestSimpleTarget* target = |
| 117 [[CommandDispatcherTestSimpleTarget alloc] init]; |
| 118 |
| 119 [dispatcher registerTarget:target forSelector:@selector(show)]; |
| 120 [dispatcher registerTarget:target forSelector:@selector(hide)]; |
| 121 |
| 122 [dispatcher show]; |
| 123 EXPECT_TRUE(target.showCalled); |
| 124 EXPECT_FALSE(target.hideCalled); |
| 125 |
| 126 [target resetProperties]; |
| 127 [dispatcher hide]; |
| 128 EXPECT_FALSE(target.showCalled); |
| 129 EXPECT_TRUE(target.hideCalled); |
| 130 } |
| 131 |
| 132 // Tests handler methods that take arguments. |
| 133 TEST(CommandDispatcherTest, TargetWithArguments) { |
| 134 id dispatcher = [[CommandDispatcher alloc] init]; |
| 135 CommandDispatcherTestTargetWithArguments* target = |
| 136 [[CommandDispatcherTestTargetWithArguments alloc] init]; |
| 137 |
| 138 [dispatcher registerTarget:target forSelector:@selector(methodWithInt:)]; |
| 139 [dispatcher registerTarget:target forSelector:@selector(methodWithObject:)]; |
| 140 [dispatcher registerTarget:target |
| 141 forSelector:@selector(methodToAddFirstArgument:toSecond:)]; |
| 142 |
| 143 const int int_argument = 4; |
| 144 [dispatcher methodWithInt:int_argument]; |
| 145 EXPECT_TRUE(target.intMethodCalled); |
| 146 EXPECT_FALSE(target.objectMethodCalled); |
| 147 EXPECT_EQ(int_argument, target.intArgument); |
| 148 |
| 149 [target resetProperties]; |
| 150 NSObject* object_argument = [[NSObject alloc] init]; |
| 151 [dispatcher methodWithObject:object_argument]; |
| 152 EXPECT_FALSE(target.intMethodCalled); |
| 153 EXPECT_TRUE(target.objectMethodCalled); |
| 154 EXPECT_EQ(object_argument, target.objectArgument); |
| 155 |
| 156 [target resetProperties]; |
| 157 EXPECT_EQ(13, [dispatcher methodToAddFirstArgument:7 toSecond:6]); |
| 158 EXPECT_FALSE(target.intMethodCalled); |
| 159 EXPECT_FALSE(target.objectMethodCalled); |
| 160 EXPECT_EQ(42, [dispatcher methodToAddFirstArgument:34 toSecond:8]); |
| 161 } |
| 162 |
| 163 // Tests that messages are routed to the proper handler when multiple targets |
| 164 // are registered. |
| 165 TEST(CommandDispatcherTest, MultipleTargets) { |
| 166 id dispatcher = [[CommandDispatcher alloc] init]; |
| 167 CommandDispatcherTestSimpleTarget* showTarget = |
| 168 [[CommandDispatcherTestSimpleTarget alloc] init]; |
| 169 CommandDispatcherTestSimpleTarget* hideTarget = |
| 170 [[CommandDispatcherTestSimpleTarget alloc] init]; |
| 171 |
| 172 [dispatcher registerTarget:showTarget forSelector:@selector(show)]; |
| 173 [dispatcher registerTarget:hideTarget forSelector:@selector(hide)]; |
| 174 |
| 175 [dispatcher show]; |
| 176 EXPECT_TRUE(showTarget.showCalled); |
| 177 EXPECT_FALSE(hideTarget.showCalled); |
| 178 |
| 179 [showTarget resetProperties]; |
| 180 [dispatcher hide]; |
| 181 EXPECT_FALSE(showTarget.hideCalled); |
| 182 EXPECT_TRUE(hideTarget.hideCalled); |
| 183 } |
| 184 |
| 185 // Tests that handlers are no longer forwarded messages after deregistration. |
| 186 TEST(CommandDispatcherTest, Deregistration) { |
| 187 id dispatcher = [[CommandDispatcher alloc] init]; |
| 188 CommandDispatcherTestSimpleTarget* showTarget = |
| 189 [[CommandDispatcherTestSimpleTarget alloc] init]; |
| 190 CommandDispatcherTestSimpleTarget* hideTarget = |
| 191 [[CommandDispatcherTestSimpleTarget alloc] init]; |
| 192 |
| 193 [dispatcher registerTarget:showTarget forSelector:@selector(show)]; |
| 194 [dispatcher registerTarget:hideTarget forSelector:@selector(hide)]; |
| 195 |
| 196 [dispatcher show]; |
| 197 EXPECT_TRUE(showTarget.showCalled); |
| 198 EXPECT_FALSE(hideTarget.showCalled); |
| 199 |
| 200 [dispatcher stopDispatchingForTarget:showTarget]; |
| 201 bool exception_caught = false; |
| 202 @try { |
| 203 [dispatcher show]; |
| 204 } @catch (NSException* exception) { |
| 205 EXPECT_EQ(NSInvalidArgumentException, [exception name]); |
| 206 exception_caught = true; |
| 207 } |
| 208 EXPECT_TRUE(exception_caught); |
| 209 |
| 210 [dispatcher hide]; |
| 211 EXPECT_FALSE(showTarget.hideCalled); |
| 212 EXPECT_TRUE(hideTarget.hideCalled); |
| 213 } |
| 214 |
| 215 // Tests that an exception is thrown when there is no registered handler for a |
| 216 // given selector. |
| 217 TEST(CommandDispatcherTest, NoTargetRegisteredForSelector) { |
| 218 id dispatcher = [[CommandDispatcher alloc] init]; |
| 219 CommandDispatcherTestSimpleTarget* target = |
| 220 [[CommandDispatcherTestSimpleTarget alloc] init]; |
| 221 |
| 222 [dispatcher registerTarget:target forSelector:@selector(show)]; |
| 223 |
| 224 bool exception_caught = false; |
| 225 @try { |
| 226 [dispatcher hide]; |
| 227 } @catch (NSException* exception) { |
| 228 EXPECT_EQ(NSInvalidArgumentException, [exception name]); |
| 229 exception_caught = true; |
| 230 } |
| 231 |
| 232 EXPECT_TRUE(exception_caught); |
| 233 } |
OLD | NEW |