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