OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_cache_scheduler.h" | 5 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 class JobStats { | 16 class JobStats { |
17 public: | 17 public: |
18 JobStats(ServiceWorkerCacheScheduler* scheduler) | 18 JobStats(CacheStorageScheduler* scheduler) |
19 : scheduler_(scheduler), callback_count_(0) {} | 19 : scheduler_(scheduler), callback_count_(0) {} |
20 | 20 |
21 virtual void Run() = 0; | 21 virtual void Run() = 0; |
22 | 22 |
23 int callback_count() const { return callback_count_; } | 23 int callback_count() const { return callback_count_; } |
24 | 24 |
25 protected: | 25 protected: |
26 ServiceWorkerCacheScheduler* scheduler_; | 26 CacheStorageScheduler* scheduler_; |
27 int callback_count_; | 27 int callback_count_; |
28 }; | 28 }; |
29 | 29 |
30 class SyncJob : public JobStats { | 30 class SyncJob : public JobStats { |
31 public: | 31 public: |
32 SyncJob(ServiceWorkerCacheScheduler* scheduler) : JobStats(scheduler) {} | 32 SyncJob(CacheStorageScheduler* scheduler) : JobStats(scheduler) {} |
33 | 33 |
34 void Run() override { | 34 void Run() override { |
35 callback_count_++; | 35 callback_count_++; |
36 scheduler_->CompleteOperationAndRunNext(); | 36 scheduler_->CompleteOperationAndRunNext(); |
37 } | 37 } |
38 }; | 38 }; |
39 | 39 |
40 class AsyncJob : public JobStats { | 40 class AsyncJob : public JobStats { |
41 public: | 41 public: |
42 AsyncJob(ServiceWorkerCacheScheduler* scheduler) : JobStats(scheduler) {} | 42 AsyncJob(CacheStorageScheduler* scheduler) : JobStats(scheduler) {} |
43 | 43 |
44 void Run() override { callback_count_++; } | 44 void Run() override { callback_count_++; } |
45 void Done() { scheduler_->CompleteOperationAndRunNext(); } | 45 void Done() { scheduler_->CompleteOperationAndRunNext(); } |
46 }; | 46 }; |
47 | 47 |
48 } // namespace | 48 } // namespace |
49 | 49 |
50 class ServiceWorkerCacheSchedulerTest : public testing::Test { | 50 class CacheStorageSchedulerTest : public testing::Test { |
51 protected: | 51 protected: |
52 ServiceWorkerCacheSchedulerTest() | 52 CacheStorageSchedulerTest() |
53 : async_job_(AsyncJob(&scheduler_)), sync_job_(SyncJob(&scheduler_)) {} | 53 : async_job_(AsyncJob(&scheduler_)), sync_job_(SyncJob(&scheduler_)) {} |
54 | 54 |
55 ServiceWorkerCacheScheduler scheduler_; | 55 CacheStorageScheduler scheduler_; |
56 AsyncJob async_job_; | 56 AsyncJob async_job_; |
57 SyncJob sync_job_; | 57 SyncJob sync_job_; |
58 }; | 58 }; |
59 | 59 |
60 TEST_F(ServiceWorkerCacheSchedulerTest, ScheduleOne) { | 60 TEST_F(CacheStorageSchedulerTest, ScheduleOne) { |
61 scheduler_.ScheduleOperation( | 61 scheduler_.ScheduleOperation( |
62 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); | 62 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); |
63 EXPECT_EQ(1, sync_job_.callback_count()); | 63 EXPECT_EQ(1, sync_job_.callback_count()); |
64 } | 64 } |
65 | 65 |
66 TEST_F(ServiceWorkerCacheSchedulerTest, ScheduleTwo) { | 66 TEST_F(CacheStorageSchedulerTest, ScheduleTwo) { |
67 scheduler_.ScheduleOperation( | 67 scheduler_.ScheduleOperation( |
68 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); | 68 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); |
69 scheduler_.ScheduleOperation( | 69 scheduler_.ScheduleOperation( |
70 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); | 70 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); |
71 EXPECT_EQ(2, sync_job_.callback_count()); | 71 EXPECT_EQ(2, sync_job_.callback_count()); |
72 } | 72 } |
73 | 73 |
74 TEST_F(ServiceWorkerCacheSchedulerTest, Block) { | 74 TEST_F(CacheStorageSchedulerTest, Block) { |
75 scheduler_.ScheduleOperation( | 75 scheduler_.ScheduleOperation( |
76 base::Bind(&JobStats::Run, base::Unretained(&async_job_))); | 76 base::Bind(&JobStats::Run, base::Unretained(&async_job_))); |
77 EXPECT_EQ(1, async_job_.callback_count()); | 77 EXPECT_EQ(1, async_job_.callback_count()); |
78 scheduler_.ScheduleOperation( | 78 scheduler_.ScheduleOperation( |
79 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); | 79 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); |
80 EXPECT_EQ(0, sync_job_.callback_count()); | 80 EXPECT_EQ(0, sync_job_.callback_count()); |
81 async_job_.Done(); | 81 async_job_.Done(); |
82 EXPECT_EQ(1, sync_job_.callback_count()); | 82 EXPECT_EQ(1, sync_job_.callback_count()); |
83 } | 83 } |
84 | 84 |
85 } // namespace content | 85 } // namespace content |
OLD | NEW |