external_provider_impl_chromeos_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/memory/scoped_ptr.h"
9#include "base/prefs/testing_pref_service.h"
10#include "base/test/scoped_path_override.h"
11#include "chrome/browser/chrome_notification_types.h"
12#include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
13#include "chrome/browser/chromeos/customization_document.h"
14#include "chrome/browser/chromeos/login/users/fake_user_manager.h"
15#include "chrome/browser/extensions/extension_service.h"
16#include "chrome/browser/extensions/extension_service_test_base.h"
17#include "chrome/common/chrome_paths.h"
18#include "chrome/common/chrome_switches.h"
19#include "chrome/test/base/testing_browser_process.h"
20#include "chrome/test/base/testing_profile.h"
21#include "chromeos/system/mock_statistics_provider.h"
22#include "chromeos/system/statistics_provider.h"
23#include "content/public/browser/notification_service.h"
24#include "content/public/test/test_utils.h"
25#include "testing/gmock/include/gmock/gmock.h"
26
27using ::testing::_;
28using ::testing::NotNull;
29using ::testing::Return;
30
31namespace extensions {
32
33namespace {
34
35const char kExternalAppId[] = "kekdneafjmhmndejhmbcadfiiofngffo";
36
37class ExternalProviderImplChromeOSTest : public ExtensionServiceTestBase {
38 public:
39  ExternalProviderImplChromeOSTest()
40      : fake_user_manager_(new chromeos::FakeUserManager()),
41        scoped_user_manager_(fake_user_manager_) {
42  }
43
44  virtual ~ExternalProviderImplChromeOSTest() {}
45
46  void InitServiceWithExternalProviders() {
47    InitializeEmptyExtensionService();
48    service_->Init();
49
50    ProviderCollection providers;
51    extensions::ExternalProviderImpl::CreateExternalProviders(
52        service_, profile_.get(), &providers);
53
54    for (ProviderCollection::iterator i = providers.begin();
55         i != providers.end();
56         ++i) {
57      service_->AddProviderForTesting(i->release());
58    }
59  }
60
61  // ExtensionServiceTestBase overrides:
62  virtual void SetUp() OVERRIDE {
63    ExtensionServiceTestBase::SetUp();
64
65    TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
66    chromeos::ServicesCustomizationDocument::RegisterPrefs(
67        local_state_.registry());
68
69    external_externsions_overrides_.reset(new base::ScopedPathOverride(
70        chrome::DIR_EXTERNAL_EXTENSIONS, data_dir().Append("external")));
71
72    chromeos::system::StatisticsProvider::SetTestProvider(
73        &mock_statistics_provider_);
74    EXPECT_CALL(mock_statistics_provider_, GetMachineStatistic(_, NotNull()))
75        .WillRepeatedly(Return(false));
76  }
77
78  virtual void TearDown() OVERRIDE {
79    chromeos::KioskAppManager::Shutdown();
80    chromeos::system::StatisticsProvider::SetTestProvider(NULL);
81    TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
82  }
83
84 private:
85  TestingPrefServiceSimple local_state_;
86  scoped_ptr<base::ScopedPathOverride> external_externsions_overrides_;
87  chromeos::system::MockStatisticsProvider mock_statistics_provider_;
88  chromeos::FakeUserManager* fake_user_manager_;
89  chromeos::ScopedUserManagerEnabler scoped_user_manager_;
90
91  DISALLOW_COPY_AND_ASSIGN(ExternalProviderImplChromeOSTest);
92};
93
94}  // namespace
95
96// Normal mode, external app should be installed.
97TEST_F(ExternalProviderImplChromeOSTest, Normal) {
98  InitServiceWithExternalProviders();
99
100  service_->CheckForExternalUpdates();
101  content::WindowedNotificationObserver(
102      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
103      content::NotificationService::AllSources()).Wait();
104
105  EXPECT_TRUE(service_->GetInstalledExtension(kExternalAppId));
106}
107
108// App mode, no external app should be installed.
109TEST_F(ExternalProviderImplChromeOSTest, AppMode) {
110  CommandLine* command = CommandLine::ForCurrentProcess();
111  command->AppendSwitchASCII(switches::kForceAppMode, std::string());
112  command->AppendSwitchASCII(switches::kAppId, std::string("app_id"));
113
114  InitServiceWithExternalProviders();
115
116  service_->CheckForExternalUpdates();
117  base::RunLoop().RunUntilIdle();
118
119  EXPECT_FALSE(service_->GetInstalledExtension(kExternalAppId));
120}
121
122}  // namespace extensions
123