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/file_util.h"
9 #include "base/files/file_path.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_unittest.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/test/test_utils.h"
21 #include "net/test/embedded_test_server/embedded_test_server.h"
22 #include "net/test/embedded_test_server/http_request.h"
23 #include "net/test/embedded_test_server/http_response.h"
25 namespace extensions
{
29 using namespace net::test_server
;
31 const char kManifestPath
[] = "/update_manifest";
32 const char kAppPath
[] = "/app.crx";
34 class ExternalProviderImplTest
: public ExtensionServiceTestBase
{
36 ExternalProviderImplTest() {}
37 virtual ~ExternalProviderImplTest() {}
39 void InitServiceWithExternalProviders() {
40 InitializeExtensionServiceWithUpdater();
42 ProviderCollection providers
;
43 extensions::ExternalProviderImpl::CreateExternalProviders(
44 service_
, profile_
.get(), &providers
);
46 for (ProviderCollection::iterator i
= providers
.begin();
49 service_
->AddProviderForTesting(i
->release());
53 // ExtensionServiceTestBase overrides:
54 virtual void SetUp() OVERRIDE
{
55 ExtensionServiceTestBase::SetUp();
56 test_server_
.reset(new EmbeddedTestServer());
57 ASSERT_TRUE(test_server_
->InitializeAndWaitUntilReady());
58 test_server_
->RegisterRequestHandler(
59 base::Bind(&ExternalProviderImplTest::HandleRequest
,
60 base::Unretained(this)));
62 CommandLine
* cmdline
= CommandLine::ForCurrentProcess();
63 cmdline
->AppendSwitchASCII(switches::kAppsGalleryUpdateURL
,
64 test_server_
->GetURL(kManifestPath
).spec());
68 scoped_ptr
<HttpResponse
> HandleRequest(const HttpRequest
& request
) {
69 GURL url
= test_server_
->GetURL(request
.relative_url
);
70 if (url
.path() == kManifestPath
) {
71 scoped_ptr
<BasicHttpResponse
> response(new BasicHttpResponse
);
72 response
->set_code(net::HTTP_OK
);
73 response
->set_content(base::StringPrintf(
74 "<?xml version='1.0' encoding='UTF-8'?>\n"
75 "<gupdate xmlns='http://www.google.com/update2/response' "
78 " <updatecheck codebase='%s' version='1.0' />\n"
81 extension_misc::kInAppPaymentsSupportAppId
,
82 test_server_
->GetURL(kAppPath
).spec().c_str()));
83 response
->set_content_type("text/xml");
84 return response
.PassAs
<HttpResponse
>();
86 if (url
.path() == kAppPath
) {
87 base::FilePath test_data_dir
;
88 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
90 base::ReadFileToString(
91 test_data_dir
.AppendASCII("extensions/dummyiap.crx"),
93 scoped_ptr
<BasicHttpResponse
> response(new BasicHttpResponse
);
94 response
->set_code(net::HTTP_OK
);
95 response
->set_content(contents
);
96 return response
.PassAs
<HttpResponse
>();
99 return scoped_ptr
<HttpResponse
>();
102 scoped_ptr
<EmbeddedTestServer
> test_server_
;
104 DISALLOW_COPY_AND_ASSIGN(ExternalProviderImplTest
);
109 TEST_F(ExternalProviderImplTest
, InAppPayments
) {
110 InitServiceWithExternalProviders();
112 scoped_refptr
<content::MessageLoopRunner
> runner
=
113 new content::MessageLoopRunner
;
114 service_
->set_external_updates_finished_callback_for_test(
115 runner
->QuitClosure());
117 service_
->CheckForExternalUpdates();
120 EXPECT_TRUE(service_
->GetInstalledExtension(
121 extension_misc::kInAppPaymentsSupportAppId
));
122 EXPECT_TRUE(service_
->IsExtensionEnabled(
123 extension_misc::kInAppPaymentsSupportAppId
));
126 } // namespace extensions