NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / external_provider_impl_unittest.cc
blob2d1a562034fac708f94a8fc464541816304e8e23
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/browser/extensions/updater/extension_cache_fake.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/test/test_utils.h"
22 #include "net/test/embedded_test_server/embedded_test_server.h"
23 #include "net/test/embedded_test_server/http_request.h"
24 #include "net/test/embedded_test_server/http_response.h"
26 namespace extensions {
28 namespace {
30 using namespace net::test_server;
32 const char kManifestPath[] = "/update_manifest";
33 const char kAppPath[] = "/app.crx";
35 class ExternalProviderImplTest : public ExtensionServiceTestBase {
36 public:
37 ExternalProviderImplTest() {}
38 virtual ~ExternalProviderImplTest() {}
40 void InitServiceWithExternalProviders() {
41 InitializeExtensionServiceWithUpdater();
43 ProviderCollection providers;
44 extensions::ExternalProviderImpl::CreateExternalProviders(
45 service_, profile_.get(), &providers);
47 for (ProviderCollection::iterator i = providers.begin();
48 i != providers.end();
49 ++i) {
50 service_->AddProviderForTesting(i->release());
54 // ExtensionServiceTestBase overrides:
55 virtual void SetUp() OVERRIDE {
56 ExtensionServiceTestBase::SetUp();
57 test_server_.reset(new EmbeddedTestServer());
58 ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady());
59 test_server_->RegisterRequestHandler(
60 base::Bind(&ExternalProviderImplTest::HandleRequest,
61 base::Unretained(this)));
63 test_extension_cache_.reset(new ExtensionCacheFake());
65 CommandLine* cmdline = CommandLine::ForCurrentProcess();
66 cmdline->AppendSwitchASCII(switches::kAppsGalleryUpdateURL,
67 test_server_->GetURL(kManifestPath).spec());
70 private:
71 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
72 GURL url = test_server_->GetURL(request.relative_url);
73 if (url.path() == kManifestPath) {
74 scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse);
75 response->set_code(net::HTTP_OK);
76 response->set_content(base::StringPrintf(
77 "<?xml version='1.0' encoding='UTF-8'?>\n"
78 "<gupdate xmlns='http://www.google.com/update2/response' "
79 "protocol='2.0'>\n"
80 " <app appid='%s'>\n"
81 " <updatecheck codebase='%s' version='1.0' />\n"
82 " </app>\n"
83 "</gupdate>",
84 extension_misc::kInAppPaymentsSupportAppId,
85 test_server_->GetURL(kAppPath).spec().c_str()));
86 response->set_content_type("text/xml");
87 return response.PassAs<HttpResponse>();
89 if (url.path() == kAppPath) {
90 base::FilePath test_data_dir;
91 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
92 std::string contents;
93 base::ReadFileToString(
94 test_data_dir.AppendASCII("extensions/dummyiap.crx"),
95 &contents);
96 scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse);
97 response->set_code(net::HTTP_OK);
98 response->set_content(contents);
99 return response.PassAs<HttpResponse>();
102 return scoped_ptr<HttpResponse>();
105 scoped_ptr<EmbeddedTestServer> test_server_;
106 scoped_ptr<ExtensionCacheFake> test_extension_cache_;
108 DISALLOW_COPY_AND_ASSIGN(ExternalProviderImplTest);
111 } // namespace
113 TEST_F(ExternalProviderImplTest, InAppPayments) {
114 InitServiceWithExternalProviders();
116 scoped_refptr<content::MessageLoopRunner> runner =
117 new content::MessageLoopRunner;
118 service_->set_external_updates_finished_callback_for_test(
119 runner->QuitClosure());
121 service_->CheckForExternalUpdates();
122 runner->Run();
124 EXPECT_TRUE(service_->GetInstalledExtension(
125 extension_misc::kInAppPaymentsSupportAppId));
126 EXPECT_TRUE(service_->IsExtensionEnabled(
127 extension_misc::kInAppPaymentsSupportAppId));
130 } // namespace extensions