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"
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"
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_
; }
28 CacheStorageScheduler
* scheduler_
;
34 class CacheStorageSchedulerTest
: public testing::Test
{
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_
;
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());
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());
78 EXPECT_FALSE(scheduler_
.ScheduledOperations());
81 } // namespace content