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 #import "base/mac/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h"
9 #import "base/test/ios/wait_util.h"
10 #include "ios/web/public/active_state_manager.h"
11 #include "ios/web/public/browser_state.h"
12 #include "ios/web/public/test/test_browser_state.h"
13 #include "ios/web/public/test/test_web_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
20 class BrowsingDataStoreTest : public PlatformTest {
22 void SetUp() override {
23 PlatformTest::SetUp();
24 browser_state_.reset(new TestBrowserState());
25 BrowserState::GetActiveStateManager(browser_state_.get())->SetActive(true);
26 browsing_data_store_.reset([[CRWBrowsingDataStore alloc]
27 initWithBrowserState:browser_state_.get()]);
29 void TearDown() override {
30 // The BrowserState needs to be destroyed first so that it is outlived by
31 // the WebThreadBundle.
32 BrowserState::GetActiveStateManager(browser_state_.get())->SetActive(false);
33 browser_state_.reset();
34 PlatformTest::TearDown();
37 // The CRWBrowsingDataStore used for testing purposes.
38 base::scoped_nsobject<CRWBrowsingDataStore> browsing_data_store_;
41 // The WebThreadBundle used for testing purposes.
42 TestWebThreadBundle thread_bundle_;
43 // The BrowserState used for testing purposes.
44 scoped_ptr<BrowserState> browser_state_;
49 // Tests that a CRWBrowsingDataStore's initial mode is set correctly and that it
50 // has no pending operations.
51 TEST_F(BrowsingDataStoreTest, InitialModeAndNoPendingOperations) {
52 EXPECT_EQ(ACTIVE, [browsing_data_store_ mode]);
53 EXPECT_FALSE([browsing_data_store_ hasPendingOperations]);
56 // Tests that CRWBrowsingDataStore handles several consecutive calls to
57 // |makeActive| and |makeInactive| correctly.
58 TEST_F(BrowsingDataStoreTest, MakeActiveAndInactiveOperations) {
59 ProceduralBlock makeActiveCallback = ^{
60 ASSERT_TRUE([NSThread isMainThread]);
61 CRWBrowsingDataStoreMode mode = [browsing_data_store_ mode];
62 EXPECT_TRUE((mode == ACTIVE) || (mode == SYNCHRONIZING));
64 ProceduralBlock makeInactiveCallback = ^{
65 ASSERT_TRUE([NSThread isMainThread]);
66 CRWBrowsingDataStoreMode mode = [browsing_data_store_ mode];
67 EXPECT_TRUE((mode == INACTIVE) || (mode == SYNCHRONIZING));
69 [browsing_data_store_ makeActiveWithCompletionHandler:makeActiveCallback];
70 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]);
72 [browsing_data_store_ makeInactiveWithCompletionHandler:makeInactiveCallback];
73 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]);
75 [browsing_data_store_ makeActiveWithCompletionHandler:makeActiveCallback];
76 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]);
78 __block BOOL block_was_called = NO;
79 [browsing_data_store_ makeInactiveWithCompletionHandler:^{
80 makeInactiveCallback();
81 block_was_called = YES;
83 EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]);
85 base::test::ios::WaitUntilCondition(^bool{
86 return block_was_called;
90 // Tests that CRWBrowsingDataStore correctly handles |removeDataOfTypes:| call.
91 TEST_F(BrowsingDataStoreTest, RemoveDataOperations) {
92 web::BrowsingDataTypes browsing_data_types = web::BROWSING_DATA_TYPE_COOKIES;
93 __block BOOL block_was_called = NO;
94 [browsing_data_store_ removeDataOfTypes:browsing_data_types
96 DCHECK([NSThread isMainThread]);
97 block_was_called = YES;
99 base::test::ios::WaitUntilCondition(^bool() {
100 return block_was_called;