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
3 // found in the LICENSE file.
5 #include "extensions/browser/lazy_background_task_queue.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "components/keyed_service/content/browser_context_dependency_manager.h"
11 #include "components/pref_registry/testing_pref_service_syncable.h"
12 #include "components/user_prefs/user_prefs.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/test/test_browser_context.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/extension_registry_factory.h"
17 #include "extensions/browser/extensions_test.h"
18 #include "extensions/browser/process_manager.h"
19 #include "extensions/browser/process_manager_factory.h"
20 #include "extensions/browser/test_extensions_browser_client.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/extension_builder.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using content::BrowserContext
;
27 namespace extensions
{
30 // A ProcessManager that doesn't create background host pages.
31 class TestProcessManager
: public ProcessManager
{
33 explicit TestProcessManager(BrowserContext
* context
)
34 : ProcessManager(context
, context
, ExtensionRegistry::Get(context
)),
36 // ProcessManager constructor above assumes non-incognito.
37 DCHECK(!context
->IsOffTheRecord());
39 ~TestProcessManager() override
{}
41 int create_count() { return create_count_
; }
43 // ProcessManager overrides:
44 bool CreateBackgroundHost(const Extension
* extension
,
45 const GURL
& url
) override
{
46 // Don't actually try to create a web contents.
54 DISALLOW_COPY_AND_ASSIGN(TestProcessManager
);
57 scoped_ptr
<KeyedService
> CreateTestProcessManager(BrowserContext
* context
) {
58 return make_scoped_ptr(new TestProcessManager(context
));
63 // Derives from ExtensionsTest to provide content module and keyed service
65 class LazyBackgroundTaskQueueTest
: public ExtensionsTest
{
67 LazyBackgroundTaskQueueTest()
68 : notification_service_(content::NotificationService::Create()),
71 ~LazyBackgroundTaskQueueTest() override
{}
73 int task_run_count() { return task_run_count_
; }
75 // A simple callback for AddPendingTask.
76 void RunPendingTask(ExtensionHost
* host
) {
80 // Creates and registers an extension without a background page.
81 scoped_refptr
<Extension
> CreateSimpleExtension() {
82 scoped_refptr
<Extension
> extension
= ExtensionBuilder()
83 .SetManifest(DictionaryBuilder()
84 .Set("name", "No background")
86 .Set("manifest_version", 2))
87 .SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
89 ExtensionRegistry::Get(browser_context())->AddEnabled(extension
);
93 // Creates and registers an extension with a lazy background page.
94 scoped_refptr
<Extension
> CreateLazyBackgroundExtension() {
95 scoped_refptr
<Extension
> extension
= ExtensionBuilder()
96 .SetManifest(DictionaryBuilder()
97 .Set("name", "Lazy background")
99 .Set("manifest_version", 2)
102 .Set("page", "background.html")
103 .SetBoolean("persistent", false)))
104 .SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
106 ExtensionRegistry::Get(browser_context())->AddEnabled(extension
);
111 void SetUp() override
{
112 user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_
);
116 scoped_ptr
<content::NotificationService
> notification_service_
;
118 user_prefs::TestingPrefServiceSyncable testing_pref_service_
;
120 // The total number of pending tasks that have been executed.
123 DISALLOW_COPY_AND_ASSIGN(LazyBackgroundTaskQueueTest
);
126 // Tests that only extensions with background pages should have tasks queued.
127 TEST_F(LazyBackgroundTaskQueueTest
, ShouldEnqueueTask
) {
128 LazyBackgroundTaskQueue
queue(browser_context());
130 // Build a simple extension with no background page.
131 scoped_refptr
<Extension
> no_background
= CreateSimpleExtension();
132 EXPECT_FALSE(queue
.ShouldEnqueueTask(browser_context(), no_background
.get()));
134 // Build another extension with a background page.
135 scoped_refptr
<Extension
> with_background
= CreateLazyBackgroundExtension();
137 queue
.ShouldEnqueueTask(browser_context(), with_background
.get()));
140 // Tests that adding tasks actually increases the pending task count, and that
141 // multiple extensions can have pending tasks.
142 TEST_F(LazyBackgroundTaskQueueTest
, AddPendingTask
) {
143 // Get our TestProcessManager.
144 TestProcessManager
* process_manager
= static_cast<TestProcessManager
*>(
145 ProcessManagerFactory::GetInstance()->SetTestingFactoryAndUse(
146 browser_context(), CreateTestProcessManager
));
148 LazyBackgroundTaskQueue
queue(browser_context());
150 // Build a simple extension with no background page.
151 scoped_refptr
<Extension
> no_background
= CreateSimpleExtension();
153 // Adding a pending task increases the number of extensions with tasks, but
154 // doesn't run the task.
155 queue
.AddPendingTask(browser_context(),
157 base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask
,
158 base::Unretained(this)));
159 EXPECT_EQ(1u, queue
.extensions_with_pending_tasks());
160 EXPECT_EQ(0, task_run_count());
162 // Another task on the same extension doesn't increase the number of
163 // extensions that have tasks and doesn't run any tasks.
164 queue
.AddPendingTask(browser_context(),
166 base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask
,
167 base::Unretained(this)));
168 EXPECT_EQ(1u, queue
.extensions_with_pending_tasks());
169 EXPECT_EQ(0, task_run_count());
171 // Adding a task on an extension with a lazy background page tries to create
172 // a background host, and if that fails, runs the task immediately.
173 scoped_refptr
<Extension
> lazy_background
= CreateLazyBackgroundExtension();
174 queue
.AddPendingTask(browser_context(),
175 lazy_background
->id(),
176 base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask
,
177 base::Unretained(this)));
178 EXPECT_EQ(2u, queue
.extensions_with_pending_tasks());
179 // The process manager tried to create a background host.
180 EXPECT_EQ(1, process_manager
->create_count());
181 // The task ran immediately because the creation failed.
182 EXPECT_EQ(1, task_run_count());
185 // Tests that pending tasks are actually run.
186 TEST_F(LazyBackgroundTaskQueueTest
, ProcessPendingTasks
) {
187 LazyBackgroundTaskQueue
queue(browser_context());
189 // ProcessPendingTasks is a no-op if there are no tasks.
190 scoped_refptr
<Extension
> extension
= CreateSimpleExtension();
191 queue
.ProcessPendingTasks(NULL
, browser_context(), extension
.get());
192 EXPECT_EQ(0, task_run_count());
194 // Schedule a task to run.
195 queue
.AddPendingTask(browser_context(),
197 base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask
,
198 base::Unretained(this)));
199 EXPECT_EQ(0, task_run_count());
200 EXPECT_EQ(1u, queue
.extensions_with_pending_tasks());
202 // Trying to run tasks for an unrelated BrowserContext should do nothing.
203 content::TestBrowserContext unrelated_context
;
204 queue
.ProcessPendingTasks(NULL
, &unrelated_context
, extension
.get());
205 EXPECT_EQ(0, task_run_count());
206 EXPECT_EQ(1u, queue
.extensions_with_pending_tasks());
208 // Processing tasks when there is one pending runs the task and removes the
209 // extension from the list of extensions with pending tasks.
210 queue
.ProcessPendingTasks(NULL
, browser_context(), extension
.get());
211 EXPECT_EQ(1, task_run_count());
212 EXPECT_EQ(0u, queue
.extensions_with_pending_tasks());
215 } // namespace extensions