| 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/command_line.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" | 9 #include "chrome/browser/extensions/extension_service_unittest.h" |
| 11 #include "chrome/browser/extensions/test_extension_system.h" | 10 #include "chrome/browser/extensions/test_extension_system.h" |
| 12 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "extensions/browser/process_manager.h" |
| 15 #include "extensions/common/extension_builder.h" | 15 #include "extensions/common/extension_builder.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 // An ExtensionProcessManager that doesn't create background host pages. | 20 // A ProcessManager that doesn't create background host pages. |
| 21 class TestExtensionProcessManager : public ExtensionProcessManager { | 21 class TestProcessManager : public ProcessManager { |
| 22 public: | 22 public: |
| 23 explicit TestExtensionProcessManager(Profile* profile) | 23 explicit TestProcessManager(Profile* profile) |
| 24 : ExtensionProcessManager(profile, profile->GetOriginalProfile()), | 24 : ProcessManager(profile, profile->GetOriginalProfile()), |
| 25 create_count_(0) {} | 25 create_count_(0) {} |
| 26 virtual ~TestExtensionProcessManager() {} | 26 virtual ~TestProcessManager() {} |
| 27 | 27 |
| 28 int create_count() { return create_count_; } | 28 int create_count() { return create_count_; } |
| 29 | 29 |
| 30 // ExtensionProcessManager overrides: | 30 // ProcessManager overrides: |
| 31 virtual extensions::ExtensionHost* CreateBackgroundHost( | 31 virtual ExtensionHost* CreateBackgroundHost(const Extension* extension, |
| 32 const extensions::Extension* extension, | 32 const GURL& url) OVERRIDE { |
| 33 const GURL& url) OVERRIDE { | |
| 34 // Don't actually try to create a web contents. | 33 // Don't actually try to create a web contents. |
| 35 create_count_++; | 34 create_count_++; |
| 36 return NULL; | 35 return NULL; |
| 37 } | 36 } |
| 38 | 37 |
| 39 private: | 38 private: |
| 40 int create_count_; | 39 int create_count_; |
| 41 | 40 |
| 42 DISALLOW_COPY_AND_ASSIGN(TestExtensionProcessManager); | 41 DISALLOW_COPY_AND_ASSIGN(TestProcessManager); |
| 43 }; | 42 }; |
| 44 | 43 |
| 45 // Derives from ExtensionServiceTestBase because ExtensionService is difficult | 44 // Derives from ExtensionServiceTestBase because ExtensionService is difficult |
| 46 // to initialize alone. | 45 // to initialize alone. |
| 47 class LazyBackgroundTaskQueueTest : public ExtensionServiceTestBase { | 46 class LazyBackgroundTaskQueueTest : public ExtensionServiceTestBase { |
| 48 public: | 47 public: |
| 49 LazyBackgroundTaskQueueTest() : task_run_count_(0) {} | 48 LazyBackgroundTaskQueueTest() : task_run_count_(0) {} |
| 50 virtual ~LazyBackgroundTaskQueueTest() {} | 49 virtual ~LazyBackgroundTaskQueueTest() {} |
| 51 | 50 |
| 52 int task_run_count() { return task_run_count_; } | 51 int task_run_count() { return task_run_count_; } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 private: | 88 private: |
| 90 // The total number of pending tasks that have been executed. | 89 // The total number of pending tasks that have been executed. |
| 91 int task_run_count_; | 90 int task_run_count_; |
| 92 | 91 |
| 93 DISALLOW_COPY_AND_ASSIGN(LazyBackgroundTaskQueueTest); | 92 DISALLOW_COPY_AND_ASSIGN(LazyBackgroundTaskQueueTest); |
| 94 }; | 93 }; |
| 95 | 94 |
| 96 // Tests that only extensions with background pages should have tasks queued. | 95 // Tests that only extensions with background pages should have tasks queued. |
| 97 TEST_F(LazyBackgroundTaskQueueTest, ShouldEnqueueTask) { | 96 TEST_F(LazyBackgroundTaskQueueTest, ShouldEnqueueTask) { |
| 98 InitializeEmptyExtensionService(); | 97 InitializeEmptyExtensionService(); |
| 99 InitializeExtensionProcessManager(); | 98 InitializeProcessManager(); |
| 100 | 99 |
| 101 LazyBackgroundTaskQueue queue(profile_.get()); | 100 LazyBackgroundTaskQueue queue(profile_.get()); |
| 102 | 101 |
| 103 // Build a simple extension with no background page. | 102 // Build a simple extension with no background page. |
| 104 scoped_refptr<Extension> no_background = CreateSimpleExtension(); | 103 scoped_refptr<Extension> no_background = CreateSimpleExtension(); |
| 105 EXPECT_FALSE(queue.ShouldEnqueueTask(profile_.get(), no_background.get())); | 104 EXPECT_FALSE(queue.ShouldEnqueueTask(profile_.get(), no_background.get())); |
| 106 | 105 |
| 107 // Build another extension with a background page. | 106 // Build another extension with a background page. |
| 108 scoped_refptr<Extension> with_background = CreateLazyBackgroundExtension(); | 107 scoped_refptr<Extension> with_background = CreateLazyBackgroundExtension(); |
| 109 EXPECT_TRUE(queue.ShouldEnqueueTask(profile_.get(), with_background.get())); | 108 EXPECT_TRUE(queue.ShouldEnqueueTask(profile_.get(), with_background.get())); |
| 110 } | 109 } |
| 111 | 110 |
| 112 // Tests that adding tasks actually increases the pending task count, and that | 111 // Tests that adding tasks actually increases the pending task count, and that |
| 113 // multiple extensions can have pending tasks. | 112 // multiple extensions can have pending tasks. |
| 114 TEST_F(LazyBackgroundTaskQueueTest, AddPendingTask) { | 113 TEST_F(LazyBackgroundTaskQueueTest, AddPendingTask) { |
| 115 InitializeEmptyExtensionService(); | 114 InitializeEmptyExtensionService(); |
| 116 | 115 |
| 117 // Swap in our stub TestExtensionProcessManager. | 116 // Swap in our stub TestProcessManager. |
| 118 TestExtensionSystem* extension_system = | 117 TestExtensionSystem* extension_system = |
| 119 static_cast<extensions::TestExtensionSystem*>( | 118 static_cast<extensions::TestExtensionSystem*>( |
| 120 ExtensionSystem::Get(profile_.get())); | 119 ExtensionSystem::Get(profile_.get())); |
| 121 // Owned by |extension_system|. | 120 // Owned by |extension_system|. |
| 122 TestExtensionProcessManager* process_manager = | 121 TestProcessManager* process_manager = new TestProcessManager(profile_.get()); |
| 123 new TestExtensionProcessManager(profile_.get()); | 122 extension_system->SetProcessManager(process_manager); |
| 124 extension_system->SetExtensionProcessManager(process_manager); | |
| 125 | 123 |
| 126 LazyBackgroundTaskQueue queue(profile_.get()); | 124 LazyBackgroundTaskQueue queue(profile_.get()); |
| 127 | 125 |
| 128 // Build a simple extension with no background page. | 126 // Build a simple extension with no background page. |
| 129 scoped_refptr<Extension> no_background = CreateSimpleExtension(); | 127 scoped_refptr<Extension> no_background = CreateSimpleExtension(); |
| 130 | 128 |
| 131 // Adding a pending task increases the number of extensions with tasks, but | 129 // Adding a pending task increases the number of extensions with tasks, but |
| 132 // doesn't run the task. | 130 // doesn't run the task. |
| 133 queue.AddPendingTask(profile_.get(), | 131 queue.AddPendingTask(profile_.get(), |
| 134 no_background->id(), | 132 no_background->id(), |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 EXPECT_EQ(1u, queue.extensions_with_pending_tasks()); | 184 EXPECT_EQ(1u, queue.extensions_with_pending_tasks()); |
| 187 | 185 |
| 188 // Processing tasks when there is one pending runs the task and removes the | 186 // Processing tasks when there is one pending runs the task and removes the |
| 189 // extension from the list of extensions with pending tasks. | 187 // extension from the list of extensions with pending tasks. |
| 190 queue.ProcessPendingTasks(NULL, profile_.get(), extension); | 188 queue.ProcessPendingTasks(NULL, profile_.get(), extension); |
| 191 EXPECT_EQ(1, task_run_count()); | 189 EXPECT_EQ(1, task_run_count()); |
| 192 EXPECT_EQ(0u, queue.extensions_with_pending_tasks()); | 190 EXPECT_EQ(0u, queue.extensions_with_pending_tasks()); |
| 193 } | 191 } |
| 194 | 192 |
| 195 } // namespace extensions | 193 } // namespace extensions |
| OLD | NEW |