Prevent chrome://net-internals/#export from flickering
[chromium-blink-merge.git] / chrome / browser / extensions / external_provider_impl_unittest.cc
blob43962c9fb6a38d522bb8238c949ed485574b0ce0
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/external_provider_impl.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/test/scoped_path_override.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_service_test_base.h"
17 #include "chrome/browser/extensions/updater/extension_cache_fake.h"
18 #include "chrome/browser/extensions/updater/extension_updater.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/extensions/extension_constants.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_profile.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/test/test_utils.h"
28 #include "net/test/embedded_test_server/embedded_test_server.h"
29 #include "net/test/embedded_test_server/http_request.h"
30 #include "net/test/embedded_test_server/http_response.h"
31 #include "testing/gmock/include/gmock/gmock.h"
33 #if defined(OS_CHROMEOS)
34 #include "chrome/browser/chromeos/customization/customization_document.h"
35 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
36 #include "chromeos/system/fake_statistics_provider.h"
37 #include "chromeos/system/statistics_provider.h"
38 #include "components/user_manager/fake_user_manager.h"
39 #endif
41 using ::testing::NotNull;
42 using ::testing::Return;
43 using ::testing::_;
45 namespace extensions {
47 namespace {
49 using namespace net::test_server;
51 const char kManifestPath[] = "/update_manifest";
52 const char kAppPath[] = "/app.crx";
54 class ExternalProviderImplTest : public ExtensionServiceTestBase {
55 public:
56 ExternalProviderImplTest() {}
57 ~ExternalProviderImplTest() override {}
59 void InitServiceWithExternalProviders() {
60 #if defined(OS_CHROMEOS)
61 chromeos::ScopedUserManagerEnabler scoped_user_manager(
62 new user_manager::FakeUserManager);
63 #endif
64 InitializeExtensionServiceWithUpdaterAndPrefs();
66 service()->updater()->SetExtensionCacheForTesting(
67 test_extension_cache_.get());
69 // Don't install default apps. Some of the default apps are downloaded from
70 // the webstore, ignoring the url we pass to kAppsGalleryUpdateURL, which
71 // would cause the external updates to never finish install.
72 profile_->GetPrefs()->SetString(prefs::kDefaultApps, "");
74 ProviderCollection providers;
75 extensions::ExternalProviderImpl::CreateExternalProviders(
76 service_, profile_.get(), &providers);
78 for (ProviderCollection::iterator i = providers.begin();
79 i != providers.end();
80 ++i) {
81 service_->AddProviderForTesting(i->release());
85 void InitializeExtensionServiceWithUpdaterAndPrefs() {
86 ExtensionServiceInitParams params = CreateDefaultInitParams();
87 params.autoupdate_enabled = true;
88 // Create prefs file to make the profile not new.
89 const char prefs[] = "{}";
90 EXPECT_EQ(base::WriteFile(params.pref_file, prefs, sizeof(prefs)),
91 int(sizeof(prefs)));
92 InitializeExtensionService(params);
93 service_->updater()->Start();
96 // ExtensionServiceTestBase overrides:
97 void SetUp() override {
98 ExtensionServiceTestBase::SetUp();
99 test_server_.reset(new EmbeddedTestServer());
101 ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady());
102 test_server_->RegisterRequestHandler(
103 base::Bind(&ExternalProviderImplTest::HandleRequest,
104 base::Unretained(this)));
106 test_extension_cache_.reset(new ExtensionCacheFake());
108 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
109 cmdline->AppendSwitchASCII(switches::kAppsGalleryUpdateURL,
110 test_server_->GetURL(kManifestPath).spec());
113 private:
114 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
115 GURL url = test_server_->GetURL(request.relative_url);
116 if (url.path() == kManifestPath) {
117 scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse);
118 response->set_code(net::HTTP_OK);
119 response->set_content(base::StringPrintf(
120 "<?xml version='1.0' encoding='UTF-8'?>\n"
121 "<gupdate xmlns='http://www.google.com/update2/response' "
122 "protocol='2.0'>\n"
123 " <app appid='%s'>\n"
124 " <updatecheck codebase='%s' version='1.0' />\n"
125 " </app>\n"
126 "</gupdate>",
127 extension_misc::kInAppPaymentsSupportAppId,
128 test_server_->GetURL(kAppPath).spec().c_str()));
129 response->set_content_type("text/xml");
130 return response.Pass();
132 if (url.path() == kAppPath) {
133 base::FilePath test_data_dir;
134 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
135 std::string contents;
136 base::ReadFileToString(
137 test_data_dir.AppendASCII("extensions/dummyiap.crx"),
138 &contents);
139 scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse);
140 response->set_code(net::HTTP_OK);
141 response->set_content(contents);
142 return response.Pass();
145 return nullptr;
148 scoped_ptr<EmbeddedTestServer> test_server_;
149 scoped_ptr<ExtensionCacheFake> test_extension_cache_;
151 #if defined(OS_CHROMEOS)
152 // chromeos::ServicesCustomizationExternalLoader is hooked up as an
153 // extensions::ExternalLoader and depends on a functioning StatisticsProvider.
154 chromeos::system::ScopedFakeStatisticsProvider fake_statistics_provider_;
155 #endif
157 DISALLOW_COPY_AND_ASSIGN(ExternalProviderImplTest);
160 } // namespace
162 TEST_F(ExternalProviderImplTest, InAppPayments) {
163 InitServiceWithExternalProviders();
165 scoped_refptr<content::MessageLoopRunner> runner =
166 new content::MessageLoopRunner;
167 service_->set_external_updates_finished_callback_for_test(
168 runner->QuitClosure());
170 service_->CheckForExternalUpdates();
171 runner->Run();
173 EXPECT_TRUE(service_->GetInstalledExtension(
174 extension_misc::kInAppPaymentsSupportAppId));
175 EXPECT_TRUE(service_->IsExtensionEnabled(
176 extension_misc::kInAppPaymentsSupportAppId));
179 } // namespace extensions