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/service_worker/service_worker_cache_scheduler.h"
8 #include "base/callback.h"
9 #include "base/run_loop.h"
10 #include "testing/gtest/include/gtest/gtest.h"
18 JobStats(ServiceWorkerCacheScheduler
* scheduler
)
19 : scheduler_(scheduler
), callback_count_(0) {}
21 virtual void Run() = 0;
23 int callback_count() const { return callback_count_
; }
26 ServiceWorkerCacheScheduler
* scheduler_
;
30 class SyncJob
: public JobStats
{
32 SyncJob(ServiceWorkerCacheScheduler
* scheduler
) : JobStats(scheduler
) {}
36 scheduler_
->CompleteOperationAndRunNext();
40 class AsyncJob
: public JobStats
{
42 AsyncJob(ServiceWorkerCacheScheduler
* scheduler
) : JobStats(scheduler
) {}
44 void Run() override
{ callback_count_
++; }
45 void Done() { scheduler_
->CompleteOperationAndRunNext(); }
50 class ServiceWorkerCacheSchedulerTest
: public testing::Test
{
52 ServiceWorkerCacheSchedulerTest()
53 : async_job_(AsyncJob(&scheduler_
)), sync_job_(SyncJob(&scheduler_
)) {}
55 ServiceWorkerCacheScheduler scheduler_
;
60 TEST_F(ServiceWorkerCacheSchedulerTest
, ScheduleOne
) {
61 scheduler_
.ScheduleOperation(
62 base::Bind(&JobStats::Run
, base::Unretained(&sync_job_
)));
63 EXPECT_EQ(1, sync_job_
.callback_count());
66 TEST_F(ServiceWorkerCacheSchedulerTest
, ScheduleTwo
) {
67 scheduler_
.ScheduleOperation(
68 base::Bind(&JobStats::Run
, base::Unretained(&sync_job_
)));
69 scheduler_
.ScheduleOperation(
70 base::Bind(&JobStats::Run
, base::Unretained(&sync_job_
)));
71 EXPECT_EQ(2, sync_job_
.callback_count());
74 TEST_F(ServiceWorkerCacheSchedulerTest
, Block
) {
75 scheduler_
.ScheduleOperation(
76 base::Bind(&JobStats::Run
, base::Unretained(&async_job_
)));
77 EXPECT_EQ(1, async_job_
.callback_count());
78 scheduler_
.ScheduleOperation(
79 base::Bind(&JobStats::Run
, base::Unretained(&sync_job_
)));
80 EXPECT_EQ(0, sync_job_
.callback_count());
82 EXPECT_EQ(1, sync_job_
.callback_count());
85 } // namespace content