This source file includes following definitions.
- create_count_
- create_count
- CreateBackgroundHost
- task_run_count
- RunPendingTask
- CreateSimpleExtension
- CreateLazyBackgroundExtension
- TEST_F
- TEST_F
- TEST_F
#include "extensions/browser/lazy_background_task_queue.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "chrome/browser/extensions/extension_service_unittest.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
class TestProcessManager : public ProcessManager {
public:
explicit TestProcessManager(Profile* profile)
: ProcessManager(profile, profile->GetOriginalProfile()),
create_count_(0) {}
virtual ~TestProcessManager() {}
int create_count() { return create_count_; }
virtual bool CreateBackgroundHost(const Extension* extension,
const GURL& url) OVERRIDE {
create_count_++;
return false;
}
private:
int create_count_;
DISALLOW_COPY_AND_ASSIGN(TestProcessManager);
};
class LazyBackgroundTaskQueueTest : public ExtensionServiceTestBase {
public:
LazyBackgroundTaskQueueTest() : task_run_count_(0) {}
virtual ~LazyBackgroundTaskQueueTest() {}
int task_run_count() { return task_run_count_; }
void RunPendingTask(ExtensionHost* host) {
task_run_count_++;
}
scoped_refptr<Extension> CreateSimpleExtension() {
scoped_refptr<Extension> extension = ExtensionBuilder()
.SetManifest(DictionaryBuilder()
.Set("name", "No background")
.Set("version", "1")
.Set("manifest_version", 2))
.SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
.Build();
service_->AddExtension(extension);
return extension;
}
scoped_refptr<Extension> CreateLazyBackgroundExtension() {
scoped_refptr<Extension> extension = ExtensionBuilder()
.SetManifest(DictionaryBuilder()
.Set("name", "Lazy background")
.Set("version", "1")
.Set("manifest_version", 2)
.Set("background",
DictionaryBuilder()
.Set("page", "background.html")
.SetBoolean("persistent", false)))
.SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
.Build();
service_->AddExtension(extension);
return extension;
}
private:
int task_run_count_;
DISALLOW_COPY_AND_ASSIGN(LazyBackgroundTaskQueueTest);
};
TEST_F(LazyBackgroundTaskQueueTest, ShouldEnqueueTask) {
InitializeEmptyExtensionService();
InitializeProcessManager();
LazyBackgroundTaskQueue queue(profile_.get());
scoped_refptr<Extension> no_background = CreateSimpleExtension();
EXPECT_FALSE(queue.ShouldEnqueueTask(profile_.get(), no_background.get()));
scoped_refptr<Extension> with_background = CreateLazyBackgroundExtension();
EXPECT_TRUE(queue.ShouldEnqueueTask(profile_.get(), with_background.get()));
}
TEST_F(LazyBackgroundTaskQueueTest, AddPendingTask) {
InitializeEmptyExtensionService();
TestExtensionSystem* extension_system =
static_cast<extensions::TestExtensionSystem*>(
ExtensionSystem::Get(profile_.get()));
TestProcessManager* process_manager = new TestProcessManager(profile_.get());
extension_system->SetProcessManager(process_manager);
LazyBackgroundTaskQueue queue(profile_.get());
scoped_refptr<Extension> no_background = CreateSimpleExtension();
queue.AddPendingTask(profile_.get(),
no_background->id(),
base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
base::Unretained(this)));
EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
EXPECT_EQ(0, task_run_count());
queue.AddPendingTask(profile_.get(),
no_background->id(),
base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
base::Unretained(this)));
EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
EXPECT_EQ(0, task_run_count());
scoped_refptr<Extension> lazy_background = CreateLazyBackgroundExtension();
queue.AddPendingTask(profile_.get(),
lazy_background->id(),
base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
base::Unretained(this)));
EXPECT_EQ(2u, queue.extensions_with_pending_tasks());
EXPECT_EQ(1, process_manager->create_count());
EXPECT_EQ(1, task_run_count());
}
TEST_F(LazyBackgroundTaskQueueTest, ProcessPendingTasks) {
InitializeEmptyExtensionService();
LazyBackgroundTaskQueue queue(profile_.get());
scoped_refptr<Extension> extension = CreateSimpleExtension();
queue.ProcessPendingTasks(NULL, profile_.get(), extension);
EXPECT_EQ(0, task_run_count());
queue.AddPendingTask(profile_.get(),
extension->id(),
base::Bind(&LazyBackgroundTaskQueueTest::RunPendingTask,
base::Unretained(this)));
EXPECT_EQ(0, task_run_count());
EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
TestingProfile profile2;
queue.ProcessPendingTasks(NULL, &profile2, extension);
EXPECT_EQ(0, task_run_count());
EXPECT_EQ(1u, queue.extensions_with_pending_tasks());
queue.ProcessPendingTasks(NULL, profile_.get(), extension);
EXPECT_EQ(1, task_run_count());
EXPECT_EQ(0u, queue.extensions_with_pending_tasks());
}
}