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/memory/scoped_ptr.h"
9 #include "base/test/scoped_path_override.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
12 #include "chrome/browser/chromeos/customization/customization_document.h"
13 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_service_test_base.h"
16 #include "chrome/browser/prefs/pref_service_syncable_util.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
19 #include "chrome/browser/signin/signin_manager_factory.h"
20 #include "chrome/browser/sync/profile_sync_service.h"
21 #include "chrome/browser/sync/profile_sync_service_factory.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_profile.h"
26 #include "chromeos/system/fake_statistics_provider.h"
27 #include "chromeos/system/statistics_provider.h"
28 #include "components/signin/core/browser/profile_oauth2_token_service.h"
29 #include "components/signin/core/browser/signin_manager_base.h"
30 #include "components/sync_driver/pref_names.h"
31 #include "components/syncable_prefs/pref_service_syncable.h"
32 #include "components/user_manager/fake_user_manager.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/test/test_utils.h"
35 #include "sync/api/fake_sync_change_processor.h"
36 #include "sync/api/sync_change_processor.h"
37 #include "sync/api/sync_error_factory_mock.h"
39 namespace extensions
{
43 const char kExternalAppId
[] = "kekdneafjmhmndejhmbcadfiiofngffo";
44 const char kStandaloneAppId
[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
46 class ExternalProviderImplChromeOSTest
: public ExtensionServiceTestBase
{
48 ExternalProviderImplChromeOSTest()
49 : fake_user_manager_(new user_manager::FakeUserManager()),
50 scoped_user_manager_(fake_user_manager_
) {}
52 ~ExternalProviderImplChromeOSTest() override
{}
54 void InitServiceWithExternalProviders(bool standalone
) {
55 InitializeEmptyExtensionService();
59 external_externsions_overrides_
.reset(new base::ScopedPathOverride(
60 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
,
61 data_dir().Append("external_standalone")));
63 external_externsions_overrides_
.reset(new base::ScopedPathOverride(
64 chrome::DIR_EXTERNAL_EXTENSIONS
, data_dir().Append("external")));
67 ProviderCollection providers
;
68 extensions::ExternalProviderImpl::CreateExternalProviders(
69 service_
, profile_
.get(), &providers
);
71 for (ProviderCollection::iterator i
= providers
.begin();
74 service_
->AddProviderForTesting(i
->release());
78 // ExtensionServiceTestBase overrides:
79 void SetUp() override
{
80 ExtensionServiceTestBase::SetUp();
83 void TearDown() override
{
84 chromeos::KioskAppManager::Shutdown();
88 scoped_ptr
<base::ScopedPathOverride
> external_externsions_overrides_
;
89 chromeos::system::ScopedFakeStatisticsProvider fake_statistics_provider_
;
90 user_manager::FakeUserManager
* fake_user_manager_
;
91 chromeos::ScopedUserManagerEnabler scoped_user_manager_
;
93 DISALLOW_COPY_AND_ASSIGN(ExternalProviderImplChromeOSTest
);
98 // Normal mode, external app should be installed.
99 TEST_F(ExternalProviderImplChromeOSTest
, Normal
) {
100 InitServiceWithExternalProviders(false);
102 service_
->CheckForExternalUpdates();
103 content::WindowedNotificationObserver(
104 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
105 content::NotificationService::AllSources()).Wait();
107 EXPECT_TRUE(service_
->GetInstalledExtension(kExternalAppId
));
110 // App mode, no external app should be installed.
111 TEST_F(ExternalProviderImplChromeOSTest
, AppMode
) {
112 base::CommandLine
* command
= base::CommandLine::ForCurrentProcess();
113 command
->AppendSwitchASCII(switches::kForceAppMode
, std::string());
114 command
->AppendSwitchASCII(switches::kAppId
, std::string("app_id"));
116 InitServiceWithExternalProviders(false);
118 service_
->CheckForExternalUpdates();
119 base::RunLoop().RunUntilIdle();
121 EXPECT_FALSE(service_
->GetInstalledExtension(kExternalAppId
));
124 // Normal mode, standalone app should be installed, because sync is enabled but
126 TEST_F(ExternalProviderImplChromeOSTest
, Standalone
) {
127 InitServiceWithExternalProviders(true);
129 service_
->CheckForExternalUpdates();
130 content::WindowedNotificationObserver(
131 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
132 content::NotificationService::AllSources()).Wait();
134 EXPECT_TRUE(service_
->GetInstalledExtension(kStandaloneAppId
));
137 // Normal mode, standalone app should be installed, because sync is disabled.
138 TEST_F(ExternalProviderImplChromeOSTest
, SyncDisabled
) {
139 base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableSync
);
141 InitServiceWithExternalProviders(true);
143 service_
->CheckForExternalUpdates();
144 content::WindowedNotificationObserver(
145 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
146 content::NotificationService::AllSources()).Wait();
148 EXPECT_TRUE(service_
->GetInstalledExtension(kStandaloneAppId
));
151 // User signed in, sync service started, install app when sync is disabled by
153 TEST_F(ExternalProviderImplChromeOSTest
, PolicyDisabled
) {
154 InitServiceWithExternalProviders(true);
156 // Log user in, start sync.
157 TestingBrowserProcess::GetGlobal()->SetProfileManager(
158 new ProfileManagerWithoutInit(temp_dir().path()));
159 SigninManagerBase
* signin
=
160 SigninManagerFactory::GetForProfile(profile_
.get());
161 signin
->SetAuthenticatedAccountInfo("gaia-id-test_user@gmail.com",
162 "test_user@gmail.com");
163 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get())
164 ->UpdateCredentials("test_user@gmail.com", "oauth2_login_token");
166 // App sync will wait for priority sync to complete.
167 service_
->CheckForExternalUpdates();
169 // Sync is dsabled by policy.
170 profile_
->GetPrefs()->SetBoolean(sync_driver::prefs::kSyncManaged
, true);
172 content::WindowedNotificationObserver(
173 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
174 content::NotificationService::AllSources()).Wait();
176 EXPECT_TRUE(service_
->GetInstalledExtension(kStandaloneAppId
));
178 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL
);
181 // User signed in, sync service started, install app when priority sync is
183 TEST_F(ExternalProviderImplChromeOSTest
, PriorityCompleted
) {
184 InitServiceWithExternalProviders(true);
186 // User is logged in.
187 SigninManagerBase
* signin
=
188 SigninManagerFactory::GetForProfile(profile_
.get());
189 signin
->SetAuthenticatedAccountInfo("gaia-id-test_user@gmail.com",
190 "test_user@gmail.com");
192 // App sync will wait for priority sync to complete.
193 service_
->CheckForExternalUpdates();
195 // Priority sync completed.
196 PrefServiceSyncableFromProfile(profile_
.get())
197 ->GetSyncableService(syncer::PRIORITY_PREFERENCES
)
198 ->MergeDataAndStartSyncing(syncer::PRIORITY_PREFERENCES
,
199 syncer::SyncDataList(),
200 scoped_ptr
<syncer::SyncChangeProcessor
>(
201 new syncer::FakeSyncChangeProcessor
),
202 scoped_ptr
<syncer::SyncErrorFactory
>(
203 new syncer::SyncErrorFactoryMock()));
205 content::WindowedNotificationObserver(
206 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
207 content::NotificationService::AllSources()).Wait();
209 EXPECT_TRUE(service_
->GetInstalledExtension(kStandaloneAppId
));
212 } // namespace extensions