Fix breakages in https://codereview.chromium.org/1155713003/
[chromium-blink-merge.git] / ios / web / crw_browsing_data_store_unittest.mm
blobf5466990cfb72e6ee6bcdf0598dc92bc2f4413eb
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"
17 namespace web {
18 namespace {
20 class BrowsingDataStoreTest : public PlatformTest {
21  protected:
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()]);
28   }
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();
35   }
37   // The CRWBrowsingDataStore used for testing purposes.
38   base::scoped_nsobject<CRWBrowsingDataStore> browsing_data_store_;
40  private:
41   // The WebThreadBundle used for testing purposes.
42   TestWebThreadBundle thread_bundle_;
43   // The BrowserState used for testing purposes.
44   scoped_ptr<BrowserState> browser_state_;
47 }  // namespace
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));
63   };
64   ProceduralBlock makeInactiveCallback = ^{
65     ASSERT_TRUE([NSThread isMainThread]);
66     CRWBrowsingDataStoreMode mode = [browsing_data_store_ mode];
67     EXPECT_TRUE((mode == INACTIVE) || (mode == SYNCHRONIZING));
68   };
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;
82   }];
83   EXPECT_EQ(SYNCHRONIZING, [browsing_data_store_ mode]);
85   base::test::ios::WaitUntilCondition(^bool{
86     return block_was_called;
87   });
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
95                         completionHandler:^{
96                           DCHECK([NSThread isMainThread]);
97                           block_was_called = YES;
98                         }];
99   base::test::ios::WaitUntilCondition(^bool() {
100     return block_was_called;
101   });
104 }  // namespace web