Commit e45791c3 authored by Devlin Cronin's avatar Devlin Cronin Committed by Commit Bot

[Extensions] Add ExtensionBuilder::SetBackgroundPage()

Setting a background page is a pretty common operation in test
extensions. Bake it into ExtensionBuilder.

Use it in a small smattering of places, and add a unittest.

Bug: 832958
Change-Id: Iaa3464dfa183f990fb579d289f99271983344983
Reviewed-on: https://chromium-review.googlesource.com/1013128Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550982}
parent 16cdbd08
...@@ -113,15 +113,9 @@ TEST_F(ExtensionHooksDelegateTest, MessagingSanityChecks) { ...@@ -113,15 +113,9 @@ TEST_F(ExtensionHooksDelegateTest, MessagingSanityChecks) {
TEST_F(ExtensionHooksDelegateTest, SendRequestDisabled) { TEST_F(ExtensionHooksDelegateTest, SendRequestDisabled) {
// Construct an extension for which sendRequest is disabled (unpacked // Construct an extension for which sendRequest is disabled (unpacked
// extension with an event page). // extension with an event page).
// TODO(devlin): Add a SetBackgroundPage() to ExtensionBuilder?
scoped_refptr<Extension> extension = scoped_refptr<Extension> extension =
ExtensionBuilder("foo") ExtensionBuilder("foo")
.MergeManifest(DictionaryBuilder() .SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT)
.Set("background", DictionaryBuilder()
.Set("persistent", false)
.Set("page", "page.html")
.Build())
.Build())
.SetLocation(Manifest::UNPACKED) .SetLocation(Manifest::UNPACKED)
.Build(); .Build();
RegisterExtension(extension); RegisterExtension(extension);
......
...@@ -79,12 +79,7 @@ class LazyBackgroundTaskQueueTest : public ExtensionsTest { ...@@ -79,12 +79,7 @@ class LazyBackgroundTaskQueueTest : public ExtensionsTest {
// Creates and registers an extension without a background page. // Creates and registers an extension without a background page.
scoped_refptr<Extension> CreateSimpleExtension() { scoped_refptr<Extension> CreateSimpleExtension() {
scoped_refptr<Extension> extension = scoped_refptr<Extension> extension =
ExtensionBuilder() ExtensionBuilder("No background")
.SetManifest(DictionaryBuilder()
.Set("name", "No background")
.Set("version", "1")
.Set("manifest_version", 2)
.Build())
.SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") .SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
.Build(); .Build();
ExtensionRegistry::Get(browser_context())->AddEnabled(extension); ExtensionRegistry::Get(browser_context())->AddEnabled(extension);
...@@ -94,17 +89,8 @@ class LazyBackgroundTaskQueueTest : public ExtensionsTest { ...@@ -94,17 +89,8 @@ class LazyBackgroundTaskQueueTest : public ExtensionsTest {
// Creates and registers an extension with a lazy background page. // Creates and registers an extension with a lazy background page.
scoped_refptr<Extension> CreateLazyBackgroundExtension() { scoped_refptr<Extension> CreateLazyBackgroundExtension() {
scoped_refptr<Extension> extension = scoped_refptr<Extension> extension =
ExtensionBuilder() ExtensionBuilder("Lazy background")
.SetManifest( .SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT)
DictionaryBuilder()
.Set("name", "Lazy background")
.Set("version", "1")
.Set("manifest_version", 2)
.Set("background", DictionaryBuilder()
.Set("page", "background.html")
.Set("persistent", false)
.Build())
.Build())
.SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") .SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
.Build(); .Build();
ExtensionRegistry::Get(browser_context())->AddEnabled(extension); ExtensionRegistry::Get(browser_context())->AddEnabled(extension);
......
...@@ -20,11 +20,7 @@ namespace { ...@@ -20,11 +20,7 @@ namespace {
// Creates a very simple extension with a background page. // Creates a very simple extension with a background page.
scoped_refptr<Extension> CreateExtensionWithBackgroundPage() { scoped_refptr<Extension> CreateExtensionWithBackgroundPage() {
return ExtensionBuilder("test") return ExtensionBuilder("test")
.MergeManifest( .SetBackgroundPage(ExtensionBuilder::BackgroundPage::PERSISTENT)
DictionaryBuilder()
.Set("background",
DictionaryBuilder().Set("page", "bg.html").Build())
.Build())
.SetID("id2") .SetID("id2")
.Build(); .Build();
} }
......
...@@ -18,6 +18,7 @@ struct ExtensionBuilder::ManifestData { ...@@ -18,6 +18,7 @@ struct ExtensionBuilder::ManifestData {
std::string name; std::string name;
std::vector<std::string> permissions; std::vector<std::string> permissions;
base::Optional<ActionType> action; base::Optional<ActionType> action;
base::Optional<BackgroundPage> background_page;
std::unique_ptr<base::DictionaryValue> extra; std::unique_ptr<base::DictionaryValue> extra;
std::unique_ptr<base::DictionaryValue> GetValue() const { std::unique_ptr<base::DictionaryValue> GetValue() const {
...@@ -60,6 +61,22 @@ struct ExtensionBuilder::ManifestData { ...@@ -60,6 +61,22 @@ struct ExtensionBuilder::ManifestData {
manifest.Set(action_key, std::make_unique<base::DictionaryValue>()); manifest.Set(action_key, std::make_unique<base::DictionaryValue>());
} }
if (background_page) {
DictionaryBuilder background;
background.Set("page", "background_page.html");
bool persistent = false;
switch (*background_page) {
case BackgroundPage::PERSISTENT:
persistent = true;
break;
case BackgroundPage::EVENT:
persistent = false;
break;
}
background.Set("persistent", persistent);
manifest.Set("background", background.Build());
}
std::unique_ptr<base::DictionaryValue> result = manifest.Build(); std::unique_ptr<base::DictionaryValue> result = manifest.Build();
if (extra) if (extra)
result->MergeDictionary(extra.get()); result->MergeDictionary(extra.get());
...@@ -123,6 +140,13 @@ ExtensionBuilder& ExtensionBuilder::SetAction(ActionType action) { ...@@ -123,6 +140,13 @@ ExtensionBuilder& ExtensionBuilder::SetAction(ActionType action) {
return *this; return *this;
} }
ExtensionBuilder& ExtensionBuilder::SetBackgroundPage(
BackgroundPage background_page) {
CHECK(manifest_data_);
manifest_data_->background_page = background_page;
return *this;
}
ExtensionBuilder& ExtensionBuilder::SetPath(const base::FilePath& path) { ExtensionBuilder& ExtensionBuilder::SetPath(const base::FilePath& path) {
path_ = path; path_ = path;
return *this; return *this;
......
...@@ -48,6 +48,11 @@ class ExtensionBuilder { ...@@ -48,6 +48,11 @@ class ExtensionBuilder {
BROWSER_ACTION, BROWSER_ACTION,
}; };
enum class BackgroundPage {
PERSISTENT,
EVENT,
};
// Initializes an ExtensionBuilder that can be used with SetManifest() for // Initializes an ExtensionBuilder that can be used with SetManifest() for
// complete customization. // complete customization.
ExtensionBuilder(); ExtensionBuilder();
...@@ -78,6 +83,10 @@ class ExtensionBuilder { ...@@ -78,6 +83,10 @@ class ExtensionBuilder {
// be set (though note that we synthesize a page action for most extensions). // be set (though note that we synthesize a page action for most extensions).
ExtensionBuilder& SetAction(ActionType action); ExtensionBuilder& SetAction(ActionType action);
// Sets a background page for the extension to have. By default, no background
// page will be set.
ExtensionBuilder& SetBackgroundPage(BackgroundPage background_page);
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// Utility methods for use with custom manifest construction. // Utility methods for use with custom manifest construction.
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/version.h" #include "base/version.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h" #include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/externally_connectable.h" #include "extensions/common/manifest_handlers/externally_connectable.h"
#include "extensions/common/permissions/permissions_data.h" #include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/value_builder.h" #include "extensions/common/value_builder.h"
...@@ -77,6 +78,30 @@ TEST(ExtensionBuilderTest, Actions) { ...@@ -77,6 +78,30 @@ TEST(ExtensionBuilderTest, Actions) {
} }
} }
TEST(ExtensionBuilderTest, Background) {
{
scoped_refptr<const Extension> extension =
ExtensionBuilder("no background").Build();
EXPECT_FALSE(BackgroundInfo::HasBackgroundPage(extension.get()));
}
{
scoped_refptr<const Extension> extension =
ExtensionBuilder("persistent background page")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::PERSISTENT)
.Build();
EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension.get()));
EXPECT_TRUE(BackgroundInfo::HasPersistentBackgroundPage(extension.get()));
}
{
scoped_refptr<const Extension> extension =
ExtensionBuilder("event page")
.SetBackgroundPage(ExtensionBuilder::BackgroundPage::EVENT)
.Build();
EXPECT_TRUE(BackgroundInfo::HasBackgroundPage(extension.get()));
EXPECT_TRUE(BackgroundInfo::HasLazyBackgroundPage(extension.get()));
}
}
TEST(ExtensionBuilderTest, MergeManifest) { TEST(ExtensionBuilderTest, MergeManifest) {
DictionaryBuilder connectable; DictionaryBuilder connectable;
connectable.Set("matches", ListBuilder().Append("*://example.com/*").Build()); connectable.Set("matches", ListBuilder().Append("*://example.com/*").Build());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment