• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/extensions/external_provider_impl.h"
6 
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/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/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/extensions/extension_constants.h"
22 #include "chrome/test/base/testing_browser_process.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/test/test_utils.h"
26 #include "net/test/embedded_test_server/embedded_test_server.h"
27 #include "net/test/embedded_test_server/http_request.h"
28 #include "net/test/embedded_test_server/http_response.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 
31 #if defined(OS_CHROMEOS)
32 #include "chrome/browser/chromeos/customization_document.h"
33 #include "chrome/browser/chromeos/login/users/fake_user_manager.h"
34 #include "chromeos/system/mock_statistics_provider.h"
35 #include "chromeos/system/statistics_provider.h"
36 #endif
37 
38 using ::testing::_;
39 using ::testing::NotNull;
40 using ::testing::Return;
41 
42 namespace extensions {
43 
44 namespace {
45 
46 using namespace net::test_server;
47 
48 const char kManifestPath[] = "/update_manifest";
49 const char kAppPath[] = "/app.crx";
50 
51 class ExternalProviderImplTest : public ExtensionServiceTestBase {
52  public:
ExternalProviderImplTest()53   ExternalProviderImplTest() {}
~ExternalProviderImplTest()54   virtual ~ExternalProviderImplTest() {}
55 
InitServiceWithExternalProviders()56   void InitServiceWithExternalProviders() {
57 #if defined(OS_CHROMEOS)
58     chromeos::ScopedUserManagerEnabler scoped_user_manager(
59         new chromeos::FakeUserManager);
60 #endif
61     InitializeExtensionServiceWithUpdater();
62 
63     ProviderCollection providers;
64     extensions::ExternalProviderImpl::CreateExternalProviders(
65         service_, profile_.get(), &providers);
66 
67     for (ProviderCollection::iterator i = providers.begin();
68          i != providers.end();
69          ++i) {
70       service_->AddProviderForTesting(i->release());
71     }
72   }
73 
74   // ExtensionServiceTestBase overrides:
SetUp()75   virtual void SetUp() OVERRIDE {
76     ExtensionServiceTestBase::SetUp();
77     test_server_.reset(new EmbeddedTestServer());
78 
79 #if defined(OS_CHROMEOS)
80     TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
81     chromeos::ServicesCustomizationDocument::RegisterPrefs(
82         local_state_.registry());
83 #endif
84 
85     ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady());
86     test_server_->RegisterRequestHandler(
87         base::Bind(&ExternalProviderImplTest::HandleRequest,
88                    base::Unretained(this)));
89 
90     test_extension_cache_.reset(new ExtensionCacheFake());
91 
92     CommandLine* cmdline = CommandLine::ForCurrentProcess();
93     cmdline->AppendSwitchASCII(switches::kAppsGalleryUpdateURL,
94                                test_server_->GetURL(kManifestPath).spec());
95 #if defined(OS_CHROMEOS)
96     chromeos::system::StatisticsProvider::SetTestProvider(
97         &mock_statistics_provider_);
98     EXPECT_CALL(mock_statistics_provider_, GetMachineStatistic(_, NotNull()))
99         .WillRepeatedly(Return(false));
100 #endif
101   }
102 
TearDown()103   virtual void TearDown() OVERRIDE {
104 #if defined(OS_CHROMEOS)
105     chromeos::system::StatisticsProvider::SetTestProvider(NULL);
106     TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
107 #endif
108   }
109 
110  private:
HandleRequest(const HttpRequest & request)111   scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
112     GURL url = test_server_->GetURL(request.relative_url);
113     if (url.path() == kManifestPath) {
114       scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse);
115       response->set_code(net::HTTP_OK);
116       response->set_content(base::StringPrintf(
117           "<?xml version='1.0' encoding='UTF-8'?>\n"
118           "<gupdate xmlns='http://www.google.com/update2/response' "
119               "protocol='2.0'>\n"
120           "  <app appid='%s'>\n"
121           "    <updatecheck codebase='%s' version='1.0' />\n"
122           "  </app>\n"
123           "</gupdate>",
124           extension_misc::kInAppPaymentsSupportAppId,
125           test_server_->GetURL(kAppPath).spec().c_str()));
126       response->set_content_type("text/xml");
127       return response.PassAs<HttpResponse>();
128     }
129     if (url.path() == kAppPath) {
130       base::FilePath test_data_dir;
131       PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
132       std::string contents;
133       base::ReadFileToString(
134           test_data_dir.AppendASCII("extensions/dummyiap.crx"),
135           &contents);
136       scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse);
137       response->set_code(net::HTTP_OK);
138       response->set_content(contents);
139       return response.PassAs<HttpResponse>();
140     }
141 
142     return scoped_ptr<HttpResponse>();
143   }
144 
145   scoped_ptr<EmbeddedTestServer> test_server_;
146   scoped_ptr<ExtensionCacheFake> test_extension_cache_;
147 #if defined(OS_CHROMEOS)
148   chromeos::system::MockStatisticsProvider mock_statistics_provider_;
149   TestingPrefServiceSimple local_state_;
150 #endif
151 
152   DISALLOW_COPY_AND_ASSIGN(ExternalProviderImplTest);
153 };
154 
155 }  // namespace
156 
157 // flaky, disabling on branch only
TEST_F(ExternalProviderImplTest,DISABLED_InAppPayments)158 TEST_F(ExternalProviderImplTest, DISABLED_InAppPayments) {
159   InitServiceWithExternalProviders();
160 
161   scoped_refptr<content::MessageLoopRunner> runner =
162       new content::MessageLoopRunner;
163   service_->set_external_updates_finished_callback_for_test(
164       runner->QuitClosure());
165 
166   service_->CheckForExternalUpdates();
167   runner->Run();
168 
169   EXPECT_TRUE(service_->GetInstalledExtension(
170       extension_misc::kInAppPaymentsSupportAppId));
171   EXPECT_TRUE(service_->IsExtensionEnabled(
172       extension_misc::kInAppPaymentsSupportAppId));
173 }
174 
175 }  // namespace extensions
176