1 // Copyright (c) 2011 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/chromeos/customization/customization_document.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/prefs/testing_pref_service.h"
9 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
12 #include "chrome/browser/extensions/external_provider_impl.h"
13 #include "chrome/browser/prefs/browser_prefs.h"
14 #include "chrome/browser/prefs/pref_service_mock_factory.h"
15 #include "chrome/browser/prefs/pref_service_syncable.h"
16 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
17 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "chromeos/dbus/dbus_thread_manager.h"
21 #include "chromeos/network/network_handler.h"
22 #include "chromeos/network/network_state.h"
23 #include "chromeos/network/network_state_handler.h"
24 #include "chromeos/system/fake_statistics_provider.h"
25 #include "components/pref_registry/pref_registry_syncable.h"
26 #include "content/public/test/test_browser_thread_bundle.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/manifest.h"
29 #include "net/http/http_response_headers.h"
30 #include "net/http/http_status_code.h"
31 #include "net/url_request/test_url_fetcher_factory.h"
32 #include "net/url_request/url_request_status.h"
33 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h"
36 using ::testing::Exactly
;
37 using ::testing::Invoke
;
38 using ::testing::Mock
;
43 const char kGoodStartupManifest
[] =
45 " \"version\": \"1.0\","
46 " \"initial_locale\" : \"en-US\","
47 " \"initial_timezone\" : \"US/Pacific\","
48 " \"keyboard_layout\" : \"xkb:us::eng\","
49 " \"setup_content\" : {"
51 " \"eula_page\" : \"file:///opt/oem/eula/en-US/eula.html\","
54 " \"eula_page\" : \"file:///opt/oem/eula/ru-RU/eula.html\","
57 " \"eula_page\" : \"file:///opt/oem/eula/en/eula.html\","
62 " \"hwid_mask\": \"ZGA*34\","
63 " \"initial_locale\" : \"ja\","
64 " \"initial_timezone\" : \"Asia/Tokyo\","
65 " \"keyboard_layout\" : \"mozc-jp\","
68 " \"hwid_mask\": \"Mario 1?3*\","
69 " \"initial_locale\" : \"ru-RU\","
70 " \"initial_timezone\" : \"Europe/Moscow\","
71 " \"keyboard_layout\" : \"xkb:ru::rus\","
76 const char kBadManifest
[] = "{\"version\": \"1\"}";
78 const char kGoodServicesManifest
[] =
80 " \"version\": \"1.0\","
81 " \"default_apps\": [\n"
82 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
83 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
85 " \"localized_content\": {\n"
87 " \"default_apps_folder_name\": \"EN-US OEM Name\"\n"
90 " \"default_apps_folder_name\": \"EN OEM Name\"\n"
93 " \"default_apps_folder_name\": \"Default OEM Name\"\n"
98 const char kDummyCustomizationID
[] = "test-dummy";
100 } // anonymous namespace
104 using ::testing::DoAll
;
105 using ::testing::NotNull
;
106 using ::testing::Return
;
107 using ::testing::SetArgumentPointee
;
110 TEST(StartupCustomizationDocumentTest
, Basic
) {
111 system::ScopedFakeStatisticsProvider fake_statistics_provider
;
113 // hardware_class selects the appropriate entry in hwid_map in the manifest.
114 fake_statistics_provider
.SetMachineStatistic("hardware_class", "Mario 12345");
115 StartupCustomizationDocument
customization(&fake_statistics_provider
,
116 kGoodStartupManifest
);
117 EXPECT_EQ("ru-RU", customization
.initial_locale());
118 EXPECT_EQ("Europe/Moscow", customization
.initial_timezone());
119 EXPECT_EQ("xkb:ru::rus", customization
.keyboard_layout());
121 EXPECT_EQ("file:///opt/oem/eula/en-US/eula.html",
122 customization
.GetEULAPage("en-US"));
123 EXPECT_EQ("file:///opt/oem/eula/ru-RU/eula.html",
124 customization
.GetEULAPage("ru-RU"));
125 EXPECT_EQ("file:///opt/oem/eula/en/eula.html",
126 customization
.GetEULAPage("ja"));
129 TEST(StartupCustomizationDocumentTest
, VPD
) {
130 system::ScopedFakeStatisticsProvider fake_statistics_provider
;
132 // hardware_class selects the appropriate entry in hwid_map in the manifest.
133 fake_statistics_provider
.SetMachineStatistic("hardware_class", "Mario 12345");
134 fake_statistics_provider
.SetMachineStatistic("initial_locale", "ja");
135 fake_statistics_provider
.SetMachineStatistic("initial_timezone",
137 fake_statistics_provider
.SetMachineStatistic("keyboard_layout", "mozc-jp");
138 StartupCustomizationDocument
customization(&fake_statistics_provider
,
139 kGoodStartupManifest
);
140 EXPECT_TRUE(customization
.IsReady());
141 EXPECT_EQ("ja", customization
.initial_locale());
142 EXPECT_EQ("Asia/Tokyo", customization
.initial_timezone());
143 EXPECT_EQ("mozc-jp", customization
.keyboard_layout());
146 TEST(StartupCustomizationDocumentTest
, BadManifest
) {
147 system::ScopedFakeStatisticsProvider fake_statistics_provider
;
148 StartupCustomizationDocument
customization(&fake_statistics_provider
,
150 EXPECT_FALSE(customization
.IsReady());
153 class TestURLFetcherCallback
{
155 scoped_ptr
<net::FakeURLFetcher
> CreateURLFetcher(
157 net::URLFetcherDelegate
* d
,
158 const std::string
& response_data
,
159 net::HttpStatusCode response_code
,
160 net::URLRequestStatus::Status status
) {
161 scoped_ptr
<net::FakeURLFetcher
> fetcher(
162 new net::FakeURLFetcher(url
, d
, response_data
, response_code
, status
));
163 OnRequestCreate(url
, fetcher
.get());
164 return fetcher
.Pass();
166 MOCK_METHOD2(OnRequestCreate
,
167 void(const GURL
&, net::FakeURLFetcher
*));
170 void AddMimeHeader(const GURL
& url
, net::FakeURLFetcher
* fetcher
) {
171 scoped_refptr
<net::HttpResponseHeaders
> download_headers
=
172 new net::HttpResponseHeaders("");
173 download_headers
->AddHeader("Content-Type: application/json");
174 fetcher
->set_response_headers(download_headers
);
177 class MockExternalProviderVisitor
178 : public extensions::ExternalProviderInterface::VisitorInterface
{
180 MockExternalProviderVisitor() {}
182 MOCK_METHOD6(OnExternalExtensionFileFound
,
183 bool(const std::string
&,
184 const base::Version
*,
185 const base::FilePath
&,
186 extensions::Manifest::Location
,
189 MOCK_METHOD6(OnExternalExtensionUpdateUrlFound
,
190 bool(const std::string
&,
193 extensions::Manifest::Location
,
196 MOCK_METHOD1(OnExternalProviderReady
,
197 void(const extensions::ExternalProviderInterface
* provider
));
200 class ServicesCustomizationDocumentTest
: public testing::Test
{
202 ServicesCustomizationDocumentTest()
204 base::Bind(&TestURLFetcherCallback::CreateURLFetcher
,
205 base::Unretained(&url_callback_
))) {
209 virtual void SetUp() override
{
210 ServicesCustomizationDocument::InitializeForTesting();
212 DBusThreadManager::Initialize();
213 NetworkHandler::Initialize();
215 const NetworkState
* default_network
=
216 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
217 std::string default_network_path
=
218 default_network
? default_network
->path() : "";
220 NetworkPortalDetector::InitializeForTesting(&network_portal_detector_
);
221 NetworkPortalDetector::CaptivePortalState online_state
;
222 online_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE
;
223 online_state
.response_code
= 204;
225 default_network
? default_network
->guid() : std::string();
226 network_portal_detector_
.SetDefaultNetworkForTesting(guid
);
228 network_portal_detector_
.SetDetectionResultsForTesting(
232 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_
);
233 ServicesCustomizationDocument::RegisterPrefs(local_state_
.registry());
236 virtual void TearDown() override
{
237 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL
);
238 NetworkHandler::Shutdown();
239 DBusThreadManager::Shutdown();
240 NetworkPortalDetector::InitializeForTesting(NULL
);
242 ServicesCustomizationDocument::ShutdownForTesting();
245 void RunUntilIdle() {
246 base::RunLoop().RunUntilIdle();
249 void AddCustomizationIdToVp(const std::string
& id
) {
250 fake_statistics_provider_
.SetMachineStatistic(system::kCustomizationIdKey
,
254 void AddExpectedManifest(const std::string
& id
,
255 const std::string
& manifest
) {
256 GURL
url(base::StringPrintf(ServicesCustomizationDocument::kManifestUrl
,
258 factory_
.SetFakeResponse(url
,
261 net::URLRequestStatus::SUCCESS
);
262 EXPECT_CALL(url_callback_
, OnRequestCreate(url
, _
))
264 .WillRepeatedly(Invoke(AddMimeHeader
));
267 void AddManifestNotFound(const std::string
& id
) {
268 GURL
url(base::StringPrintf(ServicesCustomizationDocument::kManifestUrl
,
270 factory_
.SetFakeResponse(url
,
273 net::URLRequestStatus::SUCCESS
);
274 EXPECT_CALL(url_callback_
, OnRequestCreate(url
, _
))
276 .WillRepeatedly(Invoke(AddMimeHeader
));
279 scoped_ptr
<TestingProfile
> CreateProfile() {
280 TestingProfile::Builder profile_builder
;
281 PrefServiceMockFactory factory
;
282 scoped_refptr
<user_prefs::PrefRegistrySyncable
> registry(
283 new user_prefs::PrefRegistrySyncable
);
284 scoped_ptr
<PrefServiceSyncable
> prefs(
285 factory
.CreateSyncable(registry
.get()));
286 chrome::RegisterUserProfilePrefs(registry
.get());
287 profile_builder
.SetPrefService(prefs
.Pass());
288 return profile_builder
.Build();
292 system::ScopedFakeStatisticsProvider fake_statistics_provider_
;
293 content::TestBrowserThreadBundle thread_bundle_
;
294 TestingPrefServiceSimple local_state_
;
295 TestURLFetcherCallback url_callback_
;
296 net::FakeURLFetcherFactory factory_
;
297 NetworkPortalDetectorTestImpl network_portal_detector_
;
300 TEST_F(ServicesCustomizationDocumentTest
, Basic
) {
301 AddCustomizationIdToVp(kDummyCustomizationID
);
302 AddExpectedManifest(kDummyCustomizationID
, kGoodServicesManifest
);
304 ServicesCustomizationDocument
* doc
=
305 ServicesCustomizationDocument::GetInstance();
306 EXPECT_FALSE(doc
->IsReady());
308 doc
->StartFetching();
310 EXPECT_TRUE(doc
->IsReady());
313 EXPECT_FALSE(doc
->GetDefaultWallpaperUrl(&wallpaper_url
));
314 EXPECT_EQ("", wallpaper_url
.spec());
316 std::vector
<std::string
> default_apps
;
317 EXPECT_TRUE(doc
->GetDefaultApps(&default_apps
));
318 ASSERT_EQ(default_apps
.size(), 2u);
320 EXPECT_EQ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", default_apps
[0]);
321 EXPECT_EQ("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", default_apps
[1]);
323 EXPECT_EQ("EN-US OEM Name", doc
->GetOemAppsFolderName("en-US"));
324 EXPECT_EQ("EN OEM Name", doc
->GetOemAppsFolderName("en"));
325 EXPECT_EQ("Default OEM Name", doc
->GetOemAppsFolderName("ru"));
328 TEST_F(ServicesCustomizationDocumentTest
, NoCustomizationIdInVpd
) {
329 ServicesCustomizationDocument
* doc
=
330 ServicesCustomizationDocument::GetInstance();
331 EXPECT_FALSE(doc
->IsReady());
333 scoped_ptr
<TestingProfile
> profile
= CreateProfile();
334 extensions::ExternalLoader
* loader
= doc
->CreateExternalLoader(profile
.get());
337 MockExternalProviderVisitor visitor
;
338 scoped_ptr
<extensions::ExternalProviderImpl
> provider(
339 new extensions::ExternalProviderImpl(
343 extensions::Manifest::EXTERNAL_PREF
,
344 extensions::Manifest::EXTERNAL_PREF_DOWNLOAD
,
345 extensions::Extension::FROM_WEBSTORE
|
346 extensions::Extension::WAS_INSTALLED_BY_DEFAULT
));
348 EXPECT_CALL(visitor
, OnExternalExtensionFileFound(_
, _
, _
, _
, _
, _
))
350 EXPECT_CALL(visitor
, OnExternalExtensionUpdateUrlFound(_
, _
, _
, _
, _
, _
))
352 EXPECT_CALL(visitor
, OnExternalProviderReady(_
))
355 // Manually request a load.
357 loader
->StartLoading();
358 Mock::VerifyAndClearExpectations(&visitor
);
361 // Empty customization is used when there is no customization ID in VPD.
362 EXPECT_TRUE(doc
->IsReady());
365 TEST_F(ServicesCustomizationDocumentTest
, DefaultApps
) {
366 AddCustomizationIdToVp(kDummyCustomizationID
);
367 AddExpectedManifest(kDummyCustomizationID
, kGoodServicesManifest
);
369 ServicesCustomizationDocument
* doc
=
370 ServicesCustomizationDocument::GetInstance();
371 EXPECT_FALSE(doc
->IsReady());
373 scoped_ptr
<TestingProfile
> profile
= CreateProfile();
374 extensions::ExternalLoader
* loader
= doc
->CreateExternalLoader(profile
.get());
377 app_list::AppListSyncableServiceFactory::GetInstance()->
378 SetTestingFactoryAndUse(
380 &app_list::AppListSyncableServiceFactory::BuildInstanceFor
);
382 MockExternalProviderVisitor visitor
;
383 scoped_ptr
<extensions::ExternalProviderImpl
> provider(
384 new extensions::ExternalProviderImpl(
388 extensions::Manifest::EXTERNAL_PREF
,
389 extensions::Manifest::EXTERNAL_PREF_DOWNLOAD
,
390 extensions::Extension::FROM_WEBSTORE
|
391 extensions::Extension::WAS_INSTALLED_BY_DEFAULT
));
393 EXPECT_CALL(visitor
, OnExternalExtensionFileFound(_
, _
, _
, _
, _
, _
))
395 EXPECT_CALL(visitor
, OnExternalExtensionUpdateUrlFound(_
, _
, _
, _
, _
, _
))
397 EXPECT_CALL(visitor
, OnExternalProviderReady(_
))
400 // Manually request a load.
401 loader
->StartLoading();
402 Mock::VerifyAndClearExpectations(&visitor
);
404 EXPECT_CALL(visitor
, OnExternalExtensionFileFound(_
, _
, _
, _
, _
, _
))
406 EXPECT_CALL(visitor
, OnExternalExtensionUpdateUrlFound(_
, _
, _
, _
, _
, _
))
408 EXPECT_CALL(visitor
, OnExternalProviderReady(_
))
412 EXPECT_TRUE(doc
->IsReady());
414 app_list::AppListSyncableService
* service
=
415 app_list::AppListSyncableServiceFactory::GetForProfile(profile
.get());
416 ASSERT_TRUE(service
);
417 EXPECT_EQ("EN OEM Name", service
->GetOemFolderNameForTest());
420 TEST_F(ServicesCustomizationDocumentTest
, CustomizationManifestNotFound
) {
421 AddCustomizationIdToVp(kDummyCustomizationID
);
422 AddManifestNotFound(kDummyCustomizationID
);
424 ServicesCustomizationDocument
* doc
=
425 ServicesCustomizationDocument::GetInstance();
426 EXPECT_FALSE(doc
->IsReady());
428 scoped_ptr
<TestingProfile
> profile
= CreateProfile();
429 extensions::ExternalLoader
* loader
= doc
->CreateExternalLoader(profile
.get());
432 MockExternalProviderVisitor visitor
;
433 scoped_ptr
<extensions::ExternalProviderImpl
> provider(
434 new extensions::ExternalProviderImpl(
438 extensions::Manifest::EXTERNAL_PREF
,
439 extensions::Manifest::EXTERNAL_PREF_DOWNLOAD
,
440 extensions::Extension::FROM_WEBSTORE
|
441 extensions::Extension::WAS_INSTALLED_BY_DEFAULT
));
443 EXPECT_CALL(visitor
, OnExternalExtensionFileFound(_
, _
, _
, _
, _
, _
))
445 EXPECT_CALL(visitor
, OnExternalExtensionUpdateUrlFound(_
, _
, _
, _
, _
, _
))
447 EXPECT_CALL(visitor
, OnExternalProviderReady(_
))
450 // Manually request a load.
451 loader
->StartLoading();
452 Mock::VerifyAndClearExpectations(&visitor
);
454 EXPECT_CALL(visitor
, OnExternalExtensionFileFound(_
, _
, _
, _
, _
, _
))
456 EXPECT_CALL(visitor
, OnExternalExtensionUpdateUrlFound(_
, _
, _
, _
, _
, _
))
458 EXPECT_CALL(visitor
, OnExternalProviderReady(_
))
462 EXPECT_TRUE(doc
->IsReady());
465 } // namespace chromeos