OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/web/public/crw_browsing_data_store.h" | 5 #import "ios/web/public/crw_browsing_data_store.h" |
6 | 6 |
7 #include "base/ios/ios_util.h" | 7 #include "base/ios/ios_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #import "base/test/ios/wait_util.h" | 11 #import "base/test/ios/wait_util.h" |
12 #include "ios/web/public/active_state_manager.h" | 12 #include "ios/web/public/active_state_manager.h" |
13 #include "ios/web/public/browser_state.h" | 13 #include "ios/web/public/browser_state.h" |
14 #include "ios/web/public/test/test_browser_state.h" | 14 #include "ios/web/public/test/test_browser_state.h" |
15 #include "ios/web/public/test/test_web_thread_bundle.h" | 15 #include "ios/web/public/test/test_web_thread_bundle.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
18 | 18 |
19 // An observer to observe the |mode| key changes to a CRWBrowsingDataStore. | 19 // An observer to observe the |mode| key changes to a CRWBrowsingDataStore. |
20 // Used for testing purposes. | 20 // Used for testing purposes. |
21 @interface CRWTestBrowsingDataStoreObserver : NSObject | 21 @interface CRWTestBrowsingDataStoreObserver : NSObject |
22 // Designated init. |browsingDataStore| cannot be null. | |
23 - (instancetype)initWithBrowsingDataStore: | |
24 (CRWBrowsingDataStore*)browsingDataStore NS_DESIGNATED_INITIALIZER; | |
25 - (instancetype)init NS_UNAVAILABLE; | |
26 // The number of times that the mode of the underlying CRWBrowsingDataStore | 22 // The number of times that the mode of the underlying CRWBrowsingDataStore |
27 // changed. | 23 // changed. |
28 @property(nonatomic, assign) NSUInteger modeChangeCount; | 24 @property(nonatomic, assign) NSUInteger modeChangeCount; |
| 25 |
| 26 // |browsingDataStore| cannot be null. |
| 27 - (instancetype)initWithBrowsingDataStore: |
| 28 (CRWBrowsingDataStore*)browsingDataStore NS_DESIGNATED_INITIALIZER; |
| 29 - (instancetype)init NS_UNAVAILABLE; |
29 @end | 30 @end |
30 | 31 |
31 @implementation CRWTestBrowsingDataStoreObserver { | 32 @implementation CRWTestBrowsingDataStoreObserver { |
32 // The underlying CRWBrowsingDataStore. | 33 // The underlying CRWBrowsingDataStore. |
33 __weak CRWBrowsingDataStore* _browsingDataStore; | 34 __weak CRWBrowsingDataStore* _browsingDataStore; |
34 } | 35 } |
35 | 36 |
36 @synthesize modeChangeCount = _modeChangeCount; | 37 @synthesize modeChangeCount = _modeChangeCount; |
37 | 38 |
38 - (instancetype)initWithBrowsingDataStore: | 39 - (instancetype)initWithBrowsingDataStore: |
39 (CRWBrowsingDataStore*)browsingDataStore { | 40 (CRWBrowsingDataStore*)browsingDataStore { |
40 self = [super init]; | 41 self = [super init]; |
41 if (self) { | 42 if (self) { |
42 DCHECK(browsingDataStore); | 43 DCHECK(browsingDataStore); |
43 [browsingDataStore addObserver:self | 44 [browsingDataStore addObserver:self |
44 forKeyPath:@"mode" | 45 forKeyPath:@"mode" |
45 options:0 | 46 options:0 |
46 context:nil]; | 47 context:nil]; |
47 _browsingDataStore = browsingDataStore; | 48 _browsingDataStore = browsingDataStore; |
48 } | 49 } |
49 return self; | 50 return self; |
50 } | 51 } |
51 | 52 |
52 - (instancetype)init { | 53 - (instancetype)init { |
53 NOTREACHED(); | 54 NOTREACHED(); |
54 return nil; | 55 return nil; |
55 } | 56 } |
56 | 57 |
| 58 - (void)dealloc { |
| 59 [_browsingDataStore removeObserver:self forKeyPath:@"mode"]; |
| 60 [super dealloc]; |
| 61 } |
| 62 |
57 - (void)observeValueForKeyPath:(NSString*)keyPath | 63 - (void)observeValueForKeyPath:(NSString*)keyPath |
58 ofObject:(id)object | 64 ofObject:(id)object |
59 change:(NSDictionary*)change | 65 change:(NSDictionary*)change |
60 context:(void*)context { | 66 context:(void*)context { |
61 DCHECK([keyPath isEqual:@"mode"]); | 67 DCHECK([keyPath isEqual:@"mode"]); |
62 DCHECK_EQ(_browsingDataStore, object); | 68 DCHECK_EQ(_browsingDataStore, object); |
63 | 69 |
64 ++self.modeChangeCount; | 70 ++self.modeChangeCount; |
65 } | 71 } |
66 | 72 |
67 - (void)dealloc { | |
68 [_browsingDataStore removeObserver:self forKeyPath:@"mode"]; | |
69 [super dealloc]; | |
70 } | |
71 | |
72 @end | 73 @end |
73 | 74 |
74 namespace web { | 75 namespace web { |
75 namespace { | 76 namespace { |
76 | 77 |
77 class BrowsingDataStoreTest : public PlatformTest { | 78 class BrowsingDataStoreTest : public PlatformTest { |
78 protected: | 79 protected: |
79 void SetUp() override { | 80 void SetUp() override { |
80 PlatformTest::SetUp(); | 81 PlatformTest::SetUp(); |
81 browser_state_.reset(new TestBrowserState()); | 82 browser_state_.reset(new TestBrowserState()); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 251 |
251 ASSERT_EQ(ACTIVE, [browsing_data_store_ mode]); | 252 ASSERT_EQ(ACTIVE, [browsing_data_store_ mode]); |
252 | 253 |
253 [browsing_data_store_ makeInactiveWithCompletionHandler:nil]; | 254 [browsing_data_store_ makeInactiveWithCompletionHandler:nil]; |
254 // |removeDataOfTypes| is called immediately after a |makeInactive| call. | 255 // |removeDataOfTypes| is called immediately after a |makeInactive| call. |
255 RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES); | 256 RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES); |
256 EXPECT_EQ(INACTIVE, [browsing_data_store_ mode]); | 257 EXPECT_EQ(INACTIVE, [browsing_data_store_ mode]); |
257 } | 258 } |
258 | 259 |
259 } // namespace web | 260 } // namespace web |
OLD | NEW |