0

Fix tests working with ChromeExtensionsClient.

Previously "almost constant" webstore_base_url_ was changed after tests
 with command line URLs overrides. Thus tests that were dependant on state
 of ChromeExtensionsClient were broken.
Now ChromeExtensionsClient has InitializeWebStoreUrls from command line.

Change-Id: Idce49af8754ca1b861a3a4c2406d251d854a79ac
Reviewed-on: https://chromium-review.googlesource.com/593311
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Reviewed-by: Scott Violet <sky@chromium.org>
Reviewed-by: Maksim Ivanov <emaxx@chromium.org>
Reviewed-by: Devlin <rdevlin.cronin@chromium.org>
Reviewed-by: Evan Stade <estade@chromium.org>
Reviewed-by: Achuith Bhandarkar <achuith@chromium.org>
Cr-Commit-Position: refs/heads/master@{#502277}
This commit is contained in:
Konstantin Ganenko
2017-09-15 16:28:59 +00:00
committed by Commit Bot
parent 7a9ab274eb
commit ae1129c98b
18 changed files with 105 additions and 67 deletions

@ -16,6 +16,7 @@
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "crypto/sha2.h"
#include "extensions/common/extensions_client.h"
#include "net/base/url_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
@ -83,7 +84,7 @@ void FakeCWS::Init(net::EmbeddedTestServer* embedded_test_server) {
update_check_end_point_ = "/update_check.xml";
SetupWebStoreURL(embedded_test_server->base_url());
OverrideGalleryCommandlineSwitches();
OverrideGalleryCommandlineSwitches(GalleryUpdateMode::kOnlyCommandLine);
embedded_test_server->RegisterRequestHandler(
base::Bind(&FakeCWS::HandleRequest, base::Unretained(this)));
}
@ -95,7 +96,8 @@ void FakeCWS::InitAsPrivateStore(net::EmbeddedTestServer* embedded_test_server,
update_check_end_point_ = update_check_end_point;
SetupWebStoreURL(embedded_test_server->base_url());
OverrideGalleryCommandlineSwitches();
OverrideGalleryCommandlineSwitches(
GalleryUpdateMode::kModifyExtensionsClient);
embedded_test_server->RegisterRequestHandler(
base::Bind(&FakeCWS::HandleRequest, base::Unretained(this)));
@ -150,7 +152,8 @@ void FakeCWS::SetupWebStoreURL(const GURL& test_server_url) {
web_store_url_ = test_server_url.ReplaceComponents(replace_webstore_host);
}
void FakeCWS::OverrideGalleryCommandlineSwitches() {
void FakeCWS::OverrideGalleryCommandlineSwitches(
GalleryUpdateMode gallery_update_mode) {
DCHECK(web_store_url_.is_valid());
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
@ -167,6 +170,9 @@ void FakeCWS::OverrideGalleryCommandlineSwitches() {
GURL update_url = web_store_url_.Resolve(update_check_end_point_);
command_line->AppendSwitchASCII(::switches::kAppsGalleryUpdateURL,
update_url.spec());
if (gallery_update_mode == GalleryUpdateMode::kModifyExtensionsClient)
extensions::ExtensionsClient::Get()->InitializeWebStoreUrls(command_line);
}
bool FakeCWS::GetUpdateCheckContent(const std::vector<std::string>& ids,

@ -30,9 +30,8 @@ class FakeCWS {
void Init(net::EmbeddedTestServer* embedded_test_server);
// Initializes as a private store handler using the given server and URL end
// point. Private store does not override app gallery command lines and use a
// slightly different template (as documented on
// https://developer.chrome.com/extensions/autoupdate).
// point. Override app gallery command line and provide it to Extensions
// client.
void InitAsPrivateStore(net::EmbeddedTestServer* embedded_test_server,
const std::string& update_check_end_point);
@ -48,8 +47,14 @@ class FakeCWS {
int GetUpdateCheckCountAndReset();
private:
enum class GalleryUpdateMode {
kOnlyCommandLine,
kModifyExtensionsClient,
};
void SetupWebStoreURL(const GURL& test_server_url);
void OverrideGalleryCommandlineSwitches();
void OverrideGalleryCommandlineSwitches(
GalleryUpdateMode gallery_update_mode);
bool GetUpdateCheckContent(const std::vector<std::string>& ids,
std::string* update_check_content);

@ -23,6 +23,7 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_test_util.h"
#include "chrome/common/features.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/gpu_data_manager.h"
@ -154,9 +155,8 @@ class ExtensionWebstorePrivateApiTest : public ExtensionApiTest {
// See http://crbug.com/177163 for details.
return true;
#else
GURL crx_url = GetTestServerURL(crx_file);
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kAppsGalleryUpdateURL, crx_url.spec());
const GURL crx_url = GetTestServerURL(crx_file);
extension_test_util::SetGalleryUpdateURL(crx_url);
GURL page_url = GetTestServerURL(page);
return RunPageTest(page_url.spec());

@ -34,7 +34,7 @@
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_test_util.h"
#include "chrome/common/extensions/sync_helper.h"
#include "chrome/common/features.h"
#include "chrome/test/base/testing_profile.h"
@ -1573,14 +1573,19 @@ TEST_F(ExtensionServiceSyncTest, ProcessSyncDataEnableDisable) {
}
}
TEST_F(ExtensionServiceSyncTest, ProcessSyncDataDeferredEnable) {
// The permissions_increase test extension has a different update URL.
// In order to make it syncable, we have to pretend it syncs from the
// webstore.
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kAppsGalleryUpdateURL,
"http://localhost/autoupdate/updates.xml");
class ExtensionServiceSyncCustomGalleryTest : public ExtensionServiceSyncTest {
public:
void SetUp() override {
ExtensionServiceSyncTest::SetUp();
// This is the update URL specified in the permissions test extension.
// Setting it here is necessary to make the extension considered syncable.
extension_test_util::SetGalleryUpdateURL(
GURL("http://localhost/autoupdate/updates.xml"));
}
};
TEST_F(ExtensionServiceSyncCustomGalleryTest, ProcessSyncDataDeferredEnable) {
InitializeEmptyExtensionService();
extension_sync_service()->MergeDataAndStartSyncing(
syncer::EXTENSIONS, syncer::SyncDataList(),
@ -1624,19 +1629,6 @@ TEST_F(ExtensionServiceSyncTest, ProcessSyncDataDeferredEnable) {
PackCRXAndUpdateExtension(id, path, pem_path, ENABLED);
}
class ExtensionServiceSyncCustomGalleryTest : public ExtensionServiceSyncTest {
public:
void SetUp() override {
ExtensionServiceSyncTest::SetUp();
// This is the update URL specified in the permissions test extension.
// Setting it here is necessary to make the extension considered syncable.
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kAppsGalleryUpdateURL,
"http://localhost/autoupdate/updates.xml");
}
};
TEST_F(ExtensionServiceSyncCustomGalleryTest,
ProcessSyncDataPermissionApproval) {
InitializeEmptyExtensionService();

@ -22,8 +22,8 @@
#include "chrome/browser/extensions/updater/extension_updater.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_test_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
@ -106,9 +106,8 @@ class ExternalProviderImplTest : public ExtensionServiceTestBase {
test_extension_cache_.reset(new ExtensionCacheFake());
base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
cmdline->AppendSwitchASCII(switches::kAppsGalleryUpdateURL,
test_server_->GetURL(kManifestPath).spec());
extension_test_util::SetGalleryUpdateURL(
test_server_->GetURL(kManifestPath));
}
private:

@ -90,6 +90,7 @@
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_test_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
@ -1562,8 +1563,7 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, ExtensionInstallBlacklistSharedModules) {
.AppendASCII("update.xml");
GURL update_xml_url(
URLRequestMockHTTPJob::GetMockUrl(update_xml_path.MaybeAsASCII()));
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kAppsGalleryUpdateURL, update_xml_url.spec());
extension_test_util::SetGalleryUpdateURL(update_xml_url);
ui_test_utils::NavigateToURL(browser(), update_xml_url);
// Blacklist "*" but force-install the importer extension. The shared module

@ -19,7 +19,7 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_test_util.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sync/model/attachments/attachment_id.h"
#include "components/sync/model/attachments/attachment_service_proxy_for_test.h"
@ -162,8 +162,7 @@ class ThemeSyncableServiceTest : public testing::Test {
void SetUp() override {
// Setting a matching update URL is necessary to make the test theme
// considered syncable.
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kAppsGalleryUpdateURL, kCustomThemeUrl);
extension_test_util::SetGalleryUpdateURL(GURL(kCustomThemeUrl));
profile_.reset(new TestingProfile);
fake_theme_service_ = BuildForProfile(profile_.get());

@ -20,6 +20,7 @@
#include "chrome/browser/ui/app_list/search/webstore/webstore_result.h"
#include "chrome/browser/ui/app_list/test/test_app_list_controller_delegate.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_test_util.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/common/extension_urls.h"
@ -171,9 +172,8 @@ class WebstoreProviderTest : public InProcessBrowserTest {
ASSERT_TRUE(embedded_test_server()->Start());
// Minor hack: the gallery URL is expected not to end with a slash. Just
// append "path" to maintain this.
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
::switches::kAppsGalleryURL,
embedded_test_server()->base_url().spec() + "path");
const GURL gallery_url(embedded_test_server()->base_url().spec() + "path");
extension_test_util::SetGalleryURL(gallery_url);
mock_controller_.reset(new AppListControllerDelegateForTest);
webstore_provider_.reset(new WebstoreProvider(

@ -124,9 +124,23 @@ void ChromeExtensionsClient::Initialize() {
// TODO(dmazzoni): remove this once we have an extension API that
// allows any extension to request read-only access to webui pages.
scripting_whitelist_.push_back(extension_misc::kChromeVoxExtensionId);
InitializeWebStoreUrls(base::CommandLine::ForCurrentProcess());
}
webstore_base_url_ = GURL(extension_urls::kChromeWebstoreBaseURL);
webstore_update_url_ = GURL(extension_urls::GetDefaultWebstoreUpdateUrl());
void ChromeExtensionsClient::InitializeWebStoreUrls(
base::CommandLine* command_line) {
if (command_line->HasSwitch(switches::kAppsGalleryURL)) {
webstore_base_url_ =
GURL(command_line->GetSwitchValueASCII(switches::kAppsGalleryURL));
} else {
webstore_base_url_ = GURL(extension_urls::kChromeWebstoreBaseURL);
}
if (command_line->HasSwitch(switches::kAppsGalleryUpdateURL)) {
webstore_update_url_ = GURL(
command_line->GetSwitchValueASCII(switches::kAppsGalleryUpdateURL));
} else {
webstore_update_url_ = GURL(extension_urls::GetDefaultWebstoreUpdateUrl());
}
}
const PermissionMessageProvider&
@ -265,29 +279,10 @@ void ChromeExtensionsClient::RecordDidSuppressFatalError() {
}
const GURL& ChromeExtensionsClient::GetWebstoreBaseURL() const {
// Browser tests like to alter the command line at runtime with new update
// URLs. Just update the cached value of the base url (to avoid reparsing
// it) if the value has changed.
base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
if (cmdline->HasSwitch(switches::kAppsGalleryURL)) {
std::string url = cmdline->GetSwitchValueASCII(switches::kAppsGalleryURL);
if (webstore_base_url_.possibly_invalid_spec() != url)
webstore_base_url_ = GURL(url);
}
return webstore_base_url_;
}
const GURL& ChromeExtensionsClient::GetWebstoreUpdateURL() const {
// Browser tests like to alter the command line at runtime with new update
// URLs. Just update the cached value of the update url (to avoid reparsing
// it) if the value has changed.
base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
if (cmdline->HasSwitch(switches::kAppsGalleryUpdateURL)) {
std::string url =
cmdline->GetSwitchValueASCII(switches::kAppsGalleryUpdateURL);
if (webstore_update_url_.possibly_invalid_spec() != url)
webstore_update_url_ = GURL(url);
}
return webstore_update_url_;
}

@ -27,6 +27,8 @@ class ChromeExtensionsClient : public ExtensionsClient {
void Initialize() override;
void InitializeWebStoreUrls(base::CommandLine* command_line) override;
const PermissionMessageProvider& GetPermissionMessageProvider()
const override;
const std::string GetProductName() override;
@ -69,9 +71,8 @@ class ChromeExtensionsClient : public ExtensionsClient {
// added to this list.
ScriptingWhitelist scripting_whitelist_;
// Mutable to allow caching in a const method.
mutable GURL webstore_base_url_;
mutable GURL webstore_update_url_;
GURL webstore_base_url_;
GURL webstore_update_url_;
friend struct base::LazyInstanceTraitsBase<ChromeExtensionsClient>;

@ -6,14 +6,18 @@
#include <memory>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/json/json_file_value_serializer.h"
#include "base/path_service.h"
#include "base/values.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "extensions/common/extension.h"
#include "extensions/common/extensions_client.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using extensions::Extension;
using extensions::Manifest;
@ -81,4 +85,17 @@ scoped_refptr<Extension> LoadManifest(const std::string& dir,
return LoadManifest(dir, test_file, Extension::NO_FLAGS);
}
void SetGalleryURL(const GURL& new_url) {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitchASCII(switches::kAppsGalleryURL, new_url.spec());
extensions::ExtensionsClient::Get()->InitializeWebStoreUrls(command_line);
}
void SetGalleryUpdateURL(const GURL& new_url) {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitchASCII(switches::kAppsGalleryUpdateURL,
new_url.spec());
extensions::ExtensionsClient::Get()->InitializeWebStoreUrls(command_line);
}
} // namespace extension_test_util

@ -10,6 +10,8 @@
#include "base/memory/ref_counted.h"
#include "extensions/common/manifest.h"
class GURL;
namespace extensions {
class Extension;
}
@ -50,6 +52,9 @@ scoped_refptr<extensions::Extension> LoadManifestStrict(
scoped_refptr<extensions::Extension> LoadManifest(const std::string& dir,
const std::string& test_file);
void SetGalleryURL(const GURL& new_url);
void SetGalleryUpdateURL(const GURL& new_url);
} // namespace extension_test_util
#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_TEST_UTIL_H_

@ -48,6 +48,12 @@ class ChromeUnitTestSuiteInitializer : public testing::EmptyTestEventListener {
void OnTestStart(const testing::TestInfo& test_info) override {
content_client_.reset(new ChromeContentClient);
content::SetContentClient(content_client_.get());
#if BUILDFLAG(ENABLE_EXTENSIONS)
extensions::ExtensionsClient::Get()->InitializeWebStoreUrls(
base::CommandLine::ForCurrentProcess());
#endif
browser_content_client_.reset(new ChromeContentBrowserClient());
content::SetBrowserClientForTesting(browser_content_client_.get());
utility_content_client_.reset(new ChromeContentUtilityClient());

@ -16,6 +16,7 @@
class GURL;
namespace base {
class CommandLine;
class FilePath;
}
@ -41,6 +42,10 @@ class ExtensionsClient {
// in-process.
virtual void Initialize() = 0;
// Initializes web store URLs.
// Default values could be overriden with command line.
virtual void InitializeWebStoreUrls(base::CommandLine* command_line) = 0;
// Returns the global PermissionMessageProvider to use to provide permission
// warning strings.
virtual const PermissionMessageProvider& GetPermissionMessageProvider()

@ -88,6 +88,9 @@ void ShellExtensionsClient::Initialize() {
GetExtensionsPermissionAliases());
}
void ShellExtensionsClient::InitializeWebStoreUrls(
base::CommandLine* command_line) {}
const PermissionMessageProvider&
ShellExtensionsClient::GetPermissionMessageProvider() const {
NOTIMPLEMENTED();

@ -21,6 +21,7 @@ class ShellExtensionsClient : public ExtensionsClient {
// ExtensionsClient overrides:
void Initialize() override;
void InitializeWebStoreUrls(base::CommandLine* command_line) override;
const PermissionMessageProvider& GetPermissionMessageProvider()
const override;
const std::string GetProductName() override;

@ -60,6 +60,9 @@ void TestExtensionsClient::Initialize() {
GetExtensionsPermissionAliases());
}
void TestExtensionsClient::InitializeWebStoreUrls(
base::CommandLine* command_line) {}
const PermissionMessageProvider&
TestExtensionsClient::GetPermissionMessageProvider() const {
static TestPermissionMessageProvider provider;

@ -29,6 +29,7 @@ class TestExtensionsClient : public ExtensionsClient {
private:
void Initialize() override;
void InitializeWebStoreUrls(base::CommandLine* command_line) override;
const PermissionMessageProvider& GetPermissionMessageProvider()
const override;
const std::string GetProductName() override;