[Smart Lock] Record a detailed UMA metric for each unlock attempt by Smart Lock users.
[chromium-blink-merge.git] / extensions / browser / lazy_background_task_queue_unittest.cc
blobdbe07946fd6645588560de95700ff943144d1512
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"
7 #include "base/bind.h"
8 #include "base/prefs/testing_pref_service.h"
9 #include "components/keyed_service/content/browser_context_dependency_manager.h"
10 #include "components/user_prefs/user_prefs.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/test/test_browser_context.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/extension_registry_factory.h"
15 #include "extensions/browser/extensions_test.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"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_builder.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using content::BrowserContext;
25 namespace extensions {
26 namespace {
28 // A ProcessManager that doesn't create background host pages.
29 class TestProcessManager : public ProcessManager {
30 public:
31 explicit TestProcessManager(BrowserContext* context)
32 : ProcessManager(context, context, ExtensionRegistry::Get(context)),
33 create_count_(0) {
34 // ProcessManager constructor above assumes non-incognito.
35 DCHECK(!context->IsOffTheRecord());
37 ~TestProcessManager() override {}
39 int create_count() { return create_count_; }
41 // ProcessManager overrides:
42 bool CreateBackgroundHost(const Extension* extension,
43 const GURL& url) override {
44 // Don't actually try to create a web contents.
45 create_count_++;
46 return false;
49 private:
50 int create_count_;
52 DISALLOW_COPY_AND_ASSIGN(TestProcessManager);
55 KeyedService* CreateTestProcessManager(BrowserContext* context) {
56 return new TestProcessManager(context);
59 } // namespace
61 // Derives from ExtensionsTest to provide content module and keyed service
62 // initialization.
63 class LazyBackgroundTaskQueueTest : public ExtensionsTest {
64 public:
65 LazyBackgroundTaskQueueTest()
66 : notification_service_(content::NotificationService::Create()),
67 task_run_count_(0) {
69 ~LazyBackgroundTaskQueueTest() override {}
71 int task_run_count() { return task_run_count_; }
73 // A simple callback for AddPendingTask.
74 void RunPendingTask(ExtensionHost* host) {
75 task_run_count_++;
78 // Creates and registers an extension without a background page.
79 scoped_refptr<Extension> CreateSimpleExtension() {
80 scoped_refptr<Extension> extension = ExtensionBuilder()
81 .SetManifest(DictionaryBuilder()
82 .Set("name", "No background")
83 .Set("version", "1")
84 .Set("manifest_version", 2))
85 .SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
86 .Build();
87 ExtensionRegistry::Get(browser_context())->AddEnabled(extension);
88 return extension;
91 // Creates and registers an extension with a lazy background page.
92 scoped_refptr<Extension> CreateLazyBackgroundExtension() {
93 scoped_refptr<Extension> extension = ExtensionBuilder()
94 .SetManifest(DictionaryBuilder()
95 .Set("name", "Lazy background")
96 .Set("version", "1")
97 .Set("manifest_version", 2)
98 .Set("background",
99 DictionaryBuilder()
100 .Set("page", "background.html")
101 .SetBoolean("persistent", false)))
102 .SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
103 .Build();
104 ExtensionRegistry::Get(browser_context())->AddEnabled(extension);
105 return extension;
108 protected:
109 void SetUp() override {
110 user_prefs::UserPrefs::Set(browser_context(), &testing_pref_service_);
113 private:
114 scoped_ptr<content::NotificationService> notification_service_;
116 TestingPrefServiceSimple testing_pref_service_;
118 // The total number of pending tasks that have been executed.
119 int task_run_count_;
121 DISALLOW_COPY_AND_ASSIGN(LazyBackgroundTaskQueueTest);
124 // Tests that only extensions with background pages should have tasks queued.
125 TEST_F(LazyBackgroundTaskQueueTest, ShouldEnqueueTask) {
126 LazyBackgroundTaskQueue queue(browser_context());
128 // Build a simple extension with no background page.
129 scoped_refptr<Extension> no_background = CreateSimpleExtension();
130 EXPECT_FALSE(queue.ShouldEnqueueTask(browser_context(), no_background.get()));
132 // Build another extension with a background page.
133 scoped_refptr<Extension> with_background = CreateLazyBackgroundExtension();
134 EXPECT_TRUE(
135 queue.ShouldEnqueueTask(browser_context(), with_background.get()));
138 // Tests that adding tasks actually increases the pending task count, and that
139 // multiple extensions can have pending tasks.
140 TEST_F(LazyBackgroundTaskQueueTest, AddPendingTask) {
141 // Get our TestProcessManager.
142 TestProcessManager* process_manager = static_cast<TestProcessManager*>(
143 ProcessManagerFactory::GetInstance()->SetTestingFactoryAndUse(
144 browser_context(), CreateTestProcessManager));
146 LazyBackgroundTaskQueue queue(browser_context());
148 // Build a simple extension with no background page.
149 scoped_refptr<Extension> no_background = CreateSimpleExtension();
151 // Adding a pending task increases the number of extensions with tasks, but
152 // doesn't run the task.
153 queue.AddPendingTask(browser_context(),
154 no_background->id(),
155 base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
156 base::Unretained(this)));
157 EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
158 EXPECT_EQ(0, task_run_count());
160 // Another task on the same extension doesn't increase the number of
161 // extensions that have tasks and doesn't run any tasks.
162 queue.AddPendingTask(browser_context(),
163 no_background->id(),
164 base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
165 base::Unretained(this)));
166 EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
167 EXPECT_EQ(0, task_run_count());
169 // Adding a task on an extension with a lazy background page tries to create
170 // a background host, and if that fails, runs the task immediately.
171 scoped_refptr<Extension> lazy_background = CreateLazyBackgroundExtension();
172 queue.AddPendingTask(browser_context(),
173 lazy_background->id(),
174 base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
175 base::Unretained(this)));
176 EXPECT_EQ(2u, queue.extensions_with_pending_tasks());
177 // The process manager tried to create a background host.
178 EXPECT_EQ(1, process_manager->create_count());
179 // The task ran immediately because the creation failed.
180 EXPECT_EQ(1, task_run_count());
183 // Tests that pending tasks are actually run.
184 TEST_F(LazyBackgroundTaskQueueTest, ProcessPendingTasks) {
185 LazyBackgroundTaskQueue queue(browser_context());
187 // ProcessPendingTasks is a no-op if there are no tasks.
188 scoped_refptr<Extension> extension = CreateSimpleExtension();
189 queue.ProcessPendingTasks(NULL, browser_context(), extension.get());
190 EXPECT_EQ(0, task_run_count());
192 // Schedule a task to run.
193 queue.AddPendingTask(browser_context(),
194 extension->id(),
195 base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
196 base::Unretained(this)));
197 EXPECT_EQ(0, task_run_count());
198 EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
200 // Trying to run tasks for an unrelated BrowserContext should do nothing.
201 content::TestBrowserContext unrelated_context;
202 queue.ProcessPendingTasks(NULL, &unrelated_context, extension.get());
203 EXPECT_EQ(0, task_run_count());
204 EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
206 // Processing tasks when there is one pending runs the task and removes the
207 // extension from the list of extensions with pending tasks.
208 queue.ProcessPendingTasks(NULL, browser_context(), extension.get());
209 EXPECT_EQ(1, task_run_count());
210 EXPECT_EQ(0u, queue.extensions_with_pending_tasks());
213 } // namespace extensions