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/prefs/testing_pref_service.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/test/scoped_path_override.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/extension_service_test_base.h"
18 #include "chrome/browser/extensions/updater/extension_cache_fake.h"
19 #include "chrome/browser/extensions/updater/extension_updater.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_document.h"
35 #include "chrome/browser/chromeos/login/users/fake_user_manager.h"
36 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
37 #include "chromeos/system/fake_statistics_provider.h"
38 #include "chromeos/system/statistics_provider.h"
41 using ::testing::NotNull
;
42 using ::testing::Return
;
45 namespace extensions
{
49 using namespace net::test_server
;
51 const char kManifestPath
[] = "/update_manifest";
52 const char kAppPath
[] = "/app.crx";
54 class ExternalProviderImplTest
: public ExtensionServiceTestBase
{
56 ExternalProviderImplTest() {}
57 virtual ~ExternalProviderImplTest() {}
59 void InitServiceWithExternalProviders() {
60 #if defined(OS_CHROMEOS)
61 chromeos::ScopedUserManagerEnabler
scoped_user_manager(
62 new chromeos::FakeUserManager
);
64 InitializeExtensionServiceWithUpdater();
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();
81 service_
->AddProviderForTesting(i
->release());
85 // ExtensionServiceTestBase overrides:
86 virtual void SetUp() override
{
87 ExtensionServiceTestBase::SetUp();
88 test_server_
.reset(new EmbeddedTestServer());
90 #if defined(OS_CHROMEOS)
91 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_
);
92 chromeos::ServicesCustomizationDocument::RegisterPrefs(
93 local_state_
.registry());
96 ASSERT_TRUE(test_server_
->InitializeAndWaitUntilReady());
97 test_server_
->RegisterRequestHandler(
98 base::Bind(&ExternalProviderImplTest::HandleRequest
,
99 base::Unretained(this)));
101 test_extension_cache_
.reset(new ExtensionCacheFake());
103 CommandLine
* cmdline
= CommandLine::ForCurrentProcess();
104 cmdline
->AppendSwitchASCII(switches::kAppsGalleryUpdateURL
,
105 test_server_
->GetURL(kManifestPath
).spec());
108 virtual void TearDown() override
{
109 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL
);
113 scoped_ptr
<HttpResponse
> HandleRequest(const HttpRequest
& request
) {
114 GURL url
= test_server_
->GetURL(request
.relative_url
);
115 if (url
.path() == kManifestPath
) {
116 scoped_ptr
<BasicHttpResponse
> response(new BasicHttpResponse
);
117 response
->set_code(net::HTTP_OK
);
118 response
->set_content(base::StringPrintf(
119 "<?xml version='1.0' encoding='UTF-8'?>\n"
120 "<gupdate xmlns='http://www.google.com/update2/response' "
122 " <app appid='%s'>\n"
123 " <updatecheck codebase='%s' version='1.0' />\n"
126 extension_misc::kInAppPaymentsSupportAppId
,
127 test_server_
->GetURL(kAppPath
).spec().c_str()));
128 response
->set_content_type("text/xml");
129 return response
.Pass();
131 if (url
.path() == kAppPath
) {
132 base::FilePath test_data_dir
;
133 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
134 std::string contents
;
135 base::ReadFileToString(
136 test_data_dir
.AppendASCII("extensions/dummyiap.crx"),
138 scoped_ptr
<BasicHttpResponse
> response(new BasicHttpResponse
);
139 response
->set_code(net::HTTP_OK
);
140 response
->set_content(contents
);
141 return response
.Pass();
147 scoped_ptr
<EmbeddedTestServer
> test_server_
;
148 scoped_ptr
<ExtensionCacheFake
> test_extension_cache_
;
150 #if defined(OS_CHROMEOS)
151 // chromeos::ServicesCustomizationExternalLoader is hooked up as an
152 // extensions::ExternalLoader and depends on a functioning StatisticsProvider.
153 chromeos::system::ScopedFakeStatisticsProvider fake_statistics_provider_
;
154 TestingPrefServiceSimple local_state_
;
157 DISALLOW_COPY_AND_ASSIGN(ExternalProviderImplTest
);
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();
173 EXPECT_TRUE(service_
->GetInstalledExtension(
174 extension_misc::kInAppPaymentsSupportAppId
));
175 EXPECT_TRUE(service_
->IsExtensionEnabled(
176 extension_misc::kInAppPaymentsSupportAppId
));
179 } // namespace extensions