| Index: chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc
|
| diff --git a/chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc b/chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc
|
| index a6ba50bda5276c45bb7172e39bbeb1e0e4780f3d..be925c1101bc5adb013d2375740851afed9105f6 100644
|
| --- a/chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc
|
| +++ b/chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc
|
| @@ -33,6 +33,15 @@ class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase {
|
| bool RunFunction(const scoped_refptr<UIThreadExtensionFunction>& function,
|
| const base::ListValue& args);
|
|
|
| + // Loads an unpacked extension that is backed by a real directory, allowing
|
| + // it to be reloaded.
|
| + const Extension* LoadUnpackedExtension();
|
| +
|
| + // Tests a developer private function that sets an extension pref.
|
| + void TestExtensionPrefSetting(
|
| + UIThreadExtensionFunction* (*create_function)(),
|
| + bool (*has_pref)(const std::string&, content::BrowserContext*));
|
| +
|
| Browser* browser() { return browser_.get(); }
|
|
|
| private:
|
| @@ -44,6 +53,8 @@ class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase {
|
| scoped_ptr<TestBrowserWindow> browser_window_;
|
| scoped_ptr<Browser> browser_;
|
|
|
| + ScopedVector<TestExtensionDir> test_extension_dirs_;
|
| +
|
| DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest);
|
| };
|
|
|
| @@ -57,25 +68,7 @@ bool DeveloperPrivateApiUnitTest::RunFunction(
|
| extension_function_test_utils::NONE);
|
| }
|
|
|
| -void DeveloperPrivateApiUnitTest::SetUp() {
|
| - ExtensionServiceTestBase::SetUp();
|
| - InitializeEmptyExtensionService();
|
| -
|
| - browser_window_.reset(new TestBrowserWindow());
|
| - Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE);
|
| - params.type = Browser::TYPE_TABBED;
|
| - params.window = browser_window_.get();
|
| - browser_.reset(new Browser(params));
|
| -}
|
| -
|
| -void DeveloperPrivateApiUnitTest::TearDown() {
|
| - browser_.reset();
|
| - browser_window_.reset();
|
| - ExtensionServiceTestBase::TearDown();
|
| -}
|
| -
|
| -// Test developerPrivate.allowIncognito.
|
| -TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowIncognito) {
|
| +const Extension* DeveloperPrivateApiUnitTest::LoadUnpackedExtension() {
|
| const char kManifest[] =
|
| "{"
|
| " \"name\": \"foo\","
|
| @@ -83,10 +76,9 @@ TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowIncognito) {
|
| " \"manifest_version\": 2"
|
| "}";
|
|
|
| - // Sadly, we need a "real" directory here, because toggling incognito causes
|
| - // a reload (which needs a path).
|
| - TestExtensionDir dir;
|
| - dir.WriteManifest(kManifest);
|
| + test_extension_dirs_.push_back(new TestExtensionDir);
|
| + TestExtensionDir* dir = test_extension_dirs_.back();
|
| + dir->WriteManifest(kManifest);
|
|
|
| // TODO(devlin): We should extract out methods to load an unpacked extension
|
| // synchronously. We do it in ExtensionBrowserTest, but that's not helpful
|
| @@ -94,32 +86,112 @@ TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowIncognito) {
|
| TestExtensionRegistryObserver registry_observer(registry());
|
| scoped_refptr<UnpackedInstaller> installer(
|
| UnpackedInstaller::Create(service()));
|
| - installer->Load(dir.unpacked_path());
|
| + installer->Load(dir->unpacked_path());
|
| base::FilePath extension_path =
|
| - base::MakeAbsoluteFilePath(dir.unpacked_path());
|
| + base::MakeAbsoluteFilePath(dir->unpacked_path());
|
| const Extension* extension = nullptr;
|
| do {
|
| extension = registry_observer.WaitForExtensionLoaded();
|
| } while (extension->path() != extension_path);
|
| - std::string extension_id = extension->id();
|
| + // The fact that unpacked extensions get file access by default is an
|
| + // irrelevant detail to these tests. Disable it.
|
| + ExtensionPrefs::Get(browser_context())->SetAllowFileAccess(extension->id(),
|
| + false);
|
| + return extension;
|
| +}
|
|
|
| - scoped_refptr<api::DeveloperPrivateAllowIncognitoFunction> function(
|
| - new api::DeveloperPrivateAllowIncognitoFunction());
|
| +void DeveloperPrivateApiUnitTest::TestExtensionPrefSetting(
|
| + UIThreadExtensionFunction* (*create_function)(),
|
| + bool (*has_pref)(const std::string&, content::BrowserContext*)) {
|
| + // Sadly, we need a "real" directory here, because toggling incognito causes
|
| + // a reload (which needs a path).
|
| + std::string extension_id = LoadUnpackedExtension()->id();
|
| +
|
| + scoped_refptr<UIThreadExtensionFunction> function(create_function());
|
|
|
| base::ListValue enable_args;
|
| enable_args.AppendString(extension_id);
|
| enable_args.AppendBoolean(true);
|
|
|
| - EXPECT_FALSE(util::IsIncognitoEnabled(extension_id, profile()));
|
| + EXPECT_FALSE(has_pref(extension_id, profile()));
|
| +
|
| + // Pref-setting should require a user action.
|
| + EXPECT_FALSE(RunFunction(function, enable_args));
|
| + EXPECT_EQ(std::string("This action requires a user gesture."),
|
| + function->GetError());
|
| +
|
| + ExtensionFunction::ScopedUserGestureForTests scoped_user_gesture;
|
| + function = create_function();
|
| EXPECT_TRUE(RunFunction(function, enable_args));
|
| - EXPECT_TRUE(util::IsIncognitoEnabled(extension_id, profile()));
|
| + EXPECT_TRUE(has_pref(extension_id, profile()));
|
|
|
| base::ListValue disable_args;
|
| disable_args.AppendString(extension_id);
|
| disable_args.AppendBoolean(false);
|
| - function = new api::DeveloperPrivateAllowIncognitoFunction();
|
| + function = create_function();
|
| EXPECT_TRUE(RunFunction(function, disable_args));
|
| - EXPECT_FALSE(util::IsIncognitoEnabled(extension_id, profile()));
|
| + EXPECT_FALSE(has_pref(extension_id, profile()));
|
| +}
|
| +
|
| +void DeveloperPrivateApiUnitTest::SetUp() {
|
| + ExtensionServiceTestBase::SetUp();
|
| + InitializeEmptyExtensionService();
|
| +
|
| + browser_window_.reset(new TestBrowserWindow());
|
| + Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE);
|
| + params.type = Browser::TYPE_TABBED;
|
| + params.window = browser_window_.get();
|
| + browser_.reset(new Browser(params));
|
| +}
|
| +
|
| +void DeveloperPrivateApiUnitTest::TearDown() {
|
| + test_extension_dirs_.clear();
|
| + browser_.reset();
|
| + browser_window_.reset();
|
| + ExtensionServiceTestBase::TearDown();
|
| +}
|
| +
|
| +// Test developerPrivate.allowIncognito.
|
| +TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowIncognito) {
|
| + auto create_function = []() {
|
| + return static_cast<UIThreadExtensionFunction*>(
|
| + new api::DeveloperPrivateAllowIncognitoFunction());
|
| + };
|
| + auto has_pref = [](const std::string& extension_id,
|
| + content::BrowserContext* browser_context) {
|
| + return util::IsIncognitoEnabled(extension_id, browser_context);
|
| + };
|
| + TestExtensionPrefSetting(+create_function, +has_pref);
|
| +}
|
| +
|
| +TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateReload) {
|
| + const Extension* extension = LoadUnpackedExtension();
|
| + std::string extension_id = extension->id();
|
| + scoped_refptr<UIThreadExtensionFunction> function(
|
| + new api::DeveloperPrivateReloadFunction());
|
| + base::ListValue reload_args;
|
| + reload_args.AppendString(extension_id);
|
| +
|
| + TestExtensionRegistryObserver registry_observer(registry());
|
| + EXPECT_TRUE(RunFunction(function, reload_args));
|
| + const Extension* unloaded_extension =
|
| + registry_observer.WaitForExtensionUnloaded();
|
| + EXPECT_EQ(extension, unloaded_extension);
|
| + const Extension* reloaded_extension =
|
| + registry_observer.WaitForExtensionLoaded();
|
| + EXPECT_EQ(extension_id, reloaded_extension->id());
|
| +}
|
| +
|
| +TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateAllowFileAccess) {
|
| + auto create_function = []() {
|
| + return static_cast<UIThreadExtensionFunction*>(
|
| + new api::DeveloperPrivateAllowFileAccessFunction());
|
| + };
|
| + auto has_pref = [](const std::string& extension_id,
|
| + content::BrowserContext* browser_context) {
|
| + return util::AllowFileAccess(extension_id, browser_context);
|
| + };
|
| + TestExtensionPrefSetting(+create_function, +has_pref);
|
| }
|
|
|
| } // namespace extensions
|
|
|