Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / login / kiosk_browsertest.cc
blobe8b1dc7e243dab061ad607fa34dbebab12b757a9
1 // Copyright (c) 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 <vector>
7 #include "ash/desktop_background/desktop_background_controller.h"
8 #include "ash/desktop_background/desktop_background_controller_observer.h"
9 #include "ash/shell.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/files/file_util.h"
13 #include "base/location.h"
14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/path_service.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/run_loop.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_util.h"
21 #include "base/synchronization/lock.h"
22 #include "chrome/browser/chrome_notification_types.h"
23 #include "chrome/browser/chromeos/app_mode/fake_cws.h"
24 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
25 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
26 #include "chrome/browser/chromeos/file_manager/fake_disk_mount_manager.h"
27 #include "chrome/browser/chromeos/login/app_launch_controller.h"
28 #include "chrome/browser/chromeos/login/startup_utils.h"
29 #include "chrome/browser/chromeos/login/test/app_window_waiter.h"
30 #include "chrome/browser/chromeos/login/test/oobe_base_test.h"
31 #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
32 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
33 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
34 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
35 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
36 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
37 #include "chrome/browser/chromeos/login/wizard_controller.h"
38 #include "chrome/browser/chromeos/ownership/fake_owner_settings_service.h"
39 #include "chrome/browser/chromeos/policy/device_local_account.h"
40 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
41 #include "chrome/browser/chromeos/profiles/profile_helper.h"
42 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h"
43 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
44 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
45 #include "chrome/browser/extensions/extension_service.h"
46 #include "chrome/browser/extensions/updater/local_extension_cache.h"
47 #include "chrome/browser/profiles/profile_impl.h"
48 #include "chrome/browser/profiles/profile_manager.h"
49 #include "chrome/browser/profiles/profiles_state.h"
50 #include "chrome/browser/signin/signin_manager_factory.h"
51 #include "chrome/browser/ui/webui/chromeos/login/kiosk_app_menu_handler.h"
52 #include "chrome/common/chrome_constants.h"
53 #include "chrome/common/chrome_paths.h"
54 #include "chrome/common/pref_names.h"
55 #include "chromeos/chromeos_paths.h"
56 #include "chromeos/chromeos_switches.h"
57 #include "chromeos/dbus/cryptohome_client.h"
58 #include "chromeos/disks/disk_mount_manager.h"
59 #include "chromeos/settings/cros_settings_provider.h"
60 #include "components/signin/core/browser/signin_manager.h"
61 #include "components/signin/core/common/signin_pref_names.h"
62 #include "content/public/browser/browser_thread.h"
63 #include "content/public/browser/notification_observer.h"
64 #include "content/public/browser/notification_registrar.h"
65 #include "content/public/browser/notification_service.h"
66 #include "content/public/browser/web_ui.h"
67 #include "content/public/test/browser_test_utils.h"
68 #include "extensions/browser/app_window/app_window.h"
69 #include "extensions/browser/app_window/app_window_registry.h"
70 #include "extensions/browser/app_window/native_app_window.h"
71 #include "extensions/browser/extension_system.h"
72 #include "extensions/common/manifest.h"
73 #include "extensions/components/native_app_window/native_app_window_views.h"
74 #include "extensions/test/extension_test_message_listener.h"
75 #include "extensions/test/result_catcher.h"
76 #include "google_apis/gaia/gaia_constants.h"
77 #include "google_apis/gaia/gaia_switches.h"
78 #include "google_apis/gaia/gaia_urls.h"
79 #include "net/test/embedded_test_server/embedded_test_server.h"
80 #include "ui/base/accelerators/accelerator.h"
82 namespace em = enterprise_management;
84 namespace chromeos {
86 namespace {
88 // This is a simple test app that creates an app window and immediately closes
89 // it again. Webstore data json is in
90 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/
91 // detail/ggbflgnkafappblpkiflbgpmkfdpnhhe
92 const char kTestKioskApp[] = "ggbflgnkafappblpkiflbgpmkfdpnhhe";
94 // This app creates a window and declares usage of the identity API in its
95 // manifest, so we can test device robot token minting via the identity API.
96 // Webstore data json is in
97 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/
98 // detail/ibjkkfdnfcaoapcpheeijckmpcfkifob
99 const char kTestEnterpriseKioskApp[] = "ibjkkfdnfcaoapcpheeijckmpcfkifob";
101 // An offline enable test app. Webstore data json is in
102 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/
103 // detail/ajoggoflpgplnnjkjamcmbepjdjdnpdp
104 // An app profile with version 1.0.0 installed is in
105 // chrome/test/data/chromeos/app_mode/offline_enabled_app_profile
106 // The version 2.0.0 crx is in
107 // chrome/test/data/chromeos/app_mode/webstore/downloads/
108 const char kTestOfflineEnabledKioskApp[] = "ajoggoflpgplnnjkjamcmbepjdjdnpdp";
110 // An app to test local fs data persistence across app update. V1 app writes
111 // data into local fs. V2 app reads and verifies the data.
112 // Webstore data json is in
113 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/
114 // detail/bmbpicmpniaclbbpdkfglgipkkebnbjf
115 const char kTestLocalFsKioskApp[] = "bmbpicmpniaclbbpdkfglgipkkebnbjf";
117 // An app to test local access to file systems via the
118 // chrome.fileSystem.requestFileSystem API.
119 // Webstore data json is in
120 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/
121 // detail/aaedpojejpghjkedenggihopfhfijcko
122 const char kTestGetVolumeListKioskApp[] = "aaedpojejpghjkedenggihopfhfijcko";
124 // Testing apps for testing kiosk multi-app feature.
125 const char kTestPrimaryKioskApp[] = "ceobkcclegcliomogfoeoheahogoecgl";
126 const char kTestSecondaryApp1[] = "ihplaomghjbeafnpnjkhppmfpnmdihgd";
127 const char kTestSecondaryApp2[] = "fiehokkcgaojmbhfhlpiheggjhaedjoc";
128 const char kTestSecondaryApp3[] = "aabnpdpieclcikafhdkkpldcaodmfoai";
130 // Fake usb stick mount path.
131 const char kFakeUsbMountPathUpdatePass[] =
132 "chromeos/app_mode/external_update/update_pass";
133 const char kFakeUsbMountPathNoManifest[] =
134 "chromeos/app_mode/external_update/no_manifest";
135 const char kFakeUsbMountPathBadManifest[] =
136 "chromeos/app_mode/external_update/bad_manifest";
137 const char kFakeUsbMountPathLowerAppVersion[] =
138 "chromeos/app_mode/external_update/lower_app_version";
139 const char kFakeUsbMountPathLowerCrxVersion[] =
140 "chromeos/app_mode/external_update/lower_crx_version";
141 const char kFakeUsbMountPathBadCrx[] =
142 "chromeos/app_mode/external_update/bad_crx";
144 // Timeout while waiting for network connectivity during tests.
145 const int kTestNetworkTimeoutSeconds = 1;
147 // Email of owner account for test.
148 const char kTestOwnerEmail[] = "owner@example.com";
150 const char kTestEnterpriseAccountId[] = "enterprise-kiosk-app@localhost";
151 const char kTestEnterpriseServiceAccountId[] = "service_account@example.com";
152 const char kTestRefreshToken[] = "fake-refresh-token";
153 const char kTestUserinfoToken[] = "fake-userinfo-token";
154 const char kTestLoginToken[] = "fake-login-token";
155 const char kTestAccessToken[] = "fake-access-token";
156 const char kTestClientId[] = "fake-client-id";
157 const char kTestAppScope[] =
158 "https://www.googleapis.com/auth/userinfo.profile";
160 // Test JS API.
161 const char kLaunchAppForTestNewAPI[] =
162 "login.AccountPickerScreen.runAppForTesting";
163 const char kLaunchAppForTestOldAPI[] =
164 "login.AppsMenuButton.runAppForTesting";
165 const char kCheckDiagnosticModeNewAPI[] =
166 "$('oobe').confirmDiagnosticMode_";
167 const char kCheckDiagnosticModeOldAPI[] =
168 "$('show-apps-button').confirmDiagnosticMode_";
170 // Helper function for GetConsumerKioskAutoLaunchStatusCallback.
171 void ConsumerKioskAutoLaunchStatusCheck(
172 KioskAppManager::ConsumerKioskAutoLaunchStatus* out_status,
173 const base::Closure& runner_quit_task,
174 KioskAppManager::ConsumerKioskAutoLaunchStatus in_status) {
175 LOG(INFO) << "KioskAppManager::ConsumerKioskModeStatus = " << in_status;
176 *out_status = in_status;
177 runner_quit_task.Run();
180 // Helper KioskAppManager::EnableKioskModeCallback implementation.
181 void ConsumerKioskModeAutoStartLockCheck(
182 bool* out_locked,
183 const base::Closure& runner_quit_task,
184 bool in_locked) {
185 LOG(INFO) << "kiosk locked = " << in_locked;
186 *out_locked = in_locked;
187 runner_quit_task.Run();
190 // Helper function for WaitForNetworkTimeOut.
191 void OnNetworkWaitTimedOut(const base::Closure& runner_quit_task) {
192 runner_quit_task.Run();
195 // Helper function for LockFileThread.
196 void LockAndUnlock(scoped_ptr<base::Lock> lock) {
197 lock->Acquire();
198 lock->Release();
201 bool IsAppInstalled(const std::string app_id) {
202 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
203 DCHECK(app_profile);
204 const extensions::Extension* app =
205 extensions::ExtensionSystem::Get(app_profile)
206 ->extension_service()
207 ->GetInstalledExtension(app_id);
208 return app != nullptr;
211 // Helper functions for CanConfigureNetwork mock.
212 class ScopedCanConfigureNetwork {
213 public:
214 ScopedCanConfigureNetwork(bool can_configure, bool needs_owner_auth)
215 : can_configure_(can_configure),
216 needs_owner_auth_(needs_owner_auth),
217 can_configure_network_callback_(
218 base::Bind(&ScopedCanConfigureNetwork::CanConfigureNetwork,
219 base::Unretained(this))),
220 needs_owner_auth_callback_(base::Bind(
221 &ScopedCanConfigureNetwork::NeedsOwnerAuthToConfigureNetwork,
222 base::Unretained(this))) {
223 AppLaunchController::SetCanConfigureNetworkCallbackForTesting(
224 &can_configure_network_callback_);
225 AppLaunchController::SetNeedOwnerAuthToConfigureNetworkCallbackForTesting(
226 &needs_owner_auth_callback_);
228 ~ScopedCanConfigureNetwork() {
229 AppLaunchController::SetCanConfigureNetworkCallbackForTesting(NULL);
230 AppLaunchController::SetNeedOwnerAuthToConfigureNetworkCallbackForTesting(
231 NULL);
234 bool CanConfigureNetwork() {
235 return can_configure_;
238 bool NeedsOwnerAuthToConfigureNetwork() {
239 return needs_owner_auth_;
242 private:
243 bool can_configure_;
244 bool needs_owner_auth_;
245 AppLaunchController::ReturnBoolCallback can_configure_network_callback_;
246 AppLaunchController::ReturnBoolCallback needs_owner_auth_callback_;
247 DISALLOW_COPY_AND_ASSIGN(ScopedCanConfigureNetwork);
250 // Helper class to wait until a js condition becomes true.
251 class JsConditionWaiter {
252 public:
253 JsConditionWaiter(content::WebContents* web_contents,
254 const std::string& js)
255 : web_contents_(web_contents),
256 js_(js) {
259 void Wait() {
260 if (CheckJs())
261 return;
263 base::RepeatingTimer<JsConditionWaiter> check_timer;
264 check_timer.Start(
265 FROM_HERE,
266 base::TimeDelta::FromMilliseconds(10),
267 this,
268 &JsConditionWaiter::OnTimer);
270 runner_ = new content::MessageLoopRunner;
271 runner_->Run();
274 private:
275 bool CheckJs() {
276 bool result;
277 CHECK(content::ExecuteScriptAndExtractBool(
278 web_contents_,
279 "window.domAutomationController.send(!!(" + js_ + "));",
280 &result));
281 return result;
284 void OnTimer() {
285 DCHECK(runner_.get());
286 if (CheckJs())
287 runner_->Quit();
290 content::WebContents* web_contents_;
291 const std::string js_;
292 scoped_refptr<content::MessageLoopRunner> runner_;
294 DISALLOW_COPY_AND_ASSIGN(JsConditionWaiter);
297 class KioskFakeDiskMountManager : public file_manager::FakeDiskMountManager {
298 public:
299 KioskFakeDiskMountManager() {}
301 ~KioskFakeDiskMountManager() override {}
303 void set_usb_mount_path(const std::string& usb_mount_path) {
304 usb_mount_path_ = usb_mount_path;
307 void MountUsbStick() {
308 DCHECK(!usb_mount_path_.empty());
309 MountPath(usb_mount_path_, "", "", chromeos::MOUNT_TYPE_DEVICE);
312 void UnMountUsbStick() {
313 DCHECK(!usb_mount_path_.empty());
314 UnmountPath(usb_mount_path_,
315 UNMOUNT_OPTIONS_NONE,
316 disks::DiskMountManager::UnmountPathCallback());
319 private:
320 std::string usb_mount_path_;
322 DISALLOW_COPY_AND_ASSIGN(KioskFakeDiskMountManager);
325 class AppDataLoadWaiter : public KioskAppManagerObserver {
326 public:
327 AppDataLoadWaiter(KioskAppManager* manager,
328 const std::string& app_id,
329 const std::string& version)
330 : runner_(NULL),
331 manager_(manager),
332 wait_type_(WAIT_FOR_CRX_CACHE),
333 loaded_(false),
334 quit_(false),
335 app_id_(app_id),
336 version_(version) {
337 manager_->AddObserver(this);
340 ~AppDataLoadWaiter() override { manager_->RemoveObserver(this); }
342 void Wait() {
343 wait_type_ = WAIT_FOR_CRX_CACHE;
344 if (quit_)
345 return;
346 runner_ = new content::MessageLoopRunner;
347 runner_->Run();
350 void WaitForAppData() {
351 wait_type_ = WAIT_FOR_APP_DATA;
352 if (quit_ || IsAppDataLoaded())
353 return;
354 runner_ = new content::MessageLoopRunner;
355 runner_->Run();
358 bool loaded() const { return loaded_; }
360 private:
361 enum WaitType {
362 WAIT_FOR_CRX_CACHE,
363 WAIT_FOR_APP_DATA,
366 // KioskAppManagerObserver overrides:
367 void OnKioskAppDataChanged(const std::string& app_id) override {
368 if (wait_type_ != WAIT_FOR_APP_DATA ||
369 app_id != app_id_ ||
370 !IsAppDataLoaded()) {
371 return;
374 loaded_ = true;
375 quit_ = true;
376 if (runner_.get())
377 runner_->Quit();
380 void OnKioskAppDataLoadFailure(const std::string& app_id) override {
381 if (wait_type_ != WAIT_FOR_APP_DATA || app_id != app_id_)
382 return;
384 loaded_ = false;
385 quit_ = true;
386 if (runner_.get())
387 runner_->Quit();
390 void OnKioskExtensionLoadedInCache(const std::string& app_id) override {
391 if (wait_type_ != WAIT_FOR_CRX_CACHE)
392 return;
394 std::string cached_version;
395 base::FilePath file_path;
396 if (!manager_->GetCachedCrx(app_id_, &file_path, &cached_version))
397 return;
398 if (version_ != cached_version)
399 return;
400 loaded_ = true;
401 quit_ = true;
402 if (runner_.get())
403 runner_->Quit();
406 void OnKioskExtensionDownloadFailed(const std::string& app_id) override {
407 if (wait_type_ != WAIT_FOR_CRX_CACHE)
408 return;
410 loaded_ = false;
411 quit_ = true;
412 if (runner_.get())
413 runner_->Quit();
416 bool IsAppDataLoaded() {
417 KioskAppManager::App app;
418 return manager_->GetApp(app_id_, &app) && !app.is_loading;
421 scoped_refptr<content::MessageLoopRunner> runner_;
422 KioskAppManager* manager_;
423 WaitType wait_type_;
424 bool loaded_;
425 bool quit_;
426 std::string app_id_;
427 std::string version_;
429 DISALLOW_COPY_AND_ASSIGN(AppDataLoadWaiter);
432 } // namespace
434 class KioskTest : public OobeBaseTest {
435 public:
436 KioskTest()
437 : settings_helper_(false),
438 use_consumer_kiosk_mode_(true),
439 fake_cws_(new FakeCWS) {
440 set_exit_when_last_browser_closes(false);
443 ~KioskTest() override {}
445 protected:
446 void SetUp() override {
447 test_app_id_ = kTestKioskApp;
448 set_test_app_version("1.0.0");
449 set_test_crx_file(test_app_id() + ".crx");
450 needs_background_networking_ = true;
451 mock_user_manager_.reset(new MockUserManager);
452 ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(true);
453 AppLaunchController::SkipSplashWaitForTesting();
454 AppLaunchController::SetNetworkWaitForTesting(kTestNetworkTimeoutSeconds);
456 OobeBaseTest::SetUp();
459 void TearDown() override {
460 ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(false);
461 OobeBaseTest::TearDown();
464 void SetUpOnMainThread() override {
465 OobeBaseTest::SetUpOnMainThread();
466 // Needed to avoid showing Gaia screen instead of owner signin for
467 // consumer network down test cases.
468 StartupUtils::MarkDeviceRegistered(base::Closure());
469 settings_helper_.ReplaceProvider(kAccountsPrefDeviceLocalAccounts);
470 owner_settings_service_ = settings_helper_.CreateOwnerSettingsService(
471 ProfileManager::GetPrimaryUserProfile());
474 void TearDownOnMainThread() override {
475 settings_helper_.RestoreProvider();
476 AppLaunchController::SetNetworkTimeoutCallbackForTesting(NULL);
477 AppLaunchSigninScreen::SetUserManagerForTesting(NULL);
479 OobeBaseTest::TearDownOnMainThread();
481 // Clean up while main thread still runs.
482 // See http://crbug.com/176659.
483 KioskAppManager::Get()->CleanUp();
486 void SetUpCommandLine(base::CommandLine* command_line) override {
487 OobeBaseTest::SetUpCommandLine(command_line);
488 fake_cws_->Init(embedded_test_server());
491 void LaunchApp(const std::string& app_id, bool diagnostic_mode) {
492 bool new_kiosk_ui = KioskAppMenuHandler::EnableNewKioskUI();
493 GetLoginUI()->CallJavascriptFunction(new_kiosk_ui ?
494 kLaunchAppForTestNewAPI : kLaunchAppForTestOldAPI,
495 base::StringValue(app_id),
496 base::FundamentalValue(diagnostic_mode));
499 void ReloadKioskApps() {
500 SetupTestAppUpdateCheck();
502 // Remove then add to ensure NOTIFICATION_KIOSK_APPS_LOADED fires.
503 KioskAppManager::Get()->RemoveApp(test_app_id_,
504 owner_settings_service_.get());
505 KioskAppManager::Get()->AddApp(test_app_id_, owner_settings_service_.get());
508 void FireKioskAppSettingsChanged() {
509 KioskAppManager::Get()->UpdateAppData();
512 void SetupTestAppUpdateCheck() {
513 if (!test_app_version().empty()) {
514 fake_cws_->SetUpdateCrx(
515 test_app_id(), test_crx_file(), test_app_version());
519 void ReloadAutolaunchKioskApps() {
520 SetupTestAppUpdateCheck();
522 KioskAppManager::Get()->AddApp(test_app_id_, owner_settings_service_.get());
523 KioskAppManager::Get()->SetAutoLaunchApp(test_app_id_,
524 owner_settings_service_.get());
527 void StartUIForAppLaunch() {
528 if (use_consumer_kiosk_mode_)
529 EnableConsumerKioskMode();
531 // Start UI
532 chromeos::WizardController::SkipPostLoginScreensForTesting();
533 chromeos::WizardController* wizard_controller =
534 chromeos::WizardController::default_controller();
535 if (wizard_controller) {
536 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
537 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
538 } else {
539 // No wizard and running with an existing profile and it should land
540 // on account picker when new kiosk UI is enabled. Otherwise, just
541 // wait for the login signal from Gaia.
542 if (KioskAppMenuHandler::EnableNewKioskUI())
543 OobeScreenWaiter(OobeDisplay::SCREEN_ACCOUNT_PICKER).Wait();
544 else
545 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
549 void PrepareAppLaunch() {
550 // Start UI
551 StartUIForAppLaunch();
553 // Wait for the Kiosk App configuration to reload.
554 content::WindowedNotificationObserver apps_loaded_signal(
555 chrome::NOTIFICATION_KIOSK_APPS_LOADED,
556 content::NotificationService::AllSources());
557 ReloadKioskApps();
558 apps_loaded_signal.Wait();
561 void StartAppLaunchFromLoginScreen(const base::Closure& network_setup_cb) {
562 PrepareAppLaunch();
564 if (!network_setup_cb.is_null())
565 network_setup_cb.Run();
567 LaunchApp(test_app_id(), false);
570 const extensions::Extension* GetInstalledApp() {
571 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
572 return extensions::ExtensionSystem::Get(app_profile)->
573 extension_service()->GetInstalledExtension(test_app_id_);
576 const Version& GetInstalledAppVersion() {
577 return *GetInstalledApp()->version();
580 extensions::Manifest::Location GetInstalledAppLocation() {
581 return GetInstalledApp()->location();
584 void WaitForAppLaunchWithOptions(bool check_launch_data, bool terminate_app) {
585 ExtensionTestMessageListener
586 launch_data_check_listener("launchData.isKioskSession = true", false);
588 // Wait for the Kiosk App to launch.
589 content::WindowedNotificationObserver(
590 chrome::NOTIFICATION_KIOSK_APP_LAUNCHED,
591 content::NotificationService::AllSources()).Wait();
593 // Default profile switches to app profile after app is launched.
594 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
595 ASSERT_TRUE(app_profile);
597 // Check ChromeOS preference is initialized.
598 EXPECT_TRUE(
599 static_cast<ProfileImpl*>(app_profile)->chromeos_preferences_);
601 // Check installer status.
602 EXPECT_EQ(chromeos::KioskAppLaunchError::NONE,
603 chromeos::KioskAppLaunchError::Get());
605 // Check if the kiosk webapp is really installed for the default profile.
606 const extensions::Extension* app =
607 extensions::ExtensionSystem::Get(app_profile)->
608 extension_service()->GetInstalledExtension(test_app_id_);
609 EXPECT_TRUE(app);
611 // App should appear with its window.
612 extensions::AppWindowRegistry* app_window_registry =
613 extensions::AppWindowRegistry::Get(app_profile);
614 extensions::AppWindow* window =
615 AppWindowWaiter(app_window_registry, test_app_id_).Wait();
616 EXPECT_TRUE(window);
618 // Login screen should be gone or fading out.
619 chromeos::LoginDisplayHost* login_display_host =
620 chromeos::LoginDisplayHostImpl::default_host();
621 EXPECT_TRUE(
622 login_display_host == NULL ||
623 login_display_host->GetNativeWindow()->layer()->GetTargetOpacity() ==
624 0.0f);
626 // Terminate the app.
627 if (terminate_app)
628 window->GetBaseWindow()->Close();
630 // Wait until the app terminates if it is still running.
631 if (!app_window_registry->GetAppWindowsForApp(test_app_id_).empty())
632 content::RunMessageLoop();
634 // Check that the app had been informed that it is running in a kiosk
635 // session.
636 if (check_launch_data)
637 EXPECT_TRUE(launch_data_check_listener.was_satisfied());
640 void WaitForAppLaunchSuccess() {
641 WaitForAppLaunchWithOptions(true /* check_launch_data */,
642 true /* terminate_app */);
645 void WaitForAppLaunchNetworkTimeout() {
646 if (GetAppLaunchController()->network_wait_timedout())
647 return;
649 scoped_refptr<content::MessageLoopRunner> runner =
650 new content::MessageLoopRunner;
652 base::Closure callback = base::Bind(
653 &OnNetworkWaitTimedOut, runner->QuitClosure());
654 AppLaunchController::SetNetworkTimeoutCallbackForTesting(&callback);
656 runner->Run();
658 CHECK(GetAppLaunchController()->network_wait_timedout());
659 AppLaunchController::SetNetworkTimeoutCallbackForTesting(NULL);
662 void EnableConsumerKioskMode() {
663 scoped_ptr<bool> locked(new bool(false));
664 scoped_refptr<content::MessageLoopRunner> runner =
665 new content::MessageLoopRunner;
666 KioskAppManager::Get()->EnableConsumerKioskAutoLaunch(
667 base::Bind(&ConsumerKioskModeAutoStartLockCheck,
668 locked.get(),
669 runner->QuitClosure()));
670 runner->Run();
671 EXPECT_TRUE(*locked.get());
674 KioskAppManager::ConsumerKioskAutoLaunchStatus
675 GetConsumerKioskModeStatus() {
676 KioskAppManager::ConsumerKioskAutoLaunchStatus status =
677 static_cast<KioskAppManager::ConsumerKioskAutoLaunchStatus>(-1);
678 scoped_refptr<content::MessageLoopRunner> runner =
679 new content::MessageLoopRunner;
680 KioskAppManager::Get()->GetConsumerKioskAutoLaunchStatus(
681 base::Bind(&ConsumerKioskAutoLaunchStatusCheck,
682 &status,
683 runner->QuitClosure()));
684 runner->Run();
685 CHECK_NE(status,
686 static_cast<KioskAppManager::ConsumerKioskAutoLaunchStatus>(-1));
687 return status;
690 void RunAppLaunchNetworkDownTest() {
691 mock_user_manager()->SetActiveUser(kTestOwnerEmail);
692 AppLaunchSigninScreen::SetUserManagerForTesting(mock_user_manager());
694 // Mock network could be configured with owner's password.
695 ScopedCanConfigureNetwork can_configure_network(true, true);
697 // Start app launch and wait for network connectivity timeout.
698 StartAppLaunchFromLoginScreen(SimulateNetworkOfflineClosure());
699 OobeScreenWaiter splash_waiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH);
700 splash_waiter.Wait();
701 WaitForAppLaunchNetworkTimeout();
703 // Configure network link should be visible.
704 JsExpect("$('splash-config-network').hidden == false");
706 // Set up fake user manager with an owner for the test.
707 static_cast<LoginDisplayHostImpl*>(LoginDisplayHostImpl::default_host())
708 ->GetOobeUI()->ShowOobeUI(false);
710 // Configure network should bring up lock screen for owner.
711 OobeScreenWaiter lock_screen_waiter(OobeDisplay::SCREEN_ACCOUNT_PICKER);
712 static_cast<AppLaunchSplashScreenActor::Delegate*>(GetAppLaunchController())
713 ->OnConfigureNetwork();
714 lock_screen_waiter.Wait();
716 // There should be only one owner pod on this screen.
717 JsExpect("$('pod-row').alwaysFocusSinglePod");
719 // A network error screen should be shown after authenticating.
720 OobeScreenWaiter error_screen_waiter(OobeDisplay::SCREEN_ERROR_MESSAGE);
721 static_cast<AppLaunchSigninScreen::Delegate*>(GetAppLaunchController())
722 ->OnOwnerSigninSuccess();
723 error_screen_waiter.Wait();
725 ASSERT_TRUE(GetAppLaunchController()->showing_network_dialog());
727 SimulateNetworkOnline();
728 WaitForAppLaunchSuccess();
731 AppLaunchController* GetAppLaunchController() {
732 return chromeos::LoginDisplayHostImpl::default_host()
733 ->GetAppLaunchController();
736 // Returns a lock that is holding a task on the FILE thread. Any tasks posted
737 // to the FILE thread after this call will be blocked until the returned
738 // lock is released.
739 // This can be used to prevent app installation from completing until some
740 // other conditions are checked and triggered. For example, this can be used
741 // to trigger the network screen during app launch without racing with the
742 // app launching process itself.
743 scoped_ptr<base::AutoLock> LockFileThread() {
744 scoped_ptr<base::Lock> lock(new base::Lock);
745 scoped_ptr<base::AutoLock> auto_lock(new base::AutoLock(*lock));
746 content::BrowserThread::PostTask(
747 content::BrowserThread::FILE, FROM_HERE,
748 base::Bind(&LockAndUnlock, base::Passed(&lock)));
749 return auto_lock.Pass();
752 MockUserManager* mock_user_manager() { return mock_user_manager_.get(); }
754 void set_test_app_id(const std::string& test_app_id) {
755 test_app_id_ = test_app_id;
757 const std::string& test_app_id() const { return test_app_id_; }
758 void set_test_app_version(const std::string& version) {
759 test_app_version_ = version;
761 const std::string& test_app_version() const { return test_app_version_; }
762 void set_test_crx_file(const std::string& filename) {
763 test_crx_file_ = filename;
765 const std::string& test_crx_file() const { return test_crx_file_; }
766 FakeCWS* fake_cws() { return fake_cws_.get(); }
768 void set_use_consumer_kiosk_mode(bool use) {
769 use_consumer_kiosk_mode_ = use;
772 ScopedCrosSettingsTestHelper settings_helper_;
773 scoped_ptr<FakeOwnerSettingsService> owner_settings_service_;
775 private:
776 bool use_consumer_kiosk_mode_;
777 std::string test_app_id_;
778 std::string test_app_version_;
779 std::string test_crx_file_;
780 scoped_ptr<FakeCWS> fake_cws_;
781 scoped_ptr<MockUserManager> mock_user_manager_;
783 DISALLOW_COPY_AND_ASSIGN(KioskTest);
786 IN_PROC_BROWSER_TEST_F(KioskTest, InstallAndLaunchApp) {
787 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
788 WaitForAppLaunchSuccess();
789 KioskAppManager::App app;
790 ASSERT_TRUE(KioskAppManager::Get()->GetApp(test_app_id(), &app));
791 EXPECT_FALSE(app.was_auto_launched_with_zero_delay);
792 EXPECT_EQ(extensions::Manifest::EXTERNAL_PREF, GetInstalledAppLocation());
795 IN_PROC_BROWSER_TEST_F(KioskTest, ZoomSupport) {
796 ExtensionTestMessageListener
797 app_window_loaded_listener("appWindowLoaded", false);
798 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
799 app_window_loaded_listener.WaitUntilSatisfied();
801 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
802 ASSERT_TRUE(app_profile);
804 extensions::AppWindowRegistry* app_window_registry =
805 extensions::AppWindowRegistry::Get(app_profile);
806 extensions::AppWindow* window =
807 AppWindowWaiter(app_window_registry, test_app_id()).Wait();
808 ASSERT_TRUE(window);
810 // Gets the original width of the app window.
811 int original_width;
812 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
813 window->web_contents(),
814 "window.domAutomationController.setAutomationId(0);"
815 "window.domAutomationController.send(window.innerWidth);",
816 &original_width));
818 native_app_window::NativeAppWindowViews* native_app_window_views =
819 static_cast<native_app_window::NativeAppWindowViews*>(
820 window->GetBaseWindow());
821 ui::AcceleratorTarget* accelerator_target =
822 static_cast<ui::AcceleratorTarget*>(native_app_window_views);
824 // Zoom in. Text is bigger and content window width becomes smaller.
825 accelerator_target->AcceleratorPressed(ui::Accelerator(
826 ui::VKEY_ADD, ui::EF_CONTROL_DOWN));
827 int width_zoomed_in;
828 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
829 window->web_contents(),
830 "window.domAutomationController.setAutomationId(0);"
831 "window.domAutomationController.send(window.innerWidth);",
832 &width_zoomed_in));
833 DCHECK_LT(width_zoomed_in, original_width);
835 // Go back to normal. Window width is restored.
836 accelerator_target->AcceleratorPressed(ui::Accelerator(
837 ui::VKEY_0, ui::EF_CONTROL_DOWN));
838 int width_zoom_normal;
839 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
840 window->web_contents(),
841 "window.domAutomationController.setAutomationId(0);"
842 "window.domAutomationController.send(window.innerWidth);",
843 &width_zoom_normal));
844 DCHECK_EQ(width_zoom_normal, original_width);
846 // Zoom out. Text is smaller and content window width becomes larger.
847 accelerator_target->AcceleratorPressed(ui::Accelerator(
848 ui::VKEY_SUBTRACT, ui::EF_CONTROL_DOWN));
849 int width_zoomed_out;
850 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
851 window->web_contents(),
852 "window.domAutomationController.setAutomationId(0);"
853 "window.domAutomationController.send(window.innerWidth);",
854 &width_zoomed_out));
855 DCHECK_GT(width_zoomed_out, original_width);
857 // Terminate the app.
858 window->GetBaseWindow()->Close();
859 content::RunAllPendingInMessageLoop();
862 IN_PROC_BROWSER_TEST_F(KioskTest, NotSignedInWithGAIAAccount) {
863 // Tests that the kiosk session is not considered to be logged in with a GAIA
864 // account.
865 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
866 WaitForAppLaunchSuccess();
867 EXPECT_EQ(extensions::Manifest::EXTERNAL_PREF, GetInstalledAppLocation());
869 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
870 ASSERT_TRUE(app_profile);
871 EXPECT_FALSE(
872 SigninManagerFactory::GetForProfile(app_profile)->IsAuthenticated());
875 IN_PROC_BROWSER_TEST_F(KioskTest, PRE_LaunchAppNetworkDown) {
876 // Tests the network down case for the initial app download and launch.
877 RunAppLaunchNetworkDownTest();
880 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppNetworkDown) {
881 // Tests the network down case for launching an existing app that is
882 // installed in PRE_LaunchAppNetworkDown.
883 RunAppLaunchNetworkDownTest();
886 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppWithNetworkConfigAccelerator) {
887 ScopedCanConfigureNetwork can_configure_network(true, false);
889 // Block app loading until the network screen is shown.
890 scoped_ptr<base::AutoLock> lock = LockFileThread();
892 // Start app launch and wait for network connectivity timeout.
893 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
894 OobeScreenWaiter splash_waiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH);
895 splash_waiter.Wait();
897 // A network error screen should be shown after authenticating.
898 OobeScreenWaiter error_screen_waiter(OobeDisplay::SCREEN_ERROR_MESSAGE);
899 // Simulate Ctrl+Alt+N accelerator.
900 GetLoginUI()->CallJavascriptFunction(
901 "cr.ui.Oobe.handleAccelerator",
902 base::StringValue("app_launch_network_config"));
903 error_screen_waiter.Wait();
904 ASSERT_TRUE(GetAppLaunchController()->showing_network_dialog());
906 // Continue button should be visible since we are online.
907 JsExpect("$('continue-network-config-btn').hidden == false");
909 // Click on [Continue] button.
910 ASSERT_TRUE(content::ExecuteScript(
911 GetLoginUI()->GetWebContents(),
912 "(function() {"
913 "var e = new Event('click');"
914 "$('continue-network-config-btn').dispatchEvent(e);"
915 "})();"));
917 // Let app launching resume.
918 lock.reset();
920 WaitForAppLaunchSuccess();
923 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppNetworkDownConfigureNotAllowed) {
924 // Mock network could not be configured.
925 ScopedCanConfigureNetwork can_configure_network(false, true);
927 // Start app launch and wait for network connectivity timeout.
928 StartAppLaunchFromLoginScreen(SimulateNetworkOfflineClosure());
929 OobeScreenWaiter splash_waiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH);
930 splash_waiter.Wait();
931 WaitForAppLaunchNetworkTimeout();
933 // Configure network link should not be visible.
934 JsExpect("$('splash-config-network').hidden == true");
936 // Network becomes online and app launch is resumed.
937 SimulateNetworkOnline();
938 WaitForAppLaunchSuccess();
941 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppNetworkPortal) {
942 // Mock network could be configured without the owner password.
943 ScopedCanConfigureNetwork can_configure_network(true, false);
945 // Start app launch with network portal state.
946 StartAppLaunchFromLoginScreen(SimulateNetworkPortalClosure());
947 OobeScreenWaiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH)
948 .WaitNoAssertCurrentScreen();
949 WaitForAppLaunchNetworkTimeout();
951 // Network error should show up automatically since this test does not
952 // require owner auth to configure network.
953 OobeScreenWaiter(OobeDisplay::SCREEN_ERROR_MESSAGE).Wait();
955 ASSERT_TRUE(GetAppLaunchController()->showing_network_dialog());
956 SimulateNetworkOnline();
957 WaitForAppLaunchSuccess();
960 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppUserCancel) {
961 // Make fake_cws_ return empty update response.
962 set_test_app_version("");
963 StartAppLaunchFromLoginScreen(SimulateNetworkOfflineClosure());
964 OobeScreenWaiter splash_waiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH);
965 splash_waiter.Wait();
967 settings_helper_.SetBoolean(
968 kAccountsPrefDeviceLocalAccountAutoLoginBailoutEnabled, true);
969 content::WindowedNotificationObserver signal(
970 chrome::NOTIFICATION_APP_TERMINATING,
971 content::NotificationService::AllSources());
972 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
973 base::StringValue("app_launch_bailout"));
974 signal.Wait();
975 EXPECT_EQ(chromeos::KioskAppLaunchError::USER_CANCEL,
976 chromeos::KioskAppLaunchError::Get());
979 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchInDiagnosticMode) {
980 PrepareAppLaunch();
981 SimulateNetworkOnline();
983 LaunchApp(kTestKioskApp, true);
985 content::WebContents* login_contents = GetLoginUI()->GetWebContents();
987 bool new_kiosk_ui = KioskAppMenuHandler::EnableNewKioskUI();
988 JsConditionWaiter(login_contents, new_kiosk_ui ?
989 kCheckDiagnosticModeNewAPI : kCheckDiagnosticModeOldAPI).Wait();
991 std::string diagnosticMode(new_kiosk_ui ?
992 kCheckDiagnosticModeNewAPI : kCheckDiagnosticModeOldAPI);
993 ASSERT_TRUE(content::ExecuteScript(
994 login_contents,
995 "(function() {"
996 "var e = new Event('click');" +
997 diagnosticMode + "."
998 "okButton_.dispatchEvent(e);"
999 "})();"));
1001 WaitForAppLaunchSuccess();
1002 EXPECT_EQ(extensions::Manifest::EXTERNAL_PREF, GetInstalledAppLocation());
1005 IN_PROC_BROWSER_TEST_F(KioskTest, AutolaunchWarningCancel) {
1006 EnableConsumerKioskMode();
1008 chromeos::WizardController::SkipPostLoginScreensForTesting();
1009 chromeos::WizardController* wizard_controller =
1010 chromeos::WizardController::default_controller();
1011 CHECK(wizard_controller);
1013 // Start login screen after configuring auto launch app since the warning
1014 // is triggered when switching to login screen.
1015 wizard_controller->AdvanceToScreen(WizardController::kNetworkScreenName);
1016 ReloadAutolaunchKioskApps();
1017 EXPECT_FALSE(KioskAppManager::Get()->GetAutoLaunchApp().empty());
1018 EXPECT_FALSE(KioskAppManager::Get()->IsAutoLaunchEnabled());
1019 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1021 // Wait for the auto launch warning come up.
1022 content::WindowedNotificationObserver(
1023 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
1024 content::NotificationService::AllSources()).Wait();
1025 GetLoginUI()->CallJavascriptFunction(
1026 "login.AutolaunchScreen.confirmAutoLaunchForTesting",
1027 base::FundamentalValue(false));
1029 // Wait for the auto launch warning to go away.
1030 content::WindowedNotificationObserver(
1031 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_COMPLETED,
1032 content::NotificationService::AllSources()).Wait();
1034 EXPECT_FALSE(KioskAppManager::Get()->IsAutoLaunchEnabled());
1037 IN_PROC_BROWSER_TEST_F(KioskTest, AutolaunchWarningConfirm) {
1038 EnableConsumerKioskMode();
1040 chromeos::WizardController::SkipPostLoginScreensForTesting();
1041 chromeos::WizardController* wizard_controller =
1042 chromeos::WizardController::default_controller();
1043 CHECK(wizard_controller);
1045 // Start login screen after configuring auto launch app since the warning
1046 // is triggered when switching to login screen.
1047 wizard_controller->AdvanceToScreen(WizardController::kNetworkScreenName);
1048 ReloadAutolaunchKioskApps();
1049 EXPECT_FALSE(KioskAppManager::Get()->GetAutoLaunchApp().empty());
1050 EXPECT_FALSE(KioskAppManager::Get()->IsAutoLaunchEnabled());
1051 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1053 // Wait for the auto launch warning come up.
1054 content::WindowedNotificationObserver(
1055 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
1056 content::NotificationService::AllSources()).Wait();
1057 GetLoginUI()->CallJavascriptFunction(
1058 "login.AutolaunchScreen.confirmAutoLaunchForTesting",
1059 base::FundamentalValue(true));
1061 // Wait for the auto launch warning to go away.
1062 content::WindowedNotificationObserver(
1063 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_COMPLETED,
1064 content::NotificationService::AllSources()).Wait();
1066 EXPECT_FALSE(KioskAppManager::Get()->GetAutoLaunchApp().empty());
1067 EXPECT_TRUE(KioskAppManager::Get()->IsAutoLaunchEnabled());
1069 WaitForAppLaunchSuccess();
1071 KioskAppManager::App app;
1072 ASSERT_TRUE(KioskAppManager::Get()->GetApp(test_app_id(), &app));
1073 EXPECT_TRUE(app.was_auto_launched_with_zero_delay);
1074 EXPECT_EQ(extensions::Manifest::EXTERNAL_PREF, GetInstalledAppLocation());
1077 IN_PROC_BROWSER_TEST_F(KioskTest, KioskEnableCancel) {
1078 chromeos::WizardController::SkipPostLoginScreensForTesting();
1079 chromeos::WizardController* wizard_controller =
1080 chromeos::WizardController::default_controller();
1081 CHECK(wizard_controller);
1083 // Check Kiosk mode status.
1084 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
1085 GetConsumerKioskModeStatus());
1087 // Wait for the login UI to come up and switch to the kiosk_enable screen.
1088 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1089 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
1090 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
1091 base::StringValue("kiosk_enable"));
1093 // Wait for the kiosk_enable screen to show and cancel the screen.
1094 content::WindowedNotificationObserver(
1095 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_VISIBLE,
1096 content::NotificationService::AllSources()).Wait();
1097 GetLoginUI()->CallJavascriptFunction(
1098 "login.KioskEnableScreen.enableKioskForTesting",
1099 base::FundamentalValue(false));
1101 // Wait for the kiosk_enable screen to disappear.
1102 content::WindowedNotificationObserver(
1103 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_COMPLETED,
1104 content::NotificationService::AllSources()).Wait();
1106 // Check that the status still says configurable.
1107 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
1108 GetConsumerKioskModeStatus());
1111 IN_PROC_BROWSER_TEST_F(KioskTest, KioskEnableConfirmed) {
1112 // Start UI, find menu entry for this app and launch it.
1113 chromeos::WizardController::SkipPostLoginScreensForTesting();
1114 chromeos::WizardController* wizard_controller =
1115 chromeos::WizardController::default_controller();
1116 CHECK(wizard_controller);
1118 // Check Kiosk mode status.
1119 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
1120 GetConsumerKioskModeStatus());
1122 // Wait for the login UI to come up and switch to the kiosk_enable screen.
1123 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1124 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
1125 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
1126 base::StringValue("kiosk_enable"));
1128 // Wait for the kiosk_enable screen to show and cancel the screen.
1129 content::WindowedNotificationObserver(
1130 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_VISIBLE,
1131 content::NotificationService::AllSources()).Wait();
1132 GetLoginUI()->CallJavascriptFunction(
1133 "login.KioskEnableScreen.enableKioskForTesting",
1134 base::FundamentalValue(true));
1136 // Wait for the signal that indicates Kiosk Mode is enabled.
1137 content::WindowedNotificationObserver(
1138 chrome::NOTIFICATION_KIOSK_ENABLED,
1139 content::NotificationService::AllSources()).Wait();
1140 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_ENABLED,
1141 GetConsumerKioskModeStatus());
1144 IN_PROC_BROWSER_TEST_F(KioskTest, KioskEnableAfter2ndSigninScreen) {
1145 chromeos::WizardController::SkipPostLoginScreensForTesting();
1146 chromeos::WizardController* wizard_controller =
1147 chromeos::WizardController::default_controller();
1148 CHECK(wizard_controller);
1150 // Check Kiosk mode status.
1151 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
1152 GetConsumerKioskModeStatus());
1154 // Wait for the login UI to come up and switch to the kiosk_enable screen.
1155 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1156 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
1157 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
1158 base::StringValue("kiosk_enable"));
1160 // Wait for the kiosk_enable screen to show and cancel the screen.
1161 content::WindowedNotificationObserver(
1162 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_VISIBLE,
1163 content::NotificationService::AllSources()).Wait();
1164 GetLoginUI()->CallJavascriptFunction(
1165 "login.KioskEnableScreen.enableKioskForTesting",
1166 base::FundamentalValue(false));
1168 // Wait for the kiosk_enable screen to disappear.
1169 content::WindowedNotificationObserver(
1170 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_COMPLETED,
1171 content::NotificationService::AllSources()).Wait();
1173 // Show signin screen again.
1174 chromeos::LoginDisplayHostImpl::default_host()->StartSignInScreen(
1175 LoginScreenContext());
1176 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
1178 // Show kiosk enable screen again.
1179 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
1180 base::StringValue("kiosk_enable"));
1182 // And it should show up.
1183 content::WindowedNotificationObserver(
1184 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_VISIBLE,
1185 content::NotificationService::AllSources()).Wait();
1188 IN_PROC_BROWSER_TEST_F(KioskTest, DoNotLaunchWhenUntrusted) {
1189 PrepareAppLaunch();
1190 SimulateNetworkOnline();
1192 // Make cros settings untrusted.
1193 settings_helper_.SetTrustedStatus(
1194 CrosSettingsProvider::PERMANENTLY_UNTRUSTED);
1196 // Check that the attempt to start a kiosk app fails with an error.
1197 LaunchApp(test_app_id(), false);
1198 bool ignored = false;
1199 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
1200 GetLoginUI()->GetWebContents(),
1201 "if (cr.ui.Oobe.getInstance().errorMessageWasShownForTesting_) {"
1202 " window.domAutomationController.send(true);"
1203 "} else {"
1204 " cr.ui.Oobe.showSignInError = function("
1205 " loginAttempts, message, link, helpId) {"
1206 " window.domAutomationController.send(true);"
1207 " };"
1208 "}",
1209 &ignored));
1212 // Verifies that a consumer device does not auto-launch kiosk mode when cros
1213 // settings are untrusted.
1214 IN_PROC_BROWSER_TEST_F(KioskTest, NoConsumerAutoLaunchWhenUntrusted) {
1215 EnableConsumerKioskMode();
1217 // Wait for and confirm the auto-launch warning.
1218 chromeos::WizardController::SkipPostLoginScreensForTesting();
1219 chromeos::WizardController* wizard_controller =
1220 chromeos::WizardController::default_controller();
1221 ASSERT_TRUE(wizard_controller);
1222 wizard_controller->AdvanceToScreen(WizardController::kNetworkScreenName);
1223 ReloadAutolaunchKioskApps();
1224 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1225 content::WindowedNotificationObserver(
1226 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
1227 content::NotificationService::AllSources()).Wait();
1228 GetLoginUI()->CallJavascriptFunction(
1229 "login.AutolaunchScreen.confirmAutoLaunchForTesting",
1230 base::FundamentalValue(true));
1232 // Make cros settings untrusted.
1233 settings_helper_.SetTrustedStatus(
1234 CrosSettingsProvider::PERMANENTLY_UNTRUSTED);
1236 // Check that the attempt to auto-launch a kiosk app fails with an error.
1237 OobeScreenWaiter(OobeDisplay::SCREEN_ERROR_MESSAGE).Wait();
1240 // Verifies available volumes for kiosk apps in kiosk session.
1241 IN_PROC_BROWSER_TEST_F(KioskTest, GetVolumeList) {
1242 set_test_app_id(kTestGetVolumeListKioskApp);
1243 set_test_app_version("0.1");
1244 set_test_crx_file(test_app_id() + ".crx");
1246 extensions::ResultCatcher catcher;
1247 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
1248 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
1251 // Verifies that an enterprise device does not auto-launch kiosk mode when cros
1252 // settings are untrusted.
1253 IN_PROC_BROWSER_TEST_F(KioskTest, NoEnterpriseAutoLaunchWhenUntrusted) {
1254 PrepareAppLaunch();
1255 SimulateNetworkOnline();
1257 // Make cros settings untrusted.
1258 settings_helper_.SetTrustedStatus(
1259 CrosSettingsProvider::PERMANENTLY_UNTRUSTED);
1261 // Trigger the code that handles auto-launch on enterprise devices. This would
1262 // normally be called from ShowLoginWizard(), which runs so early that it is
1263 // not possible to inject an auto-launch policy before it runs.
1264 LoginDisplayHost* login_display_host = LoginDisplayHostImpl::default_host();
1265 ASSERT_TRUE(login_display_host);
1266 login_display_host->StartAppLaunch(test_app_id(), false, true);
1268 // Check that no launch has started.
1269 EXPECT_FALSE(login_display_host->GetAppLaunchController());
1272 class KioskUpdateTest : public KioskTest {
1273 public:
1274 KioskUpdateTest() {}
1275 ~KioskUpdateTest() override {}
1277 struct TestAppInfo {
1278 std::string id;
1279 std::string version;
1280 std::string crx_filename;
1281 TestAppInfo() {}
1282 TestAppInfo(const std::string& id,
1283 const std::string& version,
1284 const std::string& crx_filename)
1285 : id(id), version(version), crx_filename(crx_filename) {}
1286 ~TestAppInfo() {}
1289 protected:
1290 void SetUp() override {
1291 fake_disk_mount_manager_ = new KioskFakeDiskMountManager();
1292 disks::DiskMountManager::InitializeForTesting(fake_disk_mount_manager_);
1294 KioskTest::SetUp();
1297 void TearDown() override {
1298 disks::DiskMountManager::Shutdown();
1300 KioskTest::TearDown();
1303 void SetUpOnMainThread() override {
1304 // For update tests, we cache the app in the PRE part, and then we load it
1305 // in the test, so we need to both store the apps list on teardown (so that
1306 // the app manager would accept existing files in its extension cache on the
1307 // next startup) and copy the list to our stub settings provider as well.
1308 settings_helper_.CopyStoredValue(kAccountsPrefDeviceLocalAccounts);
1310 CreateAndInitializeLocalCache();
1312 KioskTest::SetUpOnMainThread();
1315 void TearDownOnMainThread() override {
1316 settings_helper_.StoreCachedDeviceSetting(kAccountsPrefDeviceLocalAccounts);
1317 KioskTest::TearDownOnMainThread();
1320 void PreCacheApp(const std::string& app_id,
1321 const std::string& version,
1322 const std::string& crx_file) {
1323 set_test_app_id(app_id);
1324 set_test_app_version(version);
1325 set_test_crx_file(crx_file);
1327 KioskAppManager* manager = KioskAppManager::Get();
1328 AppDataLoadWaiter waiter(manager, app_id, version);
1329 ReloadKioskApps();
1330 waiter.Wait();
1331 EXPECT_TRUE(waiter.loaded());
1332 std::string cached_version;
1333 base::FilePath file_path;
1334 EXPECT_TRUE(manager->GetCachedCrx(app_id, &file_path, &cached_version));
1335 EXPECT_EQ(version, cached_version);
1338 void UpdateExternalCache(const std::string& version,
1339 const std::string& crx_file) {
1340 set_test_app_version(version);
1341 set_test_crx_file(crx_file);
1342 SetupTestAppUpdateCheck();
1344 KioskAppManager* manager = KioskAppManager::Get();
1345 AppDataLoadWaiter waiter(manager, test_app_id(), version);
1346 KioskAppManager::Get()->UpdateExternalCache();
1347 waiter.Wait();
1348 EXPECT_TRUE(waiter.loaded());
1349 std::string cached_version;
1350 base::FilePath file_path;
1351 EXPECT_TRUE(
1352 manager->GetCachedCrx(test_app_id(), &file_path, &cached_version));
1353 EXPECT_EQ(version, cached_version);
1356 void SetupFakeDiskMountManagerMountPath(const std::string mount_path) {
1357 base::FilePath test_data_dir;
1358 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
1359 test_data_dir = test_data_dir.AppendASCII(mount_path);
1360 fake_disk_mount_manager_->set_usb_mount_path(test_data_dir.value());
1363 void SimulateUpdateAppFromUsbStick(const std::string& usb_mount_path,
1364 bool* app_update_notified,
1365 bool* update_success) {
1366 SetupFakeDiskMountManagerMountPath(usb_mount_path);
1367 KioskAppExternalUpdateWaiter waiter(KioskAppManager::Get(), test_app_id());
1368 fake_disk_mount_manager_->MountUsbStick();
1369 waiter.Wait();
1370 fake_disk_mount_manager_->UnMountUsbStick();
1371 *update_success = waiter.update_success();
1372 *app_update_notified = waiter.app_update_notified();
1375 void PreCacheAndLaunchApp(const std::string& app_id,
1376 const std::string& version,
1377 const std::string& crx_file) {
1378 set_test_app_id(app_id);
1379 set_test_app_version(version);
1380 set_test_crx_file(crx_file);
1381 PrepareAppLaunch();
1382 SimulateNetworkOnline();
1383 LaunchApp(test_app_id(), false);
1384 WaitForAppLaunchSuccess();
1385 EXPECT_EQ(version, GetInstalledAppVersion().GetString());
1388 void LaunchKioskWithSecondaryApps(
1389 const TestAppInfo& primary_app,
1390 const std::vector<TestAppInfo>& secondary_apps) {
1391 // Pre-cache the primary app.
1392 PreCacheApp(primary_app.id, primary_app.version, primary_app.crx_filename);
1394 set_test_app_id(primary_app.id);
1395 fake_cws()->SetNoUpdate(primary_app.id);
1396 for (size_t i = 0; i < secondary_apps.size(); ++i) {
1397 fake_cws()->SetUpdateCrx(secondary_apps[i].id,
1398 secondary_apps[i].crx_filename,
1399 secondary_apps[i].version);
1402 // Launch the primary app.
1403 StartUIForAppLaunch();
1404 SimulateNetworkOnline();
1405 LaunchApp(test_app_id(), false);
1406 WaitForAppLaunchWithOptions(false, true);
1408 // Verify the primary app and the secondary apps are all installed.
1409 EXPECT_EQ(primary_app.version, GetInstalledAppVersion().GetString());
1410 for (size_t i = 0; i < secondary_apps.size(); ++i)
1411 EXPECT_TRUE(IsAppInstalled(secondary_apps[i].id));
1414 void LaunchTestKioskAppWithTwoSecondaryApps() {
1415 TestAppInfo primary_app(kTestPrimaryKioskApp, "1.0.0",
1416 std::string(kTestPrimaryKioskApp) + "-1.0.0.crx");
1418 std::vector<TestAppInfo> secondary_apps;
1419 TestAppInfo secondary_app_1(kTestSecondaryApp1, "1.0.0",
1420 std::string(kTestSecondaryApp1) + "-1.0.0.crx");
1421 secondary_apps.push_back(secondary_app_1);
1422 TestAppInfo secondary_app_2(kTestSecondaryApp2, "1.0.0",
1423 std::string(kTestSecondaryApp2) + "-1.0.0.crx");
1424 secondary_apps.push_back(secondary_app_2);
1426 LaunchKioskWithSecondaryApps(primary_app, secondary_apps);
1429 private:
1430 class KioskAppExternalUpdateWaiter : public KioskAppManagerObserver {
1431 public:
1432 KioskAppExternalUpdateWaiter(KioskAppManager* manager,
1433 const std::string& app_id)
1434 : runner_(NULL),
1435 manager_(manager),
1436 app_id_(app_id),
1437 quit_(false),
1438 update_success_(false),
1439 app_update_notified_(false) {
1440 manager_->AddObserver(this);
1443 ~KioskAppExternalUpdateWaiter() override { manager_->RemoveObserver(this); }
1445 void Wait() {
1446 if (quit_)
1447 return;
1448 runner_ = new content::MessageLoopRunner;
1449 runner_->Run();
1452 bool update_success() const { return update_success_; }
1454 bool app_update_notified() const { return app_update_notified_; }
1456 private:
1457 // KioskAppManagerObserver overrides:
1458 void OnKioskAppCacheUpdated(const std::string& app_id) override {
1459 if (app_id_ != app_id)
1460 return;
1461 app_update_notified_ = true;
1464 void OnKioskAppExternalUpdateComplete(bool success) override {
1465 quit_ = true;
1466 update_success_ = success;
1467 if (runner_.get())
1468 runner_->Quit();
1471 scoped_refptr<content::MessageLoopRunner> runner_;
1472 KioskAppManager* manager_;
1473 const std::string app_id_;
1474 bool quit_;
1475 bool update_success_;
1476 bool app_update_notified_;
1478 DISALLOW_COPY_AND_ASSIGN(KioskAppExternalUpdateWaiter);
1481 // The local cache is supposed to be initialized on chromeos device, and a
1482 // ready flag file will be pre-created to mark the ready state, before chrome
1483 // starts. In order for the tests to run without being on real chromeos
1484 // device, we need to manually create this file.
1485 void CreateAndInitializeLocalCache() {
1486 base::FilePath extension_cache_dir;
1487 CHECK(PathService::Get(chromeos::DIR_DEVICE_EXTENSION_LOCAL_CACHE,
1488 &extension_cache_dir));
1489 base::FilePath cache_init_file = extension_cache_dir.Append(
1490 extensions::LocalExtensionCache::kCacheReadyFlagFileName);
1491 EXPECT_EQ(base::WriteFile(cache_init_file, "", 0), 0);
1494 // Owned by DiskMountManager.
1495 KioskFakeDiskMountManager* fake_disk_mount_manager_;
1497 DISALLOW_COPY_AND_ASSIGN(KioskUpdateTest);
1500 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_LaunchOfflineEnabledAppNoNetwork) {
1501 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1502 "1.0.0",
1503 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1506 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, LaunchOfflineEnabledAppNoNetwork) {
1507 set_test_app_id(kTestOfflineEnabledKioskApp);
1508 StartUIForAppLaunch();
1509 SimulateNetworkOffline();
1510 LaunchApp(test_app_id(), false);
1511 WaitForAppLaunchSuccess();
1513 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1514 EXPECT_EQ(extensions::Manifest::EXTERNAL_PREF, GetInstalledAppLocation());
1517 IN_PROC_BROWSER_TEST_F(KioskUpdateTest,
1518 PRE_LaunchCachedOfflineEnabledAppNoNetwork) {
1519 PreCacheApp(kTestOfflineEnabledKioskApp,
1520 "1.0.0",
1521 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1524 IN_PROC_BROWSER_TEST_F(KioskUpdateTest,
1525 LaunchCachedOfflineEnabledAppNoNetwork) {
1526 set_test_app_id(kTestOfflineEnabledKioskApp);
1527 EXPECT_TRUE(
1528 KioskAppManager::Get()->HasCachedCrx(kTestOfflineEnabledKioskApp));
1529 StartUIForAppLaunch();
1530 SimulateNetworkOffline();
1531 LaunchApp(test_app_id(), false);
1532 WaitForAppLaunchSuccess();
1534 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1535 EXPECT_EQ(extensions::Manifest::EXTERNAL_PREF, GetInstalledAppLocation());
1538 // Network offline, app v1.0 has run before, has cached v2.0 crx and v2.0 should
1539 // be installed and launched during next launch.
1540 IN_PROC_BROWSER_TEST_F(KioskUpdateTest,
1541 PRE_LaunchCachedNewVersionOfflineEnabledAppNoNetwork) {
1542 // Install and launch v1 app.
1543 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1544 "1.0.0",
1545 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1546 // Update cache for v2 app.
1547 UpdateExternalCache("2.0.0",
1548 std::string(kTestOfflineEnabledKioskApp) + ".crx");
1549 // The installed app is still in v1.
1550 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1553 IN_PROC_BROWSER_TEST_F(KioskUpdateTest,
1554 LaunchCachedNewVersionOfflineEnabledAppNoNetwork) {
1555 set_test_app_id(kTestOfflineEnabledKioskApp);
1556 EXPECT_TRUE(KioskAppManager::Get()->HasCachedCrx(test_app_id()));
1558 StartUIForAppLaunch();
1559 SimulateNetworkOffline();
1560 LaunchApp(test_app_id(), false);
1561 WaitForAppLaunchSuccess();
1563 // v2 app should have been installed.
1564 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1565 EXPECT_EQ(extensions::Manifest::EXTERNAL_PREF, GetInstalledAppLocation());
1568 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_LaunchOfflineEnabledAppNoUpdate) {
1569 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1570 "1.0.0",
1571 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1574 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, LaunchOfflineEnabledAppNoUpdate) {
1575 set_test_app_id(kTestOfflineEnabledKioskApp);
1576 fake_cws()->SetNoUpdate(test_app_id());
1578 StartUIForAppLaunch();
1579 SimulateNetworkOnline();
1580 LaunchApp(test_app_id(), false);
1581 WaitForAppLaunchSuccess();
1583 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1584 EXPECT_EQ(extensions::Manifest::EXTERNAL_PREF, GetInstalledAppLocation());
1587 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_LaunchOfflineEnabledAppHasUpdate) {
1588 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1589 "1.0.0",
1590 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1593 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, LaunchOfflineEnabledAppHasUpdate) {
1594 set_test_app_id(kTestOfflineEnabledKioskApp);
1595 fake_cws()->SetUpdateCrx(
1596 test_app_id(), "ajoggoflpgplnnjkjamcmbepjdjdnpdp.crx", "2.0.0");
1598 StartUIForAppLaunch();
1599 SimulateNetworkOnline();
1600 LaunchApp(test_app_id(), false);
1601 WaitForAppLaunchSuccess();
1603 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1604 EXPECT_EQ(extensions::Manifest::EXTERNAL_PREF, GetInstalledAppLocation());
1607 // Pre-cache v1 kiosk app, then launch the app without network,
1608 // plug in usb stick with a v2 app for offline updating.
1609 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_UsbStickUpdateAppNoNetwork) {
1610 PreCacheApp(kTestOfflineEnabledKioskApp,
1611 "1.0.0",
1612 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1614 set_test_app_id(kTestOfflineEnabledKioskApp);
1615 StartUIForAppLaunch();
1616 SimulateNetworkOffline();
1617 LaunchApp(test_app_id(), false);
1618 WaitForAppLaunchSuccess();
1619 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1621 // Simulate mounting of usb stick with v2 app on the stick.
1622 bool update_success;
1623 bool app_update_notified;
1624 SimulateUpdateAppFromUsbStick(
1625 kFakeUsbMountPathUpdatePass, &app_update_notified, &update_success);
1626 EXPECT_TRUE(update_success);
1627 EXPECT_TRUE(app_update_notified);
1629 // The v2 kiosk app is loaded into external cache, but won't be installed
1630 // until next time the device is started.
1631 base::FilePath crx_path;
1632 std::string cached_version;
1633 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1634 test_app_id(), &crx_path, &cached_version));
1635 EXPECT_EQ("2.0.0", cached_version);
1636 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1639 // Restart the device, verify the app has been updated to v2.
1640 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppNoNetwork) {
1641 // Verify the kiosk app has been updated to v2.
1642 set_test_app_id(kTestOfflineEnabledKioskApp);
1643 StartUIForAppLaunch();
1644 SimulateNetworkOffline();
1645 LaunchApp(test_app_id(), false);
1646 WaitForAppLaunchSuccess();
1647 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1650 // Usb stick is plugged in without a manifest file on it.
1651 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppNoManifest) {
1652 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1653 "1.0.0",
1654 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1655 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1657 // Simulate mounting of usb stick with v2 app on the stick.
1658 bool update_success;
1659 bool app_update_notified;
1660 SimulateUpdateAppFromUsbStick(
1661 kFakeUsbMountPathNoManifest, &app_update_notified, &update_success);
1662 EXPECT_FALSE(update_success);
1664 // Kiosk app is not updated.
1665 base::FilePath crx_path;
1666 std::string cached_version;
1667 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1668 test_app_id(), &crx_path, &cached_version));
1669 EXPECT_EQ("1.0.0", cached_version);
1672 // Usb stick is plugged in with a bad manifest file on it.
1673 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppBadManifest) {
1674 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1675 "1.0.0",
1676 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1677 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1679 // Simulate mounting of usb stick with v2 app on the stick.
1680 bool update_success;
1681 bool app_update_notified;
1682 SimulateUpdateAppFromUsbStick(
1683 kFakeUsbMountPathBadManifest, &app_update_notified, &update_success);
1684 EXPECT_FALSE(update_success);
1686 // Kiosk app is not updated.
1687 base::FilePath crx_path;
1688 std::string cached_version;
1689 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1690 test_app_id(), &crx_path, &cached_version));
1691 EXPECT_EQ("1.0.0", cached_version);
1694 // Usb stick is plugged in with a lower version of crx file specified in
1695 // manifest.
1696 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppLowerAppVersion) {
1697 // Precache v2 version of app.
1698 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1699 "2.0.0",
1700 std::string(kTestOfflineEnabledKioskApp) + ".crx");
1701 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1703 // Simulate mounting of usb stick with v1 app on the stick.
1704 bool update_success;
1705 bool app_update_notified;
1706 SimulateUpdateAppFromUsbStick(
1707 kFakeUsbMountPathLowerAppVersion, &app_update_notified, &update_success);
1708 EXPECT_FALSE(update_success);
1710 // Kiosk app is NOT updated to the lower version.
1711 base::FilePath crx_path;
1712 std::string cached_version;
1713 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1714 test_app_id(), &crx_path, &cached_version));
1715 EXPECT_EQ("2.0.0", cached_version);
1718 // Usb stick is plugged in with a v1 crx file, although the manifest says
1719 // this is a v3 version.
1720 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppLowerCrxVersion) {
1721 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1722 "2.0.0",
1723 std::string(kTestOfflineEnabledKioskApp) + ".crx");
1724 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1726 // Simulate mounting of usb stick with v1 crx file on the stick, although
1727 // the manifest says it is v3 app.
1728 bool update_success;
1729 bool app_update_notified;
1730 SimulateUpdateAppFromUsbStick(
1731 kFakeUsbMountPathLowerCrxVersion, &app_update_notified, &update_success);
1732 EXPECT_FALSE(update_success);
1734 // Kiosk app is NOT updated to the lower version.
1735 base::FilePath crx_path;
1736 std::string cached_version;
1737 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1738 test_app_id(), &crx_path, &cached_version));
1739 EXPECT_EQ("2.0.0", cached_version);
1742 // Usb stick is plugged in with a bad crx file.
1743 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppBadCrx) {
1744 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1745 "1.0.0",
1746 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1747 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1749 // Simulate mounting of usb stick with v1 crx file on the stick, although
1750 // the manifest says it is v3 app.
1751 bool update_success;
1752 bool app_update_notified;
1753 SimulateUpdateAppFromUsbStick(
1754 kFakeUsbMountPathBadCrx, &app_update_notified, &update_success);
1755 EXPECT_FALSE(update_success);
1757 // Kiosk app is NOT updated.
1758 base::FilePath crx_path;
1759 std::string cached_version;
1760 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1761 test_app_id(), &crx_path, &cached_version));
1762 EXPECT_EQ("1.0.0", cached_version);
1765 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_PermissionChange) {
1766 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1767 "2.0.0",
1768 std::string(kTestOfflineEnabledKioskApp) + ".crx");
1771 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PermissionChange) {
1772 set_test_app_id(kTestOfflineEnabledKioskApp);
1773 set_test_app_version("2.0.0");
1774 set_test_crx_file(test_app_id() + "_v2_permission_change.crx");
1776 StartUIForAppLaunch();
1777 SimulateNetworkOnline();
1778 LaunchApp(test_app_id(), false);
1779 WaitForAppLaunchSuccess();
1781 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1784 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_PreserveLocalData) {
1785 // Installs v1 app and writes some local data.
1786 set_test_app_id(kTestLocalFsKioskApp);
1787 set_test_app_version("1.0.0");
1788 set_test_crx_file(test_app_id() + ".crx");
1790 extensions::ResultCatcher catcher;
1791 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
1792 WaitForAppLaunchWithOptions(true /* check_launch_data */,
1793 false /* terminate_app */);
1794 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
1797 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PreserveLocalData) {
1798 // Update existing v1 app installed in PRE_PreserveLocalData to v2
1799 // that reads and verifies the local data.
1800 set_test_app_id(kTestLocalFsKioskApp);
1801 set_test_app_version("2.0.0");
1802 set_test_crx_file(test_app_id() + "_v2_read_and_verify_data.crx");
1803 extensions::ResultCatcher catcher;
1804 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
1805 WaitForAppLaunchWithOptions(true /* check_launch_data */,
1806 false /* terminate_app */);
1808 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1809 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
1812 /* ***** Test Kiosk multi-app feature ***** */
1814 // Launch a primary kiosk app which has two secondary apps.
1815 IN_PROC_BROWSER_TEST_F(KioskUpdateTest,
1816 LaunchTestKioskAppWithTwoSecondaryApps) {
1817 LaunchTestKioskAppWithTwoSecondaryApps();
1820 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_UpdateMultiAppKioskRemoveOneApp) {
1821 LaunchTestKioskAppWithTwoSecondaryApps();
1824 // Update the primary app to version 2 which removes one of the secondary app
1825 // from its manifest.
1826 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UpdateMultiAppKioskRemoveOneApp) {
1827 set_test_app_id(kTestPrimaryKioskApp);
1828 fake_cws()->SetUpdateCrx(
1829 kTestPrimaryKioskApp,
1830 std::string(kTestPrimaryKioskApp) + "-2.0.0-1app.crx", "2.0.0");
1831 fake_cws()->SetNoUpdate(kTestSecondaryApp1);
1832 fake_cws()->SetNoUpdate(kTestSecondaryApp2);
1834 StartUIForAppLaunch();
1835 SimulateNetworkOnline();
1836 LaunchApp(test_app_id(), false);
1837 WaitForAppLaunchWithOptions(false, true);
1839 // Verify the secondary app kTestSecondaryApp1 is removed.
1840 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1841 EXPECT_FALSE(IsAppInstalled(kTestSecondaryApp1));
1842 EXPECT_TRUE(IsAppInstalled(kTestSecondaryApp2));
1845 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_UpdateMultiAppKioskAddOneApp) {
1846 LaunchTestKioskAppWithTwoSecondaryApps();
1849 // Update the primary app to version 3 which adds a new secondary app in its
1850 // manifest.
1851 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UpdateMultiAppKioskAddOneApp) {
1852 set_test_app_id(kTestPrimaryKioskApp);
1853 fake_cws()->SetUpdateCrx(
1854 kTestPrimaryKioskApp,
1855 std::string(kTestPrimaryKioskApp) + "-3.0.0-3app.crx", "3.0.0");
1856 fake_cws()->SetNoUpdate(kTestSecondaryApp1);
1857 fake_cws()->SetNoUpdate(kTestSecondaryApp2);
1858 fake_cws()->SetUpdateCrx(kTestSecondaryApp3,
1859 std::string(kTestSecondaryApp3) + "-1.0.0.crx",
1860 "1.0.0");
1862 StartUIForAppLaunch();
1863 SimulateNetworkOnline();
1864 LaunchApp(test_app_id(), false);
1865 WaitForAppLaunchWithOptions(false, true);
1867 // Verify the secondary app kTestSecondaryApp3 is installed.
1868 EXPECT_EQ("3.0.0", GetInstalledAppVersion().GetString());
1869 EXPECT_TRUE(IsAppInstalled(kTestSecondaryApp1));
1870 EXPECT_TRUE(IsAppInstalled(kTestSecondaryApp2));
1871 EXPECT_TRUE(IsAppInstalled(kTestSecondaryApp3));
1874 class KioskEnterpriseTest : public KioskTest {
1875 protected:
1876 KioskEnterpriseTest() {
1877 set_use_consumer_kiosk_mode(false);
1880 void SetUpInProcessBrowserTestFixture() override {
1881 policy::DevicePolicyCrosTestHelper::MarkAsEnterpriseOwnedBy(
1882 kTestOwnerEmail);
1883 settings_helper_.SetCurrentUserIsOwner(false);
1885 KioskTest::SetUpInProcessBrowserTestFixture();
1888 void SetUpOnMainThread() override {
1889 KioskTest::SetUpOnMainThread();
1891 // Configure OAuth authentication.
1892 GaiaUrls* gaia_urls = GaiaUrls::GetInstance();
1894 // This token satisfies the userinfo.email request from
1895 // DeviceOAuth2TokenService used in token validation.
1896 FakeGaia::AccessTokenInfo userinfo_token_info;
1897 userinfo_token_info.token = kTestUserinfoToken;
1898 userinfo_token_info.scopes.insert(
1899 "https://www.googleapis.com/auth/userinfo.email");
1900 userinfo_token_info.audience = gaia_urls->oauth2_chrome_client_id();
1901 userinfo_token_info.email = kTestEnterpriseServiceAccountId;
1902 fake_gaia_->IssueOAuthToken(kTestRefreshToken, userinfo_token_info);
1904 // The any-api access token for accessing the token minting endpoint.
1905 FakeGaia::AccessTokenInfo login_token_info;
1906 login_token_info.token = kTestLoginToken;
1907 login_token_info.scopes.insert(GaiaConstants::kAnyApiOAuth2Scope);
1908 login_token_info.audience = gaia_urls->oauth2_chrome_client_id();
1909 fake_gaia_->IssueOAuthToken(kTestRefreshToken, login_token_info);
1911 // This is the access token requested by the app via the identity API.
1912 FakeGaia::AccessTokenInfo access_token_info;
1913 access_token_info.token = kTestAccessToken;
1914 access_token_info.scopes.insert(kTestAppScope);
1915 access_token_info.audience = kTestClientId;
1916 access_token_info.email = kTestEnterpriseServiceAccountId;
1917 fake_gaia_->IssueOAuthToken(kTestLoginToken, access_token_info);
1919 DeviceOAuth2TokenService* token_service =
1920 DeviceOAuth2TokenServiceFactory::Get();
1921 token_service->SetAndSaveRefreshToken(
1922 kTestRefreshToken, DeviceOAuth2TokenService::StatusCallback());
1923 base::RunLoop().RunUntilIdle();
1926 void ConfigureKioskAppInPolicy(const std::string& account_id,
1927 const std::string& app_id,
1928 const std::string& update_url) {
1929 settings_helper_.SetCurrentUserIsOwner(true);
1930 std::vector<policy::DeviceLocalAccount> accounts;
1931 accounts.push_back(
1932 policy::DeviceLocalAccount(policy::DeviceLocalAccount::TYPE_KIOSK_APP,
1933 account_id, app_id, update_url));
1934 policy::SetDeviceLocalAccounts(owner_settings_service_.get(), accounts);
1935 settings_helper_.SetString(kAccountsPrefDeviceLocalAccountAutoLoginId,
1936 account_id);
1937 settings_helper_.SetString(kServiceAccountIdentity,
1938 kTestEnterpriseServiceAccountId);
1939 settings_helper_.SetCurrentUserIsOwner(false);
1942 private:
1943 DISALLOW_COPY_AND_ASSIGN(KioskEnterpriseTest);
1946 IN_PROC_BROWSER_TEST_F(KioskEnterpriseTest, EnterpriseKioskApp) {
1947 // Prepare Fake CWS to serve app crx.
1948 set_test_app_id(kTestEnterpriseKioskApp);
1949 set_test_app_version("1.0.0");
1950 set_test_crx_file(test_app_id() + ".crx");
1951 SetupTestAppUpdateCheck();
1953 // Configure kTestEnterpriseKioskApp in device policy.
1954 ConfigureKioskAppInPolicy(kTestEnterpriseAccountId,
1955 kTestEnterpriseKioskApp,
1956 "");
1958 PrepareAppLaunch();
1959 LaunchApp(kTestEnterpriseKioskApp, false);
1961 // Wait for the Kiosk App to launch.
1962 content::WindowedNotificationObserver(
1963 chrome::NOTIFICATION_KIOSK_APP_LAUNCHED,
1964 content::NotificationService::AllSources()).Wait();
1966 // Check installer status.
1967 EXPECT_EQ(chromeos::KioskAppLaunchError::NONE,
1968 chromeos::KioskAppLaunchError::Get());
1969 EXPECT_EQ(extensions::Manifest::EXTERNAL_POLICY, GetInstalledAppLocation());
1971 // Wait for the window to appear.
1972 extensions::AppWindow* window =
1973 AppWindowWaiter(
1974 extensions::AppWindowRegistry::Get(
1975 ProfileManager::GetPrimaryUserProfile()),
1976 kTestEnterpriseKioskApp).Wait();
1977 ASSERT_TRUE(window);
1979 // Check whether the app can retrieve an OAuth2 access token.
1980 std::string result;
1981 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
1982 window->web_contents(),
1983 "chrome.identity.getAuthToken({ 'interactive': false }, function(token) {"
1984 " window.domAutomationController.setAutomationId(0);"
1985 " window.domAutomationController.send(token);"
1986 "});",
1987 &result));
1988 EXPECT_EQ(kTestAccessToken, result);
1990 // Verify that the session is not considered to be logged in with a GAIA
1991 // account.
1992 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
1993 ASSERT_TRUE(app_profile);
1994 EXPECT_FALSE(
1995 SigninManagerFactory::GetForProfile(app_profile)->IsAuthenticated());
1997 // Terminate the app.
1998 window->GetBaseWindow()->Close();
1999 content::RunAllPendingInMessageLoop();
2002 IN_PROC_BROWSER_TEST_F(KioskEnterpriseTest, PrivateStore) {
2003 set_test_app_id(kTestEnterpriseKioskApp);
2005 const char kPrivateStoreUpdate[] = "/private_store_update";
2006 net::test_server::EmbeddedTestServer private_server;
2007 ASSERT_TRUE(private_server.InitializeAndWaitUntilReady());
2009 // |private_server| serves crx from test data dir.
2010 base::FilePath test_data_dir;
2011 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
2012 private_server.ServeFilesFromDirectory(test_data_dir);
2014 FakeCWS private_store;
2015 private_store.InitAsPrivateStore(&private_server, kPrivateStoreUpdate);
2016 private_store.SetUpdateCrx(kTestEnterpriseKioskApp,
2017 std::string(kTestEnterpriseKioskApp) + ".crx",
2018 "1.0.0");
2020 // Configure kTestEnterpriseKioskApp in device policy.
2021 ConfigureKioskAppInPolicy(kTestEnterpriseAccountId,
2022 kTestEnterpriseKioskApp,
2023 private_server.GetURL(kPrivateStoreUpdate).spec());
2025 // Meta should be able to be extracted from crx before launching.
2026 KioskAppManager* manager = KioskAppManager::Get();
2027 AppDataLoadWaiter waiter(manager, kTestEnterpriseKioskApp, std::string());
2028 waiter.WaitForAppData();
2030 PrepareAppLaunch();
2031 LaunchApp(kTestEnterpriseKioskApp, false);
2032 WaitForAppLaunchWithOptions(false /* check_launch_data */,
2033 true /* terminate_app */);
2035 // Private store should serve crx and CWS should not.
2036 DCHECK_GT(private_store.GetUpdateCheckCountAndReset(), 0);
2037 DCHECK_EQ(0, fake_cws()->GetUpdateCheckCountAndReset());
2038 EXPECT_EQ(extensions::Manifest::EXTERNAL_POLICY, GetInstalledAppLocation());
2041 // Specialized test fixture for testing kiosk mode on the
2042 // hidden WebUI initialization flow for slow hardware.
2043 class KioskHiddenWebUITest : public KioskTest,
2044 public ash::DesktopBackgroundControllerObserver {
2045 public:
2046 KioskHiddenWebUITest() : wallpaper_loaded_(false) {}
2048 // KioskTest overrides:
2049 void SetUpCommandLine(base::CommandLine* command_line) override {
2050 KioskTest::SetUpCommandLine(command_line);
2051 command_line->AppendSwitch(switches::kDisableBootAnimation);
2054 void SetUpOnMainThread() override {
2055 KioskTest::SetUpOnMainThread();
2056 ash::Shell::GetInstance()->desktop_background_controller()
2057 ->AddObserver(this);
2060 void TearDownOnMainThread() override {
2061 ash::Shell::GetInstance()->desktop_background_controller()
2062 ->RemoveObserver(this);
2063 KioskTest::TearDownOnMainThread();
2066 void WaitForWallpaper() {
2067 if (!wallpaper_loaded_) {
2068 runner_ = new content::MessageLoopRunner;
2069 runner_->Run();
2073 bool wallpaper_loaded() const { return wallpaper_loaded_; }
2075 // ash::DesktopBackgroundControllerObserver overrides:
2076 void OnWallpaperDataChanged() override {
2077 wallpaper_loaded_ = true;
2078 if (runner_.get())
2079 runner_->Quit();
2082 bool wallpaper_loaded_;
2083 scoped_refptr<content::MessageLoopRunner> runner_;
2085 DISALLOW_COPY_AND_ASSIGN(KioskHiddenWebUITest);
2088 IN_PROC_BROWSER_TEST_F(KioskHiddenWebUITest, AutolaunchWarning) {
2089 // Add a device owner.
2090 FakeChromeUserManager* user_manager = new FakeChromeUserManager();
2091 user_manager->AddUser(kTestOwnerEmail);
2092 ScopedUserManagerEnabler enabler(user_manager);
2094 // Set kiosk app to autolaunch.
2095 EnableConsumerKioskMode();
2096 WizardController::SkipPostLoginScreensForTesting();
2097 WizardController* wizard_controller = WizardController::default_controller();
2098 CHECK(wizard_controller);
2100 // Start login screen after configuring auto launch app since the warning
2101 // is triggered when switching to login screen.
2102 wizard_controller->AdvanceToScreen(WizardController::kNetworkScreenName);
2103 ReloadAutolaunchKioskApps();
2104 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
2106 EXPECT_FALSE(KioskAppManager::Get()->GetAutoLaunchApp().empty());
2107 EXPECT_FALSE(KioskAppManager::Get()->IsAutoLaunchEnabled());
2109 // Wait for the auto launch warning come up.
2110 content::WindowedNotificationObserver(
2111 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
2112 content::NotificationService::AllSources()).Wait();
2114 // Wait for the wallpaper to load.
2115 WaitForWallpaper();
2116 EXPECT_TRUE(wallpaper_loaded());
2119 } // namespace chromeos