Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / cache_storage / cache_storage_scheduler_unittest.cc
blobb04e8e7321f091197cae087cd7c4644e09b8907e
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 #include "content/browser/cache_storage/cache_storage_scheduler.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/run_loop.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace content {
15 namespace {
17 class TestTask {
18 public:
19 TestTask(CacheStorageScheduler* scheduler)
20 : scheduler_(scheduler), callback_count_(0) {}
22 virtual void Run() { callback_count_++; }
23 void Done() { scheduler_->CompleteOperationAndRunNext(); }
25 int callback_count() const { return callback_count_; }
27 protected:
28 CacheStorageScheduler* scheduler_;
29 int callback_count_;
32 } // namespace
34 class CacheStorageSchedulerTest : public testing::Test {
35 protected:
36 CacheStorageSchedulerTest()
37 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
38 task1_(TestTask(&scheduler_)),
39 task2_(TestTask(&scheduler_)) {}
41 TestBrowserThreadBundle browser_thread_bundle_;
42 CacheStorageScheduler scheduler_;
43 TestTask task1_;
44 TestTask task2_;
47 TEST_F(CacheStorageSchedulerTest, ScheduleOne) {
48 scheduler_.ScheduleOperation(
49 base::Bind(&TestTask::Run, base::Unretained(&task1_)));
50 base::RunLoop().RunUntilIdle();
51 EXPECT_EQ(1, task1_.callback_count());
54 TEST_F(CacheStorageSchedulerTest, ScheduleTwo) {
55 scheduler_.ScheduleOperation(
56 base::Bind(&TestTask::Run, base::Unretained(&task1_)));
57 scheduler_.ScheduleOperation(
58 base::Bind(&TestTask::Run, base::Unretained(&task2_)));
59 base::RunLoop().RunUntilIdle();
60 EXPECT_EQ(1, task1_.callback_count());
61 EXPECT_EQ(0, task2_.callback_count());
63 task1_.Done();
64 EXPECT_TRUE(scheduler_.ScheduledOperations());
65 base::RunLoop().RunUntilIdle();
66 EXPECT_EQ(1, task1_.callback_count());
67 EXPECT_EQ(1, task2_.callback_count());
70 TEST_F(CacheStorageSchedulerTest, ScheduledOperations) {
71 scheduler_.ScheduleOperation(
72 base::Bind(&TestTask::Run, base::Unretained(&task1_)));
73 EXPECT_TRUE(scheduler_.ScheduledOperations());
74 base::RunLoop().RunUntilIdle();
75 EXPECT_EQ(1, task1_.callback_count());
76 EXPECT_TRUE(scheduler_.ScheduledOperations());
77 task1_.Done();
78 EXPECT_FALSE(scheduler_.ScheduledOperations());
81 } // namespace content