OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/browser/service_worker/service_worker_cache_scheduler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
9 #include "base/run_loop.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace content { | |
13 | |
14 namespace { | |
15 | |
16 class JobStats { | |
17 public: | |
18 JobStats(ServiceWorkerCacheScheduler* scheduler) | |
19 : scheduler_(scheduler), callback_count_(0) {} | |
20 | |
21 virtual void Run() = 0; | |
22 | |
23 int callback_count() const { return callback_count_; } | |
24 | |
25 protected: | |
26 ServiceWorkerCacheScheduler* scheduler_; | |
27 int callback_count_; | |
28 }; | |
29 | |
30 class SyncJob : public JobStats { | |
31 public: | |
32 SyncJob(ServiceWorkerCacheScheduler* scheduler) : JobStats(scheduler) {} | |
33 | |
34 void Run() override { | |
35 callback_count_++; | |
36 scheduler_->CompleteOperationAndRunNext(); | |
37 } | |
38 }; | |
39 | |
40 class AsyncJob : public JobStats { | |
41 public: | |
42 AsyncJob(ServiceWorkerCacheScheduler* scheduler) : JobStats(scheduler) {} | |
43 | |
44 void Run() override { callback_count_++; } | |
45 void Done() { scheduler_->CompleteOperationAndRunNext(); } | |
46 }; | |
47 | |
48 } // namespace | |
49 | |
50 class ServiceWorkerCacheSchedulerTest : public testing::Test { | |
51 protected: | |
52 ServiceWorkerCacheSchedulerTest() | |
53 : async_job_(AsyncJob(&scheduler_)), sync_job_(SyncJob(&scheduler_)) {} | |
54 | |
55 ServiceWorkerCacheScheduler scheduler_; | |
56 AsyncJob async_job_; | |
57 SyncJob sync_job_; | |
58 }; | |
59 | |
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()); | |
64 } | |
65 | |
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()); | |
72 } | |
73 | |
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()); | |
81 async_job_.Done(); | |
82 EXPECT_EQ(1, sync_job_.callback_count()); | |
83 } | |
84 | |
85 } // namespace content | |
OLD | NEW |