- add sources.
[platform/framework/web/crosswalk.git] / src / extensions / browser / lazy_background_task_queue_unittest.cc
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.
4
5 #include "extensions/browser/lazy_background_task_queue.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "chrome/browser/extensions/extension_process_manager.h"
10 #include "chrome/browser/extensions/extension_service_unittest.h"
11 #include "chrome/browser/extensions/test_extension_system.h"
12 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_builder.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace extensions {
19
20 // An ExtensionProcessManager that doesn't create background host pages.
21 class TestExtensionProcessManager : public ExtensionProcessManager {
22  public:
23   explicit TestExtensionProcessManager(Profile* profile)
24       : ExtensionProcessManager(profile),
25         create_count_(0) {}
26   virtual ~TestExtensionProcessManager() {}
27
28   int create_count() { return create_count_; }
29
30   // ExtensionProcessManager overrides:
31   virtual extensions::ExtensionHost* CreateBackgroundHost(
32       const extensions::Extension* extension,
33       const GURL& url) OVERRIDE {
34     // Don't actually try to create a web contents.
35     create_count_++;
36     return NULL;
37   }
38
39  private:
40   int create_count_;
41
42   DISALLOW_COPY_AND_ASSIGN(TestExtensionProcessManager);
43 };
44
45 // Derives from ExtensionServiceTestBase because ExtensionService is difficult
46 // to initialize alone.
47 class LazyBackgroundTaskQueueTest : public ExtensionServiceTestBase {
48  public:
49   LazyBackgroundTaskQueueTest() : task_run_count_(0) {}
50   virtual ~LazyBackgroundTaskQueueTest() {}
51
52   int task_run_count() { return task_run_count_; }
53
54   // A simple callback for AddPendingTask.
55   void RunPendingTask(ExtensionHost* host) {
56     task_run_count_++;
57   }
58
59   // Creates and registers an extension without a background page.
60   scoped_refptr<Extension> CreateSimpleExtension() {
61     scoped_refptr<Extension> extension = ExtensionBuilder()
62         .SetManifest(DictionaryBuilder()
63                      .Set("name", "No background")
64                      .Set("version", "1")
65                      .Set("manifest_version", 2))
66         .SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
67         .Build();
68     service_->AddExtension(extension);
69     return extension;
70   }
71
72   // Creates and registers an extension with a lazy background page.
73   scoped_refptr<Extension> CreateLazyBackgroundExtension() {
74     scoped_refptr<Extension> extension = ExtensionBuilder()
75         .SetManifest(DictionaryBuilder()
76             .Set("name", "Lazy background")
77             .Set("version", "1")
78             .Set("manifest_version", 2)
79             .Set("background",
80                   DictionaryBuilder()
81                   .Set("page", "background.html")
82                   .SetBoolean("persistent", false)))
83         .SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
84         .Build();
85     service_->AddExtension(extension);
86     return extension;
87   }
88
89  private:
90   // The total number of pending tasks that have been executed.
91   int task_run_count_;
92
93   DISALLOW_COPY_AND_ASSIGN(LazyBackgroundTaskQueueTest);
94 };
95
96 // Tests that only extensions with background pages should have tasks queued.
97 TEST_F(LazyBackgroundTaskQueueTest, ShouldEnqueueTask) {
98   InitializeEmptyExtensionService();
99   InitializeExtensionProcessManager();
100
101   LazyBackgroundTaskQueue queue(profile_.get());
102
103   // Build a simple extension with no background page.
104   scoped_refptr<Extension> no_background = CreateSimpleExtension();
105   EXPECT_FALSE(queue.ShouldEnqueueTask(profile_.get(), no_background.get()));
106
107   // Build another extension with a background page.
108   scoped_refptr<Extension> with_background = CreateLazyBackgroundExtension();
109   EXPECT_TRUE(queue.ShouldEnqueueTask(profile_.get(), with_background.get()));
110 }
111
112 // Tests that adding tasks actually increases the pending task count, and that
113 // multiple extensions can have pending tasks.
114 TEST_F(LazyBackgroundTaskQueueTest, AddPendingTask) {
115   InitializeEmptyExtensionService();
116
117   // Swap in our stub TestExtensionProcessManager.
118   TestExtensionSystem* extension_system =
119       static_cast<extensions::TestExtensionSystem*>(
120           ExtensionSystem::Get(profile_.get()));
121   // Owned by |extension_system|.
122   TestExtensionProcessManager* process_manager =
123       new TestExtensionProcessManager(profile_.get());
124   extension_system->SetExtensionProcessManager(process_manager);
125
126   LazyBackgroundTaskQueue queue(profile_.get());
127
128   // Build a simple extension with no background page.
129   scoped_refptr<Extension> no_background = CreateSimpleExtension();
130
131   // Adding a pending task increases the number of extensions with tasks, but
132   // doesn't run the task.
133   queue.AddPendingTask(profile_.get(),
134                        no_background->id(),
135                        base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
136                                   base::Unretained(this)));
137   EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
138   EXPECT_EQ(0, task_run_count());
139
140   // Another task on the same extension doesn't increase the number of
141   // extensions that have tasks and doesn't run any tasks.
142   queue.AddPendingTask(profile_.get(),
143                        no_background->id(),
144                        base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
145                                   base::Unretained(this)));
146   EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
147   EXPECT_EQ(0, task_run_count());
148
149   // Adding a task on an extension with a lazy background page tries to create
150   // a background host, and if that fails, runs the task immediately.
151   scoped_refptr<Extension> lazy_background = CreateLazyBackgroundExtension();
152   queue.AddPendingTask(profile_.get(),
153                        lazy_background->id(),
154                        base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
155                                   base::Unretained(this)));
156   EXPECT_EQ(2u, queue.extensions_with_pending_tasks());
157   // The process manager tried to create a background host.
158   EXPECT_EQ(1, process_manager->create_count());
159   // The task ran immediately because the creation failed.
160   EXPECT_EQ(1, task_run_count());
161 }
162
163 // Tests that pending tasks are actually run.
164 TEST_F(LazyBackgroundTaskQueueTest, ProcessPendingTasks) {
165   InitializeEmptyExtensionService();
166
167   LazyBackgroundTaskQueue queue(profile_.get());
168
169   // ProcessPendingTasks is a no-op if there are no tasks.
170   scoped_refptr<Extension> extension = CreateSimpleExtension();
171   queue.ProcessPendingTasks(NULL, profile_.get(), extension);
172   EXPECT_EQ(0, task_run_count());
173
174   // Schedule a task to run.
175   queue.AddPendingTask(profile_.get(),
176                        extension->id(),
177                        base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
178                                   base::Unretained(this)));
179   EXPECT_EQ(0, task_run_count());
180   EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
181
182   // Trying to run tasks for an unrelated profile should do nothing.
183   TestingProfile profile2;
184   queue.ProcessPendingTasks(NULL, &profile2, extension);
185   EXPECT_EQ(0, task_run_count());
186   EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
187
188   // Processing tasks when there is one pending runs the task and removes the
189   // extension from the list of extensions with pending tasks.
190   queue.ProcessPendingTasks(NULL, profile_.get(), extension);
191   EXPECT_EQ(1, task_run_count());
192   EXPECT_EQ(0u, queue.extensions_with_pending_tasks());
193 }
194
195 }  // namespace extensions