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
3 // found in the LICENSE file.
5 #import "ios/web/public/crw_browsing_data_store.h"
7 #include "base/ios/ios_util.h"
8 #include "base/logging.h"
9 #import "base/mac/scoped_nsobject.h"
10 #include "base/memory/scoped_ptr.h"
11 #import "base/test/ios/wait_util.h"
12 #include "ios/web/public/active_state_manager.h"
13 #include "ios/web/public/browser_state.h"
14 #include "ios/web/public/test/test_browser_state.h"
15 #include "ios/web/public/test/test_web_thread_bundle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
19 // An observer to observe the |mode| key changes to a CRWBrowsingDataStore.
20 // Used for testing purposes.
21 @interface CRWTestBrowsingDataStoreObserver : NSObject
22 // Designated init. |browsingDataStore| cannot be null.
23 - (instancetype)initWithBrowsingDataStore:
24 (CRWBrowsingDataStore*)browsingDataStore NS_DESIGNATED_INITIALIZER;
25 // The number of times that the mode of the underlying CRWBrowsingDataStore
27 @property(nonatomic, assign) NSUInteger modeChangeCount;
30 @implementation CRWTestBrowsingDataStoreObserver {
31 // The underlying CRWBrowsingDataStore.
32 __weak CRWBrowsingDataStore* _browsingDataStore;
35 @synthesize modeChangeCount = _modeChangeCount;
37 - (instancetype)initWithBrowsingDataStore:
38 (CRWBrowsingDataStore*)browsingDataStore {
41 DCHECK(browsingDataStore);
42 [browsingDataStore addObserver:self
46 _browsingDataStore = browsingDataStore;
51 - (void)observeValueForKeyPath:(NSString*)keyPath
53 change:(NSDictionary*)change
54 context:(void*)context {
55 DCHECK([keyPath isEqual:@"mode"]);
56 DCHECK_EQ(_browsingDataStore, object);
58 ++self.modeChangeCount;
62 [_browsingDataStore removeObserver:self forKeyPath:@"mode"];
71 class BrowsingDataStoreTest : public PlatformTest {
73 void SetUp() override {
74 PlatformTest::SetUp();
75 browser_state_.reset(new TestBrowserState());
76 BrowserState::GetActiveStateManager(browser_state_.get())->SetActive(true);
77 browsing_data_store_.reset([[CRWBrowsingDataStore alloc]
78 initWithBrowserState:browser_state_.get()]);
80 void TearDown() override {
81 // The BrowserState needs to be destroyed first so that it is outlived by
82 // the WebThreadBundle.
83 BrowserState::GetActiveStateManager(browser_state_.get())->SetActive(false);
84 browser_state_.reset();
85 PlatformTest::TearDown();
88 // Sets the mode of the |browsing_data_store_| to |ACTIVE| and blocks until
89 // the mode has actually been changed.
91 [browsing_data_store_ makeActiveWithCompletionHandler:^(BOOL success) {
94 base::test::ios::WaitUntilCondition(^bool() {
95 return [browsing_data_store_ mode] == ACTIVE;
99 // Sets the mode of the |browsing_data_store_| to |INACTIVE| and blocks until
100 // the mode has actually been changed.
101 void MakeInactive() {
102 [browsing_data_store_ makeInactiveWithCompletionHandler:^(BOOL success) {
105 base::test::ios::WaitUntilCondition(^bool() {
106 return [browsing_data_store_ mode] == INACTIVE;
110 // Removes browsing data of |browsingDataTypes| from the underlying
111 // CRWBrowsingDataStore and waits until the operation finished.
112 void RemoveDataOfTypes(web::BrowsingDataTypes browsing_data_types) {
113 __block BOOL block_was_called = NO;
114 [browsing_data_store_ removeDataOfTypes:browsing_data_types
116 block_was_called = YES;
118 base::test::ios::WaitUntilCondition(^bool() {
119 return block_was_called;
123 // The CRWBrowsingDataStore used for testing purposes.
124 base::scoped_nsobject<CRWBrowsingDataStore> browsing_data_store_;
127 // The WebThreadBundle used for testing purposes.
128 TestWebThreadBundle thread_bundle_;
129 // The BrowserState used for testing purposes.
130 scoped_ptr<BrowserState> browser_state_;
135 // Tests that a CRWBrowsingDataStore's initial mode is set correctly and that it
136 // has no pending operations.
137 TEST_F(BrowsingDataStoreTest, InitialModeAndNoPendingOperations) {
138 if (!base::ios::IsRunningOnIOS8OrLater()) {
142 EXPECT_EQ(ACTIVE, [browsing_data_store_ mode]);
143 EXPECT_FALSE([browsing_data_store_ hasPendingOperations]);
146 // Tests that CRWBrowsingDataStore handles several consecutive calls to
147 // |makeActive| and |makeInactive| correctly.
148 TEST_F(BrowsingDataStoreTest, MakeActiveAndInactiveOperations) {
149 if (!base::ios::IsRunningOnIOS8OrLater()) {
154 base::scoped_nsobject<CRWTestBrowsingDataStoreObserver> observer(
155 [[CRWTestBrowsingDataStoreObserver alloc]
156 initWithBrowsingDataStore:browsing_data_store_]);
157 EXPECT_EQ(0U, [observer modeChangeCount]);
159 id unsucessfullCallback = ^(BOOL success) {
160 ASSERT_TRUE([NSThread isMainThread]);
161 CRWBrowsingDataStoreMode mode = [browsing_data_store_ mode];
162 EXPECT_FALSE(success);
163 EXPECT_EQ(SYNCHRONIZING, mode);
165 [browsing_data_store_ makeActiveWithCompletionHandler:unsucessfullCallback];
166 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]);
167 EXPECT_EQ(1U, [observer modeChangeCount]);
169 [browsing_data_store_ makeInactiveWithCompletionHandler:unsucessfullCallback];
170 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]);
172 [browsing_data_store_ makeActiveWithCompletionHandler:unsucessfullCallback];
173 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]);
175 __block BOOL block_was_called = NO;
176 [browsing_data_store_ makeInactiveWithCompletionHandler:^(BOOL success) {
177 ASSERT_TRUE([NSThread isMainThread]);
178 CRWBrowsingDataStoreMode mode = [browsing_data_store_ mode];
179 EXPECT_TRUE(success);
180 EXPECT_EQ(INACTIVE, mode);
181 block_was_called = YES;
183 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]);
185 base::test::ios::WaitUntilCondition(^bool{
186 return block_was_called;
189 EXPECT_EQ(INACTIVE, [browsing_data_store_ mode]);
190 EXPECT_EQ(2U, [observer modeChangeCount]);
193 // Tests that CRWBrowsingDataStore correctly handles |removeDataOfTypes:|.
194 TEST_F(BrowsingDataStoreTest, RemoveDataOperations) {
195 if (!base::ios::IsRunningOnIOS8OrLater()) {
199 ASSERT_EQ(ACTIVE, [browsing_data_store_ mode]);
200 // |removeDataOfTypes| is called when the mode was ACTIVE.
201 RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES);
204 ASSERT_EQ(INACTIVE, [browsing_data_store_ mode]);
205 // |removeDataOfTypes| is called when the mode was INACTIVE.
206 RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES);
209 // Tests that CRWBrowsingDataStore correctly handles |removeDataOfTypes:| after
210 // a |makeActive| call.
211 TEST_F(BrowsingDataStoreTest, RemoveDataOperationAfterMakeActiveCall) {
212 if (!base::ios::IsRunningOnIOS8OrLater()) {
217 ASSERT_EQ(INACTIVE, [browsing_data_store_ mode]);
219 [browsing_data_store_ makeActiveWithCompletionHandler:nil];
220 // |removeDataOfTypes| is called immediately after a |makeActive| call.
221 RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES);
222 EXPECT_EQ(ACTIVE, [browsing_data_store_ mode]);
225 // Tests that CRWBrowsingDataStore correctly handles |removeDataOfTypes:| after
226 // a |makeActive| call.
227 TEST_F(BrowsingDataStoreTest, RemoveDataOperationAfterMakeInactiveCall) {
228 if (!base::ios::IsRunningOnIOS8OrLater()) {
232 ASSERT_EQ(ACTIVE, [browsing_data_store_ mode]);
234 [browsing_data_store_ makeInactiveWithCompletionHandler:nil];
235 // |removeDataOfTypes| is called immediately after a |makeInactive| call.
236 RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES);
237 EXPECT_EQ(INACTIVE, [browsing_data_store_ mode]);