1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "base/ios/crb_protocol_observers.h"
#include "base/notreached.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
@protocol TestObserver
@required
- (void)requiredMethod;
- (void)reset;
@optional
- (void)optionalMethod;
- (void)mutateByAddingObserver:(id<TestObserver>)observer;
- (void)mutateByRemovingObserver:(id<TestObserver>)observer;
- (void)nestedMutateByAddingObserver:(id<TestObserver>)observer;
- (void)nestedMutateByRemovingObserver:(id<TestObserver>)observer;
@end
// Implements only the required methods in the TestObserver protocol.
@interface TestPartialObserver : NSObject<TestObserver>
@property(nonatomic, readonly) BOOL requiredMethodInvoked;
@end
// Implements all the methods in the TestObserver protocol.
@interface TestCompleteObserver : TestPartialObserver<TestObserver>
@property(nonatomic, readonly) BOOL optionalMethodInvoked;
@end
@interface TestMutateObserver : TestCompleteObserver
- (instancetype)initWithObserver:(CRBProtocolObservers*)observer
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
@end
namespace {
class CRBProtocolObserversTest : public PlatformTest {
public:
CRBProtocolObserversTest() {}
protected:
void SetUp() override {
PlatformTest::SetUp();
observers_ = (CRBProtocolObservers<TestObserver>*)[CRBProtocolObservers
observersWithProtocol:@protocol(TestObserver)];
partial_observer_ = [[TestPartialObserver alloc] init];
EXPECT_FALSE([partial_observer_ requiredMethodInvoked]);
complete_observer_ = [[TestCompleteObserver alloc] init];
EXPECT_FALSE([complete_observer_ requiredMethodInvoked]);
EXPECT_FALSE([complete_observer_ optionalMethodInvoked]);
mutate_observer_ = [[TestMutateObserver alloc] initWithObserver:observers_];
EXPECT_FALSE([mutate_observer_ requiredMethodInvoked]);
}
CRBProtocolObservers<TestObserver>* observers_;
TestPartialObserver* partial_observer_;
TestCompleteObserver* complete_observer_;
TestMutateObserver* mutate_observer_;
};
// Verifies basic functionality of -[CRBProtocolObservers addObserver:] and
// -[CRBProtocolObservers removeObserver:].
TEST_F(CRBProtocolObserversTest, AddRemoveObserver) {
// Add an observer and verify that the CRBProtocolObservers instance forwards
// an invocation to it.
[observers_ addObserver:partial_observer_];
[observers_ requiredMethod];
EXPECT_TRUE([partial_observer_ requiredMethodInvoked]);
[partial_observer_ reset];
EXPECT_FALSE([partial_observer_ requiredMethodInvoked]);
// Remove the observer and verify that the CRBProtocolObservers instance no
// longer forwards an invocation to it.
[observers_ removeObserver:partial_observer_];
[observers_ requiredMethod];
EXPECT_FALSE([partial_observer_ requiredMethodInvoked]);
}
// Verifies that CRBProtocolObservers correctly forwards the invocation of a
// required method in the protocol.
TEST_F(CRBProtocolObserversTest, RequiredMethods) {
[observers_ addObserver:partial_observer_];
[observers_ addObserver:complete_observer_];
[observers_ requiredMethod];
EXPECT_TRUE([partial_observer_ requiredMethodInvoked]);
EXPECT_TRUE([complete_observer_ requiredMethodInvoked]);
}
// Verifies that CRBProtocolObservers correctly forwards the invocation of an
// optional method in the protocol.
TEST_F(CRBProtocolObserversTest, OptionalMethods) {
[observers_ addObserver:partial_observer_];
[observers_ addObserver:complete_observer_];
[observers_ optionalMethod];
EXPECT_FALSE([partial_observer_ requiredMethodInvoked]);
EXPECT_FALSE([complete_observer_ requiredMethodInvoked]);
EXPECT_TRUE([complete_observer_ optionalMethodInvoked]);
}
// Verifies that CRBProtocolObservers only holds a weak reference to an
// observer.
TEST_F(CRBProtocolObserversTest, WeakReference) {
__weak TestPartialObserver* weak_observer = partial_observer_;
EXPECT_TRUE(weak_observer);
[observers_ addObserver:partial_observer_];
// Need an autorelease pool here, because
// -[CRBProtocolObservers forwardInvocation:] creates a temporary
// autoreleased array that holds all the observers.
@autoreleasepool {
[observers_ requiredMethod];
EXPECT_TRUE([partial_observer_ requiredMethodInvoked]);
partial_observer_ = nil;
}
EXPECT_FALSE(weak_observer);
}
// Verifies that an observer can safely remove itself as observer while being
// notified.
TEST_F(CRBProtocolObserversTest, SelfMutateObservers) {
[observers_ addObserver:mutate_observer_];
EXPECT_FALSE([observers_ empty]);
[observers_ requiredMethod];
EXPECT_TRUE([mutate_observer_ requiredMethodInvoked]);
[mutate_observer_ reset];
[observers_ nestedMutateByRemovingObserver:mutate_observer_];
EXPECT_FALSE([mutate_observer_ requiredMethodInvoked]);
[observers_ addObserver:partial_observer_];
[observers_ requiredMethod];
EXPECT_FALSE([mutate_observer_ requiredMethodInvoked]);
EXPECT_TRUE([partial_observer_ requiredMethodInvoked]);
[observers_ removeObserver:partial_observer_];
EXPECT_TRUE([observers_ empty]);
}
// Verifies that - [CRBProtocolObservers addObserver:] and
// - [CRBProtocolObservers removeObserver:] can be called while methods are
// being forwarded.
TEST_F(CRBProtocolObserversTest, MutateObservers) {
// Indirectly add an observer while forwarding an observer method.
[observers_ addObserver:mutate_observer_];
[observers_ mutateByAddingObserver:partial_observer_];
EXPECT_FALSE([partial_observer_ requiredMethodInvoked]);
// Check that methods are correctly forwared to the indirectly added observer.
[mutate_observer_ reset];
[observers_ requiredMethod];
EXPECT_TRUE([mutate_observer_ requiredMethodInvoked]);
EXPECT_TRUE([partial_observer_ requiredMethodInvoked]);
[mutate_observer_ reset];
[partial_observer_ reset];
// Indirectly remove an observer while forwarding an observer method.
[observers_ mutateByRemovingObserver:partial_observer_];
// Check that method is not forwared to the indirectly removed observer.
[observers_ requiredMethod];
EXPECT_TRUE([mutate_observer_ requiredMethodInvoked]);
EXPECT_FALSE([partial_observer_ requiredMethodInvoked]);
}
// Verifies that - [CRBProtocolObservers addObserver:] and
// - [CRBProtocolObservers removeObserver:] can be called while methods are
// being forwarded with a nested invocation depth > 0.
TEST_F(CRBProtocolObserversTest, NestedMutateObservers) {
// Indirectly add an observer while forwarding an observer method.
[observers_ addObserver:mutate_observer_];
[observers_ nestedMutateByAddingObserver:partial_observer_];
EXPECT_FALSE([partial_observer_ requiredMethodInvoked]);
// Check that methods are correctly forwared to the indirectly added observer.
[mutate_observer_ reset];
[observers_ requiredMethod];
EXPECT_TRUE([mutate_observer_ requiredMethodInvoked]);
EXPECT_TRUE([partial_observer_ requiredMethodInvoked]);
[mutate_observer_ reset];
[partial_observer_ reset];
// Indirectly remove an observer while forwarding an observer method.
[observers_ nestedMutateByRemovingObserver:partial_observer_];
// Check that method is not forwared to the indirectly removed observer.
[observers_ requiredMethod];
EXPECT_TRUE([mutate_observer_ requiredMethodInvoked]);
EXPECT_FALSE([partial_observer_ requiredMethodInvoked]);
}
// Verifies that CRBProtocolObservers works if an observer deallocs.
TEST_F(CRBProtocolObserversTest, IgnoresDeallocedObservers) {
__weak TestPartialObserver* weak_observer = partial_observer_;
EXPECT_TRUE(weak_observer);
[observers_ addObserver:partial_observer_];
// Need an autorelease pool here, because
// -[CRBProtocolObservers forwardInvocation:] creates a temporary
// autoreleased array that holds all the observers.
@autoreleasepool {
[observers_ requiredMethod];
EXPECT_TRUE([partial_observer_ requiredMethodInvoked]);
partial_observer_ = nil;
}
EXPECT_FALSE(weak_observer);
// This shouldn't crash.
[observers_ requiredMethod];
}
} // namespace
@implementation TestPartialObserver {
BOOL _requiredMethodInvoked;
}
- (BOOL)requiredMethodInvoked {
return _requiredMethodInvoked;
}
- (void)requiredMethod {
_requiredMethodInvoked = YES;
}
- (void)reset {
_requiredMethodInvoked = NO;
}
@end
@implementation TestCompleteObserver {
BOOL _optionalMethodInvoked;
}
- (BOOL)optionalMethodInvoked {
return _optionalMethodInvoked;
}
- (void)optionalMethod {
_optionalMethodInvoked = YES;
}
- (void)reset {
[super reset];
_optionalMethodInvoked = NO;
}
@end
@implementation TestMutateObserver {
__weak id _observers;
}
- (instancetype)initWithObserver:(CRBProtocolObservers*)observers {
self = [super init];
if (self) {
_observers = observers;
}
return self;
}
- (instancetype)init {
NOTREACHED();
return nil;
}
- (void)mutateByAddingObserver:(id<TestObserver>)observer {
[_observers addObserver:observer];
}
- (void)mutateByRemovingObserver:(id<TestObserver>)observer {
[_observers removeObserver:observer];
}
- (void)nestedMutateByAddingObserver:(id<TestObserver>)observer {
[_observers mutateByAddingObserver:observer];
}
- (void)nestedMutateByRemovingObserver:(id<TestObserver>)observer {
[_observers mutateByRemovingObserver:observer];
}
@end
|