OLD | NEW |
| (Empty) |
1 // Copyright 2016 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. | |
4 | |
5 #include "components/offline_pages/background/initialize_store_task.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/test/test_simple_task_runner.h" | |
11 #include "base/threading/thread_task_runner_handle.h" | |
12 #include "components/offline_pages/background/request_queue_in_memory_store.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace offline_pages { | |
16 | |
17 using TestScenario = RequestQueueInMemoryStore::TestScenario; | |
18 | |
19 class InitializeStoreTaskTest : public testing::Test { | |
20 public: | |
21 InitializeStoreTaskTest(); | |
22 ~InitializeStoreTaskTest() override; | |
23 | |
24 void PumpLoop(); | |
25 void RunNextStep(); | |
26 | |
27 void InitializeCallback(bool success); | |
28 | |
29 bool callback_called() const { return callback_called_; } | |
30 | |
31 bool last_call_successful() const { return success_; } | |
32 | |
33 private: | |
34 bool callback_called_; | |
35 bool success_; | |
36 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
37 base::ThreadTaskRunnerHandle task_runner_handle_; | |
38 }; | |
39 | |
40 InitializeStoreTaskTest::InitializeStoreTaskTest() | |
41 : callback_called_(false), | |
42 success_(false), | |
43 task_runner_(new base::TestSimpleTaskRunner), | |
44 task_runner_handle_(task_runner_) {} | |
45 | |
46 InitializeStoreTaskTest::~InitializeStoreTaskTest() {} | |
47 | |
48 void InitializeStoreTaskTest::PumpLoop() { | |
49 task_runner_->RunUntilIdle(); | |
50 } | |
51 | |
52 void InitializeStoreTaskTest::RunNextStep() { | |
53 // Only runs tasks that are already scheduled and available. If running these | |
54 // tasks post new tasks to the runner, they will not be triggered. This allows | |
55 // for running InitializeStoreTask one step at a time. | |
56 task_runner_->RunPendingTasks(); | |
57 } | |
58 | |
59 void InitializeStoreTaskTest::InitializeCallback(bool success) { | |
60 callback_called_ = true; | |
61 success_ = success; | |
62 } | |
63 | |
64 TEST_F(InitializeStoreTaskTest, SuccessfulInitialization) { | |
65 RequestQueueInMemoryStore store; | |
66 InitializeStoreTask task( | |
67 &store, base::Bind(&InitializeStoreTaskTest::InitializeCallback, | |
68 base::Unretained(this))); | |
69 task.Run(); | |
70 PumpLoop(); | |
71 EXPECT_TRUE(callback_called()); | |
72 EXPECT_TRUE(last_call_successful()); | |
73 EXPECT_EQ(StoreState::LOADED, store.state()); | |
74 } | |
75 | |
76 TEST_F(InitializeStoreTaskTest, SuccessfulReset) { | |
77 RequestQueueInMemoryStore store(TestScenario::LOAD_FAILED_RESET_SUCCESS); | |
78 InitializeStoreTask task( | |
79 &store, base::Bind(&InitializeStoreTaskTest::InitializeCallback, | |
80 base::Unretained(this))); | |
81 task.Run(); | |
82 EXPECT_FALSE(callback_called()); | |
83 EXPECT_EQ(StoreState::FAILED_LOADING, store.state()); | |
84 | |
85 RunNextStep(); | |
86 EXPECT_FALSE(callback_called()); | |
87 EXPECT_EQ(StoreState::NOT_LOADED, store.state()); | |
88 | |
89 PumpLoop(); | |
90 EXPECT_TRUE(callback_called()); | |
91 EXPECT_TRUE(last_call_successful()); | |
92 EXPECT_EQ(StoreState::LOADED, store.state()); | |
93 } | |
94 | |
95 TEST_F(InitializeStoreTaskTest, FailedReset) { | |
96 RequestQueueInMemoryStore store(TestScenario::LOAD_FAILED_RESET_FAILED); | |
97 InitializeStoreTask task( | |
98 &store, base::Bind(&InitializeStoreTaskTest::InitializeCallback, | |
99 base::Unretained(this))); | |
100 task.Run(); | |
101 PumpLoop(); | |
102 EXPECT_TRUE(callback_called()); | |
103 EXPECT_FALSE(last_call_successful()); | |
104 EXPECT_EQ(StoreState::FAILED_RESET, store.state()); | |
105 } | |
106 | |
107 } // namespace offline_pages | |
OLD | NEW |