summaryrefslogtreecommitdiffstats
path: root/ios/web/crw_browsing_data_store_unittest.mm
blob: f3b2bfe69a6735f69598c2fc51d3e7814aaafc45 (plain)
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
// 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 "ios/web/test/web_test.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
// The number of times that the mode of the underlying CRWBrowsingDataStore
// changed.
@property(nonatomic, assign) NSUInteger modeChangeCount;

// |browsingDataStore| cannot be null.
- (instancetype)initWithBrowsingDataStore:
    (CRWBrowsingDataStore*)browsingDataStore NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
@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;
}

- (instancetype)init {
  NOTREACHED();
  return nil;
}

- (void)dealloc {
  [_browsingDataStore removeObserver:self forKeyPath:@"mode"];
  [super dealloc];
}

- (void)observeValueForKeyPath:(NSString*)keyPath
                      ofObject:(id)object
                        change:(NSDictionary*)change
                       context:(void*)context {
  DCHECK([keyPath isEqual:@"mode"]);
  DCHECK_EQ(_browsingDataStore, object);

  ++self.modeChangeCount;
}

@end

namespace web {
namespace {

// A test fixture for testing CRWBrowsingDataStore.
class BrowsingDataStoreTest : public WebTest {
 protected:
  void SetUp() override {
    WebTest::SetUp();
    ASSERT_TRUE(
        BrowserState::GetActiveStateManager(GetBrowserState())->IsActive());
    browsing_data_store_.reset(
        [[CRWBrowsingDataStore alloc] initWithBrowserState:GetBrowserState()]);
  }

  // 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_;
};

}  // 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]);

  __block int callbacks_received_count = 0;
  void (^unsucessfullCallback)(BOOL) = ^(BOOL success) {
    ASSERT_TRUE([NSThread isMainThread]);
    ++callbacks_received_count;
    BrowsingDataStoreMode mode = [browsing_data_store_ mode];
    EXPECT_FALSE(success);
    EXPECT_EQ(CHANGING, mode);
  };

  [browsing_data_store_ makeActiveWithCompletionHandler:^(BOOL success) {
    EXPECT_EQ(0, callbacks_received_count);
    unsucessfullCallback(success);
  }];
  EXPECT_EQ(CHANGING, [browsing_data_store_ mode]);
  EXPECT_EQ(1U, [observer modeChangeCount]);

  [browsing_data_store_ makeInactiveWithCompletionHandler:^(BOOL success) {
    EXPECT_EQ(1, callbacks_received_count);
    unsucessfullCallback(success);
  }];
  EXPECT_EQ(CHANGING, [browsing_data_store_ mode]);

  [browsing_data_store_ makeActiveWithCompletionHandler:^(BOOL success) {
    EXPECT_EQ(2, callbacks_received_count);
    unsucessfullCallback(success);
  }];
  EXPECT_EQ(CHANGING, [browsing_data_store_ mode]);

  __block BOOL block_was_called = NO;
  [browsing_data_store_ makeInactiveWithCompletionHandler:^(BOOL success) {
    ASSERT_TRUE([NSThread isMainThread]);
    EXPECT_EQ(3, callbacks_received_count);
    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