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 | |
lpromero
2017/03/07 10:04:17
Missing ARC header.
rohitrao (ping after 24h)
2017/03/09 14:41:12
Done.
| |
12 @interface CommandDispatcherTestSimpleTarget : NSObject | |
13 | |
14 @property(nonatomic, assign) BOOL showCalled; | |
15 @property(nonatomic, assign) BOOL hideCalled; | |
16 | |
17 - (void)resetProperties; | |
18 | |
19 - (void)show; | |
20 - (void)hide; | |
21 | |
22 @end | |
23 | |
24 @implementation CommandDispatcherTestSimpleTarget | |
25 | |
26 @synthesize showCalled = _showCalled; | |
27 @synthesize hideCalled = _hideCalled; | |
28 | |
29 - (void)resetProperties { | |
30 self.showCalled = NO; | |
31 self.hideCalled = NO; | |
32 } | |
33 | |
34 - (void)show { | |
35 self.showCalled = YES; | |
36 } | |
37 | |
38 - (void)hide { | |
39 self.hideCalled = YES; | |
40 } | |
41 | |
42 @end | |
43 | |
44 @interface CommandDispatcherTestTargetWithArguments : NSObject | |
45 | |
46 @property(nonatomic, assign) BOOL intMethodCalled; | |
47 @property(nonatomic, assign) int intArgument; | |
48 | |
49 @property(nonatomic, assign) BOOL objectMethodCalled; | |
50 @property(nonatomic, strong) NSObject* objectArgument; | |
51 | |
52 - (void)resetProperties; | |
53 | |
54 - (void)methodWithInt:(int)arg; | |
55 - (void)methodWithObject:(NSObject*)arg; | |
56 - (int)methodToAddFirstArgument:(int)first toSecond:(int)second; | |
57 | |
58 @end | |
59 | |
60 @implementation CommandDispatcherTestTargetWithArguments | |
61 | |
62 @synthesize intMethodCalled = _intMethodCalled; | |
63 @synthesize intArgument = _intArgument; | |
64 @synthesize objectMethodCalled = _objectMethodCalled; | |
65 @synthesize objectArgument = _objectArgument; | |
66 | |
67 - (void)resetProperties { | |
68 self.intMethodCalled = NO; | |
69 self.intArgument = 0; | |
70 self.objectMethodCalled = NO; | |
71 self.objectArgument = nil; | |
72 } | |
73 | |
74 - (void)methodWithInt:(int)arg { | |
75 self.intMethodCalled = YES; | |
76 self.intArgument = arg; | |
77 } | |
78 | |
79 - (void)methodWithObject:(NSObject*)arg { | |
80 self.objectMethodCalled = YES; | |
81 self.objectArgument = arg; | |
82 } | |
83 | |
84 - (int)methodToAddFirstArgument:(int)first toSecond:(int)second { | |
85 return first + second; | |
86 } | |
87 | |
88 @end | |
89 | |
90 TEST(CommandDispatcherTest, SimpleTarget) { | |
91 id dispatcher = [[CommandDispatcher alloc] init]; | |
92 CommandDispatcherTestSimpleTarget* target = | |
93 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
94 | |
95 [dispatcher registerTarget:target forSelector:@selector(show)]; | |
96 [dispatcher registerTarget:target forSelector:@selector(hide)]; | |
97 | |
98 [dispatcher show]; | |
99 EXPECT_TRUE(target.showCalled); | |
100 EXPECT_FALSE(target.hideCalled); | |
101 | |
102 [target resetProperties]; | |
103 [dispatcher hide]; | |
104 EXPECT_FALSE(target.showCalled); | |
105 EXPECT_TRUE(target.hideCalled); | |
106 } | |
107 | |
108 TEST(CommandDispatcherTest, TargetWithArguments) { | |
109 id dispatcher = [[CommandDispatcher alloc] init]; | |
110 CommandDispatcherTestTargetWithArguments* target = | |
111 [[CommandDispatcherTestTargetWithArguments alloc] init]; | |
112 | |
113 [dispatcher registerTarget:target forSelector:@selector(methodWithInt:)]; | |
114 [dispatcher registerTarget:target forSelector:@selector(methodWithObject:)]; | |
115 [dispatcher registerTarget:target | |
116 forSelector:@selector(methodToAddFirstArgument:toSecond:)]; | |
117 | |
118 const int int_argument = 4; | |
119 [dispatcher methodWithInt:int_argument]; | |
120 EXPECT_TRUE(target.intMethodCalled); | |
121 EXPECT_FALSE(target.objectMethodCalled); | |
122 EXPECT_EQ(int_argument, target.intArgument); | |
123 | |
124 [target resetProperties]; | |
125 NSObject* object_argument = [[NSObject alloc] init]; | |
126 [dispatcher methodWithObject:object_argument]; | |
127 EXPECT_FALSE(target.intMethodCalled); | |
128 EXPECT_TRUE(target.objectMethodCalled); | |
129 EXPECT_EQ(object_argument, target.objectArgument); | |
130 | |
131 [target resetProperties]; | |
132 EXPECT_EQ(13, [dispatcher methodToAddFirstArgument:7 toSecond:6]); | |
133 EXPECT_FALSE(target.intMethodCalled); | |
134 EXPECT_FALSE(target.objectMethodCalled); | |
135 EXPECT_EQ(42, [dispatcher methodToAddFirstArgument:34 toSecond:8]); | |
136 } | |
137 | |
138 TEST(CommandDispatcherTest, MultipleTargets) { | |
139 id dispatcher = [[CommandDispatcher alloc] init]; | |
140 CommandDispatcherTestSimpleTarget* showTarget = | |
141 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
142 CommandDispatcherTestSimpleTarget* hideTarget = | |
143 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
144 | |
145 [dispatcher registerTarget:showTarget forSelector:@selector(show)]; | |
146 [dispatcher registerTarget:hideTarget forSelector:@selector(hide)]; | |
147 | |
148 [dispatcher show]; | |
149 EXPECT_TRUE(showTarget.showCalled); | |
150 EXPECT_FALSE(hideTarget.showCalled); | |
151 | |
152 [showTarget resetProperties]; | |
153 [dispatcher hide]; | |
154 EXPECT_FALSE(showTarget.hideCalled); | |
155 EXPECT_TRUE(hideTarget.hideCalled); | |
156 } | |
157 | |
158 TEST(CommandDispatcherTest, Deregistration) { | |
159 id dispatcher = [[CommandDispatcher alloc] init]; | |
160 CommandDispatcherTestSimpleTarget* showTarget = | |
161 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
162 CommandDispatcherTestSimpleTarget* hideTarget = | |
163 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
164 | |
165 [dispatcher registerTarget:showTarget forSelector:@selector(show)]; | |
166 [dispatcher registerTarget:hideTarget forSelector:@selector(hide)]; | |
167 | |
168 [dispatcher show]; | |
169 EXPECT_TRUE(showTarget.showCalled); | |
170 EXPECT_FALSE(hideTarget.showCalled); | |
171 | |
172 [dispatcher stopDispatchingForTarget:showTarget]; | |
173 bool exception_caught = false; | |
174 @try { | |
175 [dispatcher show]; | |
176 } @catch (NSException* exception) { | |
177 EXPECT_EQ(NSInvalidArgumentException, [exception name]); | |
178 exception_caught = true; | |
179 } | |
180 EXPECT_TRUE(exception_caught); | |
181 | |
182 [dispatcher hide]; | |
183 EXPECT_FALSE(showTarget.hideCalled); | |
184 EXPECT_TRUE(hideTarget.hideCalled); | |
185 } | |
186 | |
187 TEST(CommandDispatcherTest, NoTargetRegisteredForSelector) { | |
188 id dispatcher = [[CommandDispatcher alloc] init]; | |
189 CommandDispatcherTestSimpleTarget* target = | |
190 [[CommandDispatcherTestSimpleTarget alloc] init]; | |
191 | |
192 [dispatcher registerTarget:target forSelector:@selector(show)]; | |
193 | |
194 bool exception_caught = false; | |
195 @try { | |
196 [dispatcher hide]; | |
197 } @catch (NSException* exception) { | |
198 EXPECT_EQ(NSInvalidArgumentException, [exception name]); | |
199 exception_caught = true; | |
200 } | |
201 | |
202 EXPECT_TRUE(exception_caught); | |
203 } | |
OLD | NEW |