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/logging.h" |
7 #import "base/mac/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #import "base/test/ios/wait_util.h" | 10 #import "base/test/ios/wait_util.h" |
10 #include "ios/web/public/active_state_manager.h" | 11 #include "ios/web/public/active_state_manager.h" |
11 #include "ios/web/public/browser_state.h" | 12 #include "ios/web/public/browser_state.h" |
12 #include "ios/web/public/test/test_browser_state.h" | 13 #include "ios/web/public/test/test_browser_state.h" |
13 #include "ios/web/public/test/test_web_thread_bundle.h" | 14 #include "ios/web/public/test/test_web_thread_bundle.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
16 | 17 |
| 18 // An observer to observe the |mode| key changes to a CRWBrowsingDataStore. |
| 19 // Used for testing purposes. |
| 20 @interface CRWTestBrowsingDataStoreObserver : NSObject |
| 21 // Designated init. |browsingDataStore| cannot be null. |
| 22 - (instancetype)initWithBrowsingDataStore: |
| 23 (CRWBrowsingDataStore*)browsingDataStore NS_DESIGNATED_INITIALIZER; |
| 24 // The number of times that the mode of the underlying CRWBrowsingDataStore |
| 25 // changed. |
| 26 @property(nonatomic, assign) NSUInteger modeChangeCount; |
| 27 @end |
| 28 |
| 29 @implementation CRWTestBrowsingDataStoreObserver { |
| 30 // The underlying CRWBrowsingDataStore. |
| 31 __weak CRWBrowsingDataStore* _browsingDataStore; |
| 32 } |
| 33 |
| 34 @synthesize modeChangeCount = _modeChangeCount; |
| 35 |
| 36 - (instancetype)initWithBrowsingDataStore: |
| 37 (CRWBrowsingDataStore*)browsingDataStore { |
| 38 self = [super init]; |
| 39 if (self) { |
| 40 DCHECK(browsingDataStore); |
| 41 [browsingDataStore addObserver:self |
| 42 forKeyPath:@"mode" |
| 43 options:0 |
| 44 context:nil]; |
| 45 _browsingDataStore = browsingDataStore; |
| 46 } |
| 47 return self; |
| 48 } |
| 49 |
| 50 - (void)observeValueForKeyPath:(NSString*)keyPath |
| 51 ofObject:(id)object |
| 52 change:(NSDictionary*)change |
| 53 context:(void*)context { |
| 54 DCHECK([keyPath isEqual:@"mode"]); |
| 55 DCHECK_EQ(_browsingDataStore, object); |
| 56 |
| 57 ++self.modeChangeCount; |
| 58 } |
| 59 |
| 60 - (void)dealloc { |
| 61 [_browsingDataStore removeObserver:self forKeyPath:@"mode"]; |
| 62 [super dealloc]; |
| 63 } |
| 64 |
| 65 @end |
| 66 |
17 namespace web { | 67 namespace web { |
18 namespace { | 68 namespace { |
19 | 69 |
20 class BrowsingDataStoreTest : public PlatformTest { | 70 class BrowsingDataStoreTest : public PlatformTest { |
21 protected: | 71 protected: |
22 void SetUp() override { | 72 void SetUp() override { |
23 PlatformTest::SetUp(); | 73 PlatformTest::SetUp(); |
24 browser_state_.reset(new TestBrowserState()); | 74 browser_state_.reset(new TestBrowserState()); |
25 BrowserState::GetActiveStateManager(browser_state_.get())->SetActive(true); | 75 BrowserState::GetActiveStateManager(browser_state_.get())->SetActive(true); |
26 browsing_data_store_.reset([[CRWBrowsingDataStore alloc] | 76 browsing_data_store_.reset([[CRWBrowsingDataStore alloc] |
(...skipping 22 matching lines...) Expand all Loading... |
49 // Tests that a CRWBrowsingDataStore's initial mode is set correctly and that it | 99 // Tests that a CRWBrowsingDataStore's initial mode is set correctly and that it |
50 // has no pending operations. | 100 // has no pending operations. |
51 TEST_F(BrowsingDataStoreTest, InitialModeAndNoPendingOperations) { | 101 TEST_F(BrowsingDataStoreTest, InitialModeAndNoPendingOperations) { |
52 EXPECT_EQ(ACTIVE, [browsing_data_store_ mode]); | 102 EXPECT_EQ(ACTIVE, [browsing_data_store_ mode]); |
53 EXPECT_FALSE([browsing_data_store_ hasPendingOperations]); | 103 EXPECT_FALSE([browsing_data_store_ hasPendingOperations]); |
54 } | 104 } |
55 | 105 |
56 // Tests that CRWBrowsingDataStore handles several consecutive calls to | 106 // Tests that CRWBrowsingDataStore handles several consecutive calls to |
57 // |makeActive| and |makeInactive| correctly. | 107 // |makeActive| and |makeInactive| correctly. |
58 TEST_F(BrowsingDataStoreTest, MakeActiveAndInactiveOperations) { | 108 TEST_F(BrowsingDataStoreTest, MakeActiveAndInactiveOperations) { |
59 ProceduralBlock makeActiveCallback = ^{ | 109 base::scoped_nsobject<CRWTestBrowsingDataStoreObserver> observer( |
| 110 [[CRWTestBrowsingDataStoreObserver alloc] |
| 111 initWithBrowsingDataStore:browsing_data_store_]); |
| 112 EXPECT_EQ(0U, [observer modeChangeCount]); |
| 113 |
| 114 id unsucessfullCallback = ^(BOOL success) { |
60 ASSERT_TRUE([NSThread isMainThread]); | 115 ASSERT_TRUE([NSThread isMainThread]); |
61 CRWBrowsingDataStoreMode mode = [browsing_data_store_ mode]; | 116 CRWBrowsingDataStoreMode mode = [browsing_data_store_ mode]; |
62 EXPECT_TRUE((mode == ACTIVE) || (mode == SYNCHRONIZING)); | 117 EXPECT_FALSE(success); |
| 118 EXPECT_EQ(SYNCHRONIZING, mode); |
63 }; | 119 }; |
64 ProceduralBlock makeInactiveCallback = ^{ | 120 [browsing_data_store_ makeActiveWithCompletionHandler:unsucessfullCallback]; |
65 ASSERT_TRUE([NSThread isMainThread]); | 121 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]); |
66 CRWBrowsingDataStoreMode mode = [browsing_data_store_ mode]; | 122 EXPECT_EQ(1U, [observer modeChangeCount]); |
67 EXPECT_TRUE((mode == INACTIVE) || (mode == SYNCHRONIZING)); | 123 |
68 }; | 124 [browsing_data_store_ makeInactiveWithCompletionHandler:unsucessfullCallback]; |
69 [browsing_data_store_ makeActiveWithCompletionHandler:makeActiveCallback]; | |
70 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]); | 125 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]); |
71 | 126 |
72 [browsing_data_store_ makeInactiveWithCompletionHandler:makeInactiveCallback]; | 127 [browsing_data_store_ makeActiveWithCompletionHandler:unsucessfullCallback]; |
73 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]); | |
74 | |
75 [browsing_data_store_ makeActiveWithCompletionHandler:makeActiveCallback]; | |
76 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]); | 128 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]); |
77 | 129 |
78 __block BOOL block_was_called = NO; | 130 __block BOOL block_was_called = NO; |
79 [browsing_data_store_ makeInactiveWithCompletionHandler:^{ | 131 [browsing_data_store_ makeInactiveWithCompletionHandler:^(BOOL success) { |
80 makeInactiveCallback(); | 132 ASSERT_TRUE([NSThread isMainThread]); |
| 133 CRWBrowsingDataStoreMode mode = [browsing_data_store_ mode]; |
| 134 EXPECT_TRUE(success); |
| 135 EXPECT_EQ(INACTIVE, mode); |
81 block_was_called = YES; | 136 block_was_called = YES; |
82 }]; | 137 }]; |
83 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]); | 138 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]); |
84 | 139 |
85 base::test::ios::WaitUntilCondition(^bool{ | 140 base::test::ios::WaitUntilCondition(^bool{ |
86 return block_was_called; | 141 return block_was_called; |
87 }); | 142 }); |
| 143 |
| 144 EXPECT_EQ(INACTIVE, [browsing_data_store_ mode]); |
| 145 EXPECT_EQ(2U, [observer modeChangeCount]); |
88 } | 146 } |
89 | 147 |
90 // Tests that CRWBrowsingDataStore correctly handles |removeDataOfTypes:| call. | 148 // Tests that CRWBrowsingDataStore correctly handles |removeDataOfTypes:| call. |
91 TEST_F(BrowsingDataStoreTest, RemoveDataOperations) { | 149 TEST_F(BrowsingDataStoreTest, RemoveDataOperations) { |
92 web::BrowsingDataTypes browsing_data_types = web::BROWSING_DATA_TYPE_COOKIES; | 150 web::BrowsingDataTypes browsing_data_types = web::BROWSING_DATA_TYPE_COOKIES; |
93 __block BOOL block_was_called = NO; | 151 __block BOOL block_was_called = NO; |
94 [browsing_data_store_ removeDataOfTypes:browsing_data_types | 152 [browsing_data_store_ removeDataOfTypes:browsing_data_types |
95 completionHandler:^{ | 153 completionHandler:^{ |
96 DCHECK([NSThread isMainThread]); | 154 DCHECK([NSThread isMainThread]); |
97 block_was_called = YES; | 155 block_was_called = YES; |
98 }]; | 156 }]; |
99 base::test::ios::WaitUntilCondition(^bool() { | 157 base::test::ios::WaitUntilCondition(^bool() { |
100 return block_was_called; | 158 return block_was_called; |
101 }); | 159 }); |
102 } | 160 } |
103 | 161 |
104 } // namespace web | 162 } // namespace web |
OLD | NEW |