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
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/public/crw_browsing_data_store.h"
#include "base/ios/ios_util.h"
#include "base/logging.h"
#import "base/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#import "base/test/ios/wait_util.h"
#include "ios/web/public/active_state_manager.h"
#include "ios/web/public/browser_state.h"
#include "ios/web/public/test/test_browser_state.h"
#include "ios/web/public/test/test_web_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
// An observer to observe the |mode| key changes to a CRWBrowsingDataStore.
// Used for testing purposes.
@interface CRWTestBrowsingDataStoreObserver : NSObject
// Designated init. |browsingDataStore| cannot be null.
- (instancetype)initWithBrowsingDataStore:
(CRWBrowsingDataStore*)browsingDataStore NS_DESIGNATED_INITIALIZER;
// The number of times that the mode of the underlying CRWBrowsingDataStore
// changed.
@property(nonatomic, assign) NSUInteger modeChangeCount;
@end
@implementation CRWTestBrowsingDataStoreObserver {
// The underlying CRWBrowsingDataStore.
__weak CRWBrowsingDataStore* _browsingDataStore;
}
@synthesize modeChangeCount = _modeChangeCount;
- (instancetype)initWithBrowsingDataStore:
(CRWBrowsingDataStore*)browsingDataStore {
self = [super init];
if (self) {
DCHECK(browsingDataStore);
[browsingDataStore addObserver:self
forKeyPath:@"mode"
options:0
context:nil];
_browsingDataStore = browsingDataStore;
}
return self;
}
- (void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context {
DCHECK([keyPath isEqual:@"mode"]);
DCHECK_EQ(_browsingDataStore, object);
++self.modeChangeCount;
}
- (void)dealloc {
[_browsingDataStore removeObserver:self forKeyPath:@"mode"];
[super dealloc];
}
@end
namespace web {
namespace {
class BrowsingDataStoreTest : public PlatformTest {
protected:
void SetUp() override {
PlatformTest::SetUp();
browser_state_.reset(new TestBrowserState());
BrowserState::GetActiveStateManager(browser_state_.get())->SetActive(true);
browsing_data_store_.reset([[CRWBrowsingDataStore alloc]
initWithBrowserState:browser_state_.get()]);
}
void TearDown() override {
// The BrowserState needs to be destroyed first so that it is outlived by
// the WebThreadBundle.
BrowserState::GetActiveStateManager(browser_state_.get())->SetActive(false);
browser_state_.reset();
PlatformTest::TearDown();
}
// Sets the mode of the |browsing_data_store_| to |ACTIVE| and blocks until
// the mode has actually been changed.
void MakeActive() {
[browsing_data_store_ makeActiveWithCompletionHandler:^(BOOL success) {
DCHECK(success);
}];
base::test::ios::WaitUntilCondition(^bool() {
return [browsing_data_store_ mode] == ACTIVE;
});
}
// Sets the mode of the |browsing_data_store_| to |INACTIVE| and blocks until
// the mode has actually been changed.
void MakeInactive() {
[browsing_data_store_ makeInactiveWithCompletionHandler:^(BOOL success) {
DCHECK(success);
}];
base::test::ios::WaitUntilCondition(^bool() {
return [browsing_data_store_ mode] == INACTIVE;
});
}
// Removes browsing data of |browsingDataTypes| from the underlying
// CRWBrowsingDataStore and waits until the operation finished.
void RemoveDataOfTypes(web::BrowsingDataTypes browsing_data_types) {
__block BOOL block_was_called = NO;
[browsing_data_store_ removeDataOfTypes:browsing_data_types
completionHandler:^{
block_was_called = YES;
}];
base::test::ios::WaitUntilCondition(^bool() {
return block_was_called;
});
}
// The CRWBrowsingDataStore used for testing purposes.
base::scoped_nsobject<CRWBrowsingDataStore> browsing_data_store_;
private:
// The WebThreadBundle used for testing purposes.
TestWebThreadBundle thread_bundle_;
// The BrowserState used for testing purposes.
scoped_ptr<BrowserState> browser_state_;
};
} // namespace
// Tests that a CRWBrowsingDataStore's initial mode is set correctly and that it
// has no pending operations.
TEST_F(BrowsingDataStoreTest, InitialModeAndNoPendingOperations) {
if (!base::ios::IsRunningOnIOS8OrLater()) {
return;
}
EXPECT_EQ(ACTIVE, [browsing_data_store_ mode]);
EXPECT_FALSE([browsing_data_store_ hasPendingOperations]);
}
// Tests that CRWBrowsingDataStore handles several consecutive calls to
// |makeActive| and |makeInactive| correctly.
TEST_F(BrowsingDataStoreTest, MakeActiveAndInactiveOperations) {
if (!base::ios::IsRunningOnIOS8OrLater()) {
return;
}
MakeInactive();
base::scoped_nsobject<CRWTestBrowsingDataStoreObserver> observer(
[[CRWTestBrowsingDataStoreObserver alloc]
initWithBrowsingDataStore:browsing_data_store_]);
EXPECT_EQ(0U, [observer modeChangeCount]);
id unsucessfullCallback = ^(BOOL success) {
ASSERT_TRUE([NSThread isMainThread]);
BrowsingDataStoreMode mode = [browsing_data_store_ mode];
EXPECT_FALSE(success);
EXPECT_EQ(CHANGING, mode);
};
[browsing_data_store_ makeActiveWithCompletionHandler:unsucessfullCallback];
EXPECT_EQ(CHANGING, [browsing_data_store_ mode]);
EXPECT_EQ(1U, [observer modeChangeCount]);
[browsing_data_store_ makeInactiveWithCompletionHandler:unsucessfullCallback];
EXPECT_EQ(CHANGING, [browsing_data_store_ mode]);
[browsing_data_store_ makeActiveWithCompletionHandler:unsucessfullCallback];
EXPECT_EQ(CHANGING, [browsing_data_store_ mode]);
__block BOOL block_was_called = NO;
[browsing_data_store_ makeInactiveWithCompletionHandler:^(BOOL success) {
ASSERT_TRUE([NSThread isMainThread]);
BrowsingDataStoreMode mode = [browsing_data_store_ mode];
EXPECT_TRUE(success);
EXPECT_EQ(INACTIVE, mode);
block_was_called = YES;
}];
EXPECT_EQ(CHANGING, [browsing_data_store_ mode]);
base::test::ios::WaitUntilCondition(^bool{
return block_was_called;
});
EXPECT_EQ(INACTIVE, [browsing_data_store_ mode]);
EXPECT_EQ(2U, [observer modeChangeCount]);
}
// Tests that CRWBrowsingDataStore correctly handles |removeDataOfTypes:|.
TEST_F(BrowsingDataStoreTest, RemoveDataOperations) {
if (!base::ios::IsRunningOnIOS8OrLater()) {
return;
}
ASSERT_EQ(ACTIVE, [browsing_data_store_ mode]);
// |removeDataOfTypes| is called when the mode was ACTIVE.
RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES);
MakeInactive();
ASSERT_EQ(INACTIVE, [browsing_data_store_ mode]);
// |removeDataOfTypes| is called when the mode was INACTIVE.
RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES);
}
// Tests that CRWBrowsingDataStore correctly handles |removeDataOfTypes:| after
// a |makeActive| call.
TEST_F(BrowsingDataStoreTest, RemoveDataOperationAfterMakeActiveCall) {
if (!base::ios::IsRunningOnIOS8OrLater()) {
return;
}
MakeInactive();
ASSERT_EQ(INACTIVE, [browsing_data_store_ mode]);
[browsing_data_store_ makeActiveWithCompletionHandler:nil];
// |removeDataOfTypes| is called immediately after a |makeActive| call.
RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES);
EXPECT_EQ(ACTIVE, [browsing_data_store_ mode]);
}
// Tests that CRWBrowsingDataStore correctly handles |removeDataOfTypes:| after
// a |makeActive| call.
TEST_F(BrowsingDataStoreTest, RemoveDataOperationAfterMakeInactiveCall) {
if (!base::ios::IsRunningOnIOS8OrLater()) {
return;
}
ASSERT_EQ(ACTIVE, [browsing_data_store_ mode]);
[browsing_data_store_ makeInactiveWithCompletionHandler:nil];
// |removeDataOfTypes| is called immediately after a |makeInactive| call.
RemoveDataOfTypes(BROWSING_DATA_TYPE_COOKIES);
EXPECT_EQ(INACTIVE, [browsing_data_store_ mode]);
}
} // namespace web
|