| 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 startDispatchingToTarget:target forSelector:@selector(show)]; | |
| 120 [dispatcher startDispatchingToTarget: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 startDispatchingToTarget:target | |
| 139 forSelector:@selector(methodWithInt:)]; | |
| 140 [dispatcher startDispatchingToTarget:target | |
| 141 forSelector:@selector(methodWithObject:)]; | |
| 142 [dispatcher | |
| 143 startDispatchingToTarget:target | |
| 144 forSelector:@selector(methodToAddFirstArgument:toSecond:)]; | |
| 145 | |
| 146 const int int_argument = 4; | |
| 147 [dispatcher methodWithInt:int_argument]; | |
| 148 EXPECT_TRUE(target.intMethodCalled); | |
| 149 EXPECT_FALSE(target.objectMethodCalled); | |
| 150 EXPECT_EQ(int_argument, target.intArgument); | |
| 151 | |
| 152 [target resetProperties]; | |
| 153 NSObject* object_argument = [[NSObject alloc] init]; | |
| 154 [dispatcher methodWithObject:object_argument]; | |
| 155 EXPECT_FALSE(target.intMethodCalled); | |
| 156 EXPECT_TRUE(target.objectMethodCalled); | |
| 157 EXPECT_EQ(object_argument, target.objectArgument); | |
| 158 | |
| 159 [target resetProperties]; | |
| 160 EXPECT_EQ(13, [dispatcher methodToAddFirstArgument:7 toSecond:6]); | |
| 161 EXPECT_FALSE(target.intMethodCalled); | |
| 162 EXPECT_FALSE(target.objectMethodCalled); | |
| 163 EXPECT_EQ(42, [dispatcher methodToAddFirstArgument:34 toSecond:8]); | |
| 164 } | |
| 165 | |
| 166 // Tests that messages are routed to the proper handler when multiple targets | |
| 167 // are registered. | |
| 168 TEST(CommandDispatcherTest, MultipleTargets) { | |
| 169 id dispatcher = [[CommandDispatcher alloc] init]; | |
| 170 CommandDispatcherTestSimpleTarget* showTarget = | |
| 171 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
| 172 CommandDispatcherTestSimpleTarget* hideTarget = | |
| 173 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
| 174 | |
| 175 [dispatcher startDispatchingToTarget:showTarget forSelector:@selector(show)]; | |
| 176 [dispatcher startDispatchingToTarget:hideTarget forSelector:@selector(hide)]; | |
| 177 | |
| 178 [dispatcher show]; | |
| 179 EXPECT_TRUE(showTarget.showCalled); | |
| 180 EXPECT_FALSE(hideTarget.showCalled); | |
| 181 | |
| 182 [showTarget resetProperties]; | |
| 183 [dispatcher hide]; | |
| 184 EXPECT_FALSE(showTarget.hideCalled); | |
| 185 EXPECT_TRUE(hideTarget.hideCalled); | |
| 186 } | |
| 187 | |
| 188 // Tests that handlers are no longer forwarded messages after deregistration. | |
| 189 TEST(CommandDispatcherTest, Deregistration) { | |
| 190 id dispatcher = [[CommandDispatcher alloc] init]; | |
| 191 CommandDispatcherTestSimpleTarget* showTarget = | |
| 192 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
| 193 CommandDispatcherTestSimpleTarget* hideTarget = | |
| 194 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
| 195 | |
| 196 [dispatcher startDispatchingToTarget:showTarget forSelector:@selector(show)]; | |
| 197 [dispatcher startDispatchingToTarget:hideTarget forSelector:@selector(hide)]; | |
| 198 | |
| 199 [dispatcher show]; | |
| 200 EXPECT_TRUE(showTarget.showCalled); | |
| 201 EXPECT_FALSE(hideTarget.showCalled); | |
| 202 | |
| 203 [dispatcher stopDispatchingToTarget:showTarget]; | |
| 204 bool exception_caught = false; | |
| 205 @try { | |
| 206 [dispatcher show]; | |
| 207 } @catch (NSException* exception) { | |
| 208 EXPECT_EQ(NSInvalidArgumentException, [exception name]); | |
| 209 exception_caught = true; | |
| 210 } | |
| 211 EXPECT_TRUE(exception_caught); | |
| 212 | |
| 213 [dispatcher hide]; | |
| 214 EXPECT_FALSE(showTarget.hideCalled); | |
| 215 EXPECT_TRUE(hideTarget.hideCalled); | |
| 216 } | |
| 217 | |
| 218 // Tests that an exception is thrown when there is no registered handler for a | |
| 219 // given selector. | |
| 220 TEST(CommandDispatcherTest, NoTargetRegisteredForSelector) { | |
| 221 id dispatcher = [[CommandDispatcher alloc] init]; | |
| 222 CommandDispatcherTestSimpleTarget* target = | |
| 223 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
| 224 | |
| 225 [dispatcher startDispatchingToTarget:target forSelector:@selector(show)]; | |
| 226 | |
| 227 bool exception_caught = false; | |
| 228 @try { | |
| 229 [dispatcher hide]; | |
| 230 } @catch (NSException* exception) { | |
| 231 EXPECT_EQ(NSInvalidArgumentException, [exception name]); | |
| 232 exception_caught = true; | |
| 233 } | |
| 234 | |
| 235 EXPECT_TRUE(exception_caught); | |
| 236 } | |
| OLD | NEW |