Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/ios/crb_protocol_observers.h" | 5 #import "base/ios/crb_protocol_observers.h" |
| 6 #include "base/ios/weak_nsobject.h" | 6 #include "base/ios/weak_nsobject.h" |
| 7 #include "base/mac/scoped_nsautorelease_pool.h" | 7 #include "base/mac/scoped_nsautorelease_pool.h" |
| 8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "testing/gtest_mac.h" | 10 #include "testing/gtest_mac.h" |
| 11 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
| 12 | 12 |
| 13 @protocol TestObserver | 13 @protocol TestObserver |
| 14 | 14 |
| 15 @required | 15 @required |
| 16 - (void)requiredMethod; | 16 - (void)requiredMethod; |
| 17 - (void)reset; | 17 - (void)reset; |
| 18 | 18 |
| 19 @optional | 19 @optional |
| 20 - (void)optionalMethod; | 20 - (void)optionalMethod; |
| 21 - (void)mutateByAddingObserver:(id<TestObserver>)observer; | |
| 22 - (void)mutateByRemovingObserver:(id<TestObserver>)observer; | |
| 21 | 23 |
| 22 @end | 24 @end |
| 23 | 25 |
| 24 // Implements only the required methods in the TestObserver protocol. | 26 // Implements only the required methods in the TestObserver protocol. |
| 25 @interface TestPartialObserver : NSObject<TestObserver> | 27 @interface TestPartialObserver : NSObject<TestObserver> |
| 26 @property(nonatomic, readonly) BOOL requiredMethodInvoked; | 28 @property(nonatomic, readonly) BOOL requiredMethodInvoked; |
| 27 @end | 29 @end |
| 28 | 30 |
| 29 // Implements all the methods in the TestObserver protocol. | 31 // Implements all the methods in the TestObserver protocol. |
| 30 @interface TestCompleteObserver : TestPartialObserver<TestObserver> | 32 @interface TestCompleteObserver : TestPartialObserver<TestObserver> |
| 31 @property(nonatomic, readonly) BOOL optionalMethodInvoked; | 33 @property(nonatomic, readonly) BOOL optionalMethodInvoked; |
| 32 @end | 34 @end |
| 33 | 35 |
| 36 @interface TestMutateObserver : TestCompleteObserver | |
| 37 | |
| 38 - (instancetype)initWithObserver:(CRBProtocolObservers*)observer | |
| 39 NS_DESIGNATED_INITIALIZER; | |
| 40 | |
| 41 @end | |
| 42 | |
| 34 namespace { | 43 namespace { |
| 35 | 44 |
| 36 class CRBProtocolObserversTest : public PlatformTest { | 45 class CRBProtocolObserversTest : public PlatformTest { |
| 37 public: | 46 public: |
| 38 CRBProtocolObserversTest() {} | 47 CRBProtocolObserversTest() {} |
| 39 | 48 |
| 40 protected: | 49 protected: |
| 41 void SetUp() override { | 50 void SetUp() override { |
| 42 PlatformTest::SetUp(); | 51 PlatformTest::SetUp(); |
| 43 | 52 |
| 44 observers_.reset([[CRBProtocolObservers observersWithProtocol: | 53 observers_.reset([[CRBProtocolObservers observersWithProtocol: |
| 45 @protocol(TestObserver)] retain]); | 54 @protocol(TestObserver)] retain]); |
| 46 | 55 |
| 47 partial_observer_.reset([[TestPartialObserver alloc] init]); | 56 partial_observer_.reset([[TestPartialObserver alloc] init]); |
| 48 EXPECT_FALSE([partial_observer_ requiredMethodInvoked]); | 57 EXPECT_FALSE([partial_observer_ requiredMethodInvoked]); |
| 49 | 58 |
| 50 complete_observer_.reset([[TestCompleteObserver alloc] init]); | 59 complete_observer_.reset([[TestCompleteObserver alloc] init]); |
| 51 EXPECT_FALSE([complete_observer_ requiredMethodInvoked]); | 60 EXPECT_FALSE([complete_observer_ requiredMethodInvoked]); |
| 52 EXPECT_FALSE([complete_observer_ optionalMethodInvoked]); | 61 EXPECT_FALSE([complete_observer_ optionalMethodInvoked]); |
| 62 | |
| 63 mutate_observer_.reset( | |
| 64 [[TestMutateObserver alloc] initWithObserver:observers_.get()]); | |
| 65 EXPECT_FALSE([mutate_observer_ requiredMethodInvoked]); | |
| 53 } | 66 } |
| 54 | 67 |
| 55 base::scoped_nsobject<id> observers_; | 68 base::scoped_nsobject<id> observers_; |
| 56 base::scoped_nsobject<TestPartialObserver> partial_observer_; | 69 base::scoped_nsobject<TestPartialObserver> partial_observer_; |
| 57 base::scoped_nsobject<TestCompleteObserver> complete_observer_; | 70 base::scoped_nsobject<TestCompleteObserver> complete_observer_; |
| 71 base::scoped_nsobject<TestMutateObserver> mutate_observer_; | |
| 58 }; | 72 }; |
| 59 | 73 |
| 60 // Verifies basic functionality of -[CRBProtocolObservers addObserver:] and | 74 // Verifies basic functionality of -[CRBProtocolObservers addObserver:] and |
| 61 // -[CRBProtocolObservers removeObserver:]. | 75 // -[CRBProtocolObservers removeObserver:]. |
| 62 TEST_F(CRBProtocolObserversTest, AddRemoveObserver) { | 76 TEST_F(CRBProtocolObserversTest, AddRemoveObserver) { |
| 63 // Add an observer and verify that the CRBProtocolObservers instance forwards | 77 // Add an observer and verify that the CRBProtocolObservers instance forwards |
| 64 // an invocation to it. | 78 // an invocation to it. |
| 65 [observers_ addObserver:partial_observer_]; | 79 [observers_ addObserver:partial_observer_]; |
| 66 [observers_ requiredMethod]; | 80 [observers_ requiredMethod]; |
| 67 EXPECT_TRUE([partial_observer_ requiredMethodInvoked]); | 81 EXPECT_TRUE([partial_observer_ requiredMethodInvoked]); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 // autoreleased array that holds all the observers. | 126 // autoreleased array that holds all the observers. |
| 113 base::mac::ScopedNSAutoreleasePool pool; | 127 base::mac::ScopedNSAutoreleasePool pool; |
| 114 [observers_ requiredMethod]; | 128 [observers_ requiredMethod]; |
| 115 EXPECT_TRUE([partial_observer_ requiredMethodInvoked]); | 129 EXPECT_TRUE([partial_observer_ requiredMethodInvoked]); |
| 116 } | 130 } |
| 117 | 131 |
| 118 partial_observer_.reset(); | 132 partial_observer_.reset(); |
| 119 EXPECT_FALSE(weak_observer.get()); | 133 EXPECT_FALSE(weak_observer.get()); |
| 120 } | 134 } |
| 121 | 135 |
| 136 // Verifies that - [CRBProtocolObservers addObserver:] and | |
| 137 // - [CRBProtocolObservers removeObserver:] can be called while methods are | |
| 138 // being forwarded. | |
| 139 TEST_F(CRBProtocolObserversTest, MutateObservers) { | |
| 140 // Indirectly add an observer while forwarding an observer method. | |
| 141 [observers_ addObserver:mutate_observer_]; | |
| 142 | |
| 143 [observers_ mutateByAddingObserver:partial_observer_]; | |
| 144 EXPECT_FALSE([partial_observer_ requiredMethodInvoked]); | |
| 145 | |
| 146 // Check that methods are correctly forwared to the indirectly added observer. | |
| 147 [mutate_observer_ reset]; | |
| 148 [observers_ requiredMethod]; | |
| 149 EXPECT_TRUE([mutate_observer_ requiredMethodInvoked]); | |
| 150 EXPECT_TRUE([partial_observer_ requiredMethodInvoked]); | |
| 151 | |
| 152 [mutate_observer_ reset]; | |
| 153 [partial_observer_ reset]; | |
| 154 | |
| 155 // Indirectly remove an observer while forwarding an observer method. | |
| 156 [observers_ mutateByRemovingObserver:partial_observer_]; | |
| 157 | |
| 158 // Check that method is not forwared to the indirectly removed observer. | |
| 159 [observers_ requiredMethod]; | |
| 160 EXPECT_TRUE([mutate_observer_ requiredMethodInvoked]); | |
| 161 EXPECT_FALSE([partial_observer_ requiredMethodInvoked]); | |
| 162 } | |
| 163 | |
|
droger
2015/06/04 10:04:23
We should also probably test the case where _invoc
jbbegue
2015/06/04 12:13:03
I added a test for the nested case.
I also added a
| |
| 122 } // namespace | 164 } // namespace |
| 123 | 165 |
| 124 @implementation TestPartialObserver { | 166 @implementation TestPartialObserver { |
| 125 BOOL _requiredMethodInvoked; | 167 BOOL _requiredMethodInvoked; |
| 126 } | 168 } |
| 127 | 169 |
| 128 - (BOOL)requiredMethodInvoked { | 170 - (BOOL)requiredMethodInvoked { |
| 129 return _requiredMethodInvoked; | 171 return _requiredMethodInvoked; |
| 130 } | 172 } |
| 131 | 173 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 150 - (void)optionalMethod { | 192 - (void)optionalMethod { |
| 151 _optionalMethodInvoked = YES; | 193 _optionalMethodInvoked = YES; |
| 152 } | 194 } |
| 153 | 195 |
| 154 - (void)reset { | 196 - (void)reset { |
| 155 [super reset]; | 197 [super reset]; |
| 156 _optionalMethodInvoked = NO; | 198 _optionalMethodInvoked = NO; |
| 157 } | 199 } |
| 158 | 200 |
| 159 @end | 201 @end |
| 202 | |
| 203 @implementation TestMutateObserver { | |
| 204 __weak CRBProtocolObservers* _observers; | |
| 205 } | |
| 206 | |
| 207 - (instancetype)initWithObserver:(CRBProtocolObservers*)observers { | |
| 208 self = [super init]; | |
| 209 if (self) { | |
| 210 _observers = observers; | |
| 211 } | |
| 212 return self; | |
| 213 } | |
| 214 | |
| 215 - (void)mutateByAddingObserver:(id<TestObserver>)observer { | |
| 216 [_observers addObserver:observer]; | |
| 217 } | |
| 218 | |
| 219 - (void)mutateByRemovingObserver:(id<TestObserver>)observer { | |
| 220 [_observers removeObserver:observer]; | |
| 221 } | |
| 222 | |
| 223 @end | |
| OLD | NEW |