| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "extensions/browser/lazy_background_task_queue.h" | 5 #include "extensions/browser/lazy_background_task_queue.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/prefs/testing_pref_service.h" |
| 8 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 9 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 10 #include "components/user_prefs/user_prefs.h" |
| 9 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 10 #include "content/public/test/test_browser_context.h" | 12 #include "content/public/test/test_browser_context.h" |
| 11 #include "extensions/browser/extension_registry.h" | 13 #include "extensions/browser/extension_registry.h" |
| 12 #include "extensions/browser/extension_registry_factory.h" | 14 #include "extensions/browser/extension_registry_factory.h" |
| 13 #include "extensions/browser/extension_system.h" | |
| 14 #include "extensions/browser/extension_system_provider.h" | |
| 15 #include "extensions/browser/extensions_test.h" | 15 #include "extensions/browser/extensions_test.h" |
| 16 #include "extensions/browser/mock_extension_system.h" | |
| 17 #include "extensions/browser/process_manager.h" | 16 #include "extensions/browser/process_manager.h" |
| 17 #include "extensions/browser/process_manager_factory.h" |
| 18 #include "extensions/browser/test_extensions_browser_client.h" | 18 #include "extensions/browser/test_extensions_browser_client.h" |
| 19 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/extension_builder.h" | 20 #include "extensions/common/extension_builder.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 using content::BrowserContext; | 23 using content::BrowserContext; |
| 24 | 24 |
| 25 namespace extensions { | 25 namespace extensions { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 45 create_count_++; | 45 create_count_++; |
| 46 return false; | 46 return false; |
| 47 } | 47 } |
| 48 | 48 |
| 49 private: | 49 private: |
| 50 int create_count_; | 50 int create_count_; |
| 51 | 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(TestProcessManager); | 52 DISALLOW_COPY_AND_ASSIGN(TestProcessManager); |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // A simple ExtensionSystem that returns a TestProcessManager. | 55 KeyedService* CreateTestProcessManager(BrowserContext* context) { |
| 56 class MockExtensionSystemWithProcessManager : public MockExtensionSystem { | 56 return new TestProcessManager(context); |
| 57 public: | 57 } |
| 58 explicit MockExtensionSystemWithProcessManager(BrowserContext* context) | |
| 59 : MockExtensionSystem(context), test_process_manager_(context) {} | |
| 60 ~MockExtensionSystemWithProcessManager() override {} | |
| 61 | |
| 62 ProcessManager* process_manager() override { return &test_process_manager_; } | |
| 63 | |
| 64 private: | |
| 65 TestProcessManager test_process_manager_; | |
| 66 }; | |
| 67 | 58 |
| 68 } // namespace | 59 } // namespace |
| 69 | 60 |
| 70 // Derives from ExtensionsTest to provide content module and keyed service | 61 // Derives from ExtensionsTest to provide content module and keyed service |
| 71 // initialization. | 62 // initialization. |
| 72 class LazyBackgroundTaskQueueTest : public ExtensionsTest { | 63 class LazyBackgroundTaskQueueTest : public ExtensionsTest { |
| 73 public: | 64 public: |
| 74 LazyBackgroundTaskQueueTest() | 65 LazyBackgroundTaskQueueTest() |
| 75 : notification_service_(content::NotificationService::Create()), | 66 : notification_service_(content::NotificationService::Create()), |
| 76 task_run_count_(0) { | 67 task_run_count_(0) { |
| 77 extensions_browser_client()->set_extension_system_factory( | |
| 78 &extension_system_factory_); | |
| 79 } | 68 } |
| 80 virtual ~LazyBackgroundTaskQueueTest() {} | 69 virtual ~LazyBackgroundTaskQueueTest() {} |
| 81 | 70 |
| 82 int task_run_count() { return task_run_count_; } | 71 int task_run_count() { return task_run_count_; } |
| 83 | 72 |
| 84 // A simple callback for AddPendingTask. | 73 // A simple callback for AddPendingTask. |
| 85 void RunPendingTask(ExtensionHost* host) { | 74 void RunPendingTask(ExtensionHost* host) { |
| 86 task_run_count_++; | 75 task_run_count_++; |
| 87 } | 76 } |
| 88 | 77 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 109 .Set("background", | 98 .Set("background", |
| 110 DictionaryBuilder() | 99 DictionaryBuilder() |
| 111 .Set("page", "background.html") | 100 .Set("page", "background.html") |
| 112 .SetBoolean("persistent", false))) | 101 .SetBoolean("persistent", false))) |
| 113 .SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") | 102 .SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") |
| 114 .Build(); | 103 .Build(); |
| 115 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); | 104 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); |
| 116 return extension; | 105 return extension; |
| 117 } | 106 } |
| 118 | 107 |
| 108 protected: |
| 109 virtual void SetUp() override { |
| 110 user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_); |
| 111 } |
| 112 |
| 119 private: | 113 private: |
| 120 scoped_ptr<content::NotificationService> notification_service_; | 114 scoped_ptr<content::NotificationService> notification_service_; |
| 121 MockExtensionSystemFactory<MockExtensionSystemWithProcessManager> | 115 |
| 122 extension_system_factory_; | 116 TestingPrefServiceSimple testing_pref_service_; |
| 123 | 117 |
| 124 // The total number of pending tasks that have been executed. | 118 // The total number of pending tasks that have been executed. |
| 125 int task_run_count_; | 119 int task_run_count_; |
| 126 | 120 |
| 127 DISALLOW_COPY_AND_ASSIGN(LazyBackgroundTaskQueueTest); | 121 DISALLOW_COPY_AND_ASSIGN(LazyBackgroundTaskQueueTest); |
| 128 }; | 122 }; |
| 129 | 123 |
| 130 // Tests that only extensions with background pages should have tasks queued. | 124 // Tests that only extensions with background pages should have tasks queued. |
| 131 TEST_F(LazyBackgroundTaskQueueTest, ShouldEnqueueTask) { | 125 TEST_F(LazyBackgroundTaskQueueTest, ShouldEnqueueTask) { |
| 132 LazyBackgroundTaskQueue queue(browser_context()); | 126 LazyBackgroundTaskQueue queue(browser_context()); |
| 133 | 127 |
| 134 // Build a simple extension with no background page. | 128 // Build a simple extension with no background page. |
| 135 scoped_refptr<Extension> no_background = CreateSimpleExtension(); | 129 scoped_refptr<Extension> no_background = CreateSimpleExtension(); |
| 136 EXPECT_FALSE(queue.ShouldEnqueueTask(browser_context(), no_background.get())); | 130 EXPECT_FALSE(queue.ShouldEnqueueTask(browser_context(), no_background.get())); |
| 137 | 131 |
| 138 // Build another extension with a background page. | 132 // Build another extension with a background page. |
| 139 scoped_refptr<Extension> with_background = CreateLazyBackgroundExtension(); | 133 scoped_refptr<Extension> with_background = CreateLazyBackgroundExtension(); |
| 140 EXPECT_TRUE( | 134 EXPECT_TRUE( |
| 141 queue.ShouldEnqueueTask(browser_context(), with_background.get())); | 135 queue.ShouldEnqueueTask(browser_context(), with_background.get())); |
| 142 } | 136 } |
| 143 | 137 |
| 144 // Tests that adding tasks actually increases the pending task count, and that | 138 // Tests that adding tasks actually increases the pending task count, and that |
| 145 // multiple extensions can have pending tasks. | 139 // multiple extensions can have pending tasks. |
| 146 TEST_F(LazyBackgroundTaskQueueTest, AddPendingTask) { | 140 TEST_F(LazyBackgroundTaskQueueTest, AddPendingTask) { |
| 147 // Get our TestProcessManager. | 141 // Get our TestProcessManager. |
| 148 MockExtensionSystem* extension_system = static_cast<MockExtensionSystem*>( | 142 TestProcessManager* process_manager = static_cast<TestProcessManager*>( |
| 149 ExtensionSystem::Get(browser_context())); | 143 ProcessManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 150 TestProcessManager* process_manager = | 144 browser_context(), CreateTestProcessManager)); |
| 151 static_cast<TestProcessManager*>(extension_system->process_manager()); | |
| 152 | 145 |
| 153 LazyBackgroundTaskQueue queue(browser_context()); | 146 LazyBackgroundTaskQueue queue(browser_context()); |
| 154 | 147 |
| 155 // Build a simple extension with no background page. | 148 // Build a simple extension with no background page. |
| 156 scoped_refptr<Extension> no_background = CreateSimpleExtension(); | 149 scoped_refptr<Extension> no_background = CreateSimpleExtension(); |
| 157 | 150 |
| 158 // Adding a pending task increases the number of extensions with tasks, but | 151 // Adding a pending task increases the number of extensions with tasks, but |
| 159 // doesn't run the task. | 152 // doesn't run the task. |
| 160 queue.AddPendingTask(browser_context(), | 153 queue.AddPendingTask(browser_context(), |
| 161 no_background->id(), | 154 no_background->id(), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 EXPECT_EQ(1u, queue.extensions_with_pending_tasks()); | 204 EXPECT_EQ(1u, queue.extensions_with_pending_tasks()); |
| 212 | 205 |
| 213 // Processing tasks when there is one pending runs the task and removes the | 206 // Processing tasks when there is one pending runs the task and removes the |
| 214 // extension from the list of extensions with pending tasks. | 207 // extension from the list of extensions with pending tasks. |
| 215 queue.ProcessPendingTasks(NULL, browser_context(), extension.get()); | 208 queue.ProcessPendingTasks(NULL, browser_context(), extension.get()); |
| 216 EXPECT_EQ(1, task_run_count()); | 209 EXPECT_EQ(1, task_run_count()); |
| 217 EXPECT_EQ(0u, queue.extensions_with_pending_tasks()); | 210 EXPECT_EQ(0u, queue.extensions_with_pending_tasks()); |
| 218 } | 211 } |
| 219 | 212 |
| 220 } // namespace extensions | 213 } // namespace extensions |
| OLD | NEW |