ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / chromeos / login / kiosk_browsertest.cc
blob890a1b7f17fad43bc683b178fb587a483f8c27d0
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 "ash/desktop_background/desktop_background_controller.h"
6 #include "ash/desktop_background/desktop_background_controller_observer.h"
7 #include "ash/shell.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/files/file_util.h"
11 #include "base/location.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/path_service.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/run_loop.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h"
20 #include "base/synchronization/lock.h"
21 #include "base/thread_task_runner_handle.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/policy/device_policy_cros_browser_test.h"
39 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
40 #include "chrome/browser/chromeos/profiles/profile_helper.h"
41 #include "chrome/browser/chromeos/settings/cros_settings.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/device_settings_service.h"
45 #include "chrome/browser/extensions/extension_service.h"
46 #include "chrome/browser/profiles/profile_impl.h"
47 #include "chrome/browser/profiles/profile_manager.h"
48 #include "chrome/browser/profiles/profiles_state.h"
49 #include "chrome/browser/signin/signin_manager_factory.h"
50 #include "chrome/browser/ui/webui/chromeos/login/kiosk_app_menu_handler.h"
51 #include "chrome/common/chrome_constants.h"
52 #include "chrome/common/chrome_paths.h"
53 #include "chrome/common/pref_names.h"
54 #include "chromeos/chromeos_switches.h"
55 #include "chromeos/dbus/cryptohome_client.h"
56 #include "chromeos/disks/disk_mount_manager.h"
57 #include "chromeos/settings/cros_settings_provider.h"
58 #include "components/signin/core/browser/signin_manager.h"
59 #include "components/signin/core/common/signin_pref_names.h"
60 #include "content/public/browser/browser_thread.h"
61 #include "content/public/browser/notification_observer.h"
62 #include "content/public/browser/notification_registrar.h"
63 #include "content/public/browser/notification_service.h"
64 #include "content/public/browser/web_ui.h"
65 #include "content/public/test/browser_test_utils.h"
66 #include "extensions/browser/app_window/app_window.h"
67 #include "extensions/browser/app_window/app_window_registry.h"
68 #include "extensions/browser/app_window/native_app_window.h"
69 #include "extensions/browser/extension_system.h"
70 #include "extensions/components/native_app_window/native_app_window_views.h"
71 #include "extensions/test/extension_test_message_listener.h"
72 #include "extensions/test/result_catcher.h"
73 #include "google_apis/gaia/gaia_constants.h"
74 #include "google_apis/gaia/gaia_switches.h"
75 #include "google_apis/gaia/gaia_urls.h"
76 #include "net/test/embedded_test_server/embedded_test_server.h"
77 #include "ui/base/accelerators/accelerator.h"
79 namespace em = enterprise_management;
81 namespace chromeos {
83 namespace {
85 // This is a simple test app that creates an app window and immediately closes
86 // it again. Webstore data json is in
87 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/
88 // detail/ggbflgnkafappblpkiflbgpmkfdpnhhe
89 const char kTestKioskApp[] = "ggbflgnkafappblpkiflbgpmkfdpnhhe";
91 // This app creates a window and declares usage of the identity API in its
92 // manifest, so we can test device robot token minting via the identity API.
93 // Webstore data json is in
94 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/
95 // detail/ibjkkfdnfcaoapcpheeijckmpcfkifob
96 const char kTestEnterpriseKioskApp[] = "ibjkkfdnfcaoapcpheeijckmpcfkifob";
98 // An offline enable test app. Webstore data json is in
99 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/
100 // detail/ajoggoflpgplnnjkjamcmbepjdjdnpdp
101 // An app profile with version 1.0.0 installed is in
102 // chrome/test/data/chromeos/app_mode/offline_enabled_app_profile
103 // The version 2.0.0 crx is in
104 // chrome/test/data/chromeos/app_mode/webstore/downloads/
105 const char kTestOfflineEnabledKioskApp[] = "ajoggoflpgplnnjkjamcmbepjdjdnpdp";
107 // An app to test local fs data persistence across app update. V1 app writes
108 // data into local fs. V2 app reads and verifies the data.
109 // Webstore data json is in
110 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/
111 // detail/bmbpicmpniaclbbpdkfglgipkkebnbjf
112 const char kTestLocalFsKioskApp[] = "bmbpicmpniaclbbpdkfglgipkkebnbjf";
114 // Fake usb stick mount path.
115 const char kFakeUsbMountPathUpdatePass[] =
116 "chromeos/app_mode/external_update/update_pass";
117 const char kFakeUsbMountPathNoManifest[] =
118 "chromeos/app_mode/external_update/no_manifest";
119 const char kFakeUsbMountPathBadManifest[] =
120 "chromeos/app_mode/external_update/bad_manifest";
121 const char kFakeUsbMountPathLowerAppVersion[] =
122 "chromeos/app_mode/external_update/lower_app_version";
123 const char kFakeUsbMountPathLowerCrxVersion[] =
124 "chromeos/app_mode/external_update/lower_crx_version";
125 const char kFakeUsbMountPathBadCrx[] =
126 "chromeos/app_mode/external_update/bad_crx";
128 // Timeout while waiting for network connectivity during tests.
129 const int kTestNetworkTimeoutSeconds = 1;
131 // Email of owner account for test.
132 const char kTestOwnerEmail[] = "owner@example.com";
134 const char kTestEnterpriseAccountId[] = "enterprise-kiosk-app@localhost";
135 const char kTestEnterpriseServiceAccountId[] = "service_account@example.com";
136 const char kTestRefreshToken[] = "fake-refresh-token";
137 const char kTestUserinfoToken[] = "fake-userinfo-token";
138 const char kTestLoginToken[] = "fake-login-token";
139 const char kTestAccessToken[] = "fake-access-token";
140 const char kTestClientId[] = "fake-client-id";
141 const char kTestAppScope[] =
142 "https://www.googleapis.com/auth/userinfo.profile";
144 // Test JS API.
145 const char kLaunchAppForTestNewAPI[] =
146 "login.AccountPickerScreen.runAppForTesting";
147 const char kLaunchAppForTestOldAPI[] =
148 "login.AppsMenuButton.runAppForTesting";
149 const char kCheckDiagnosticModeNewAPI[] =
150 "$('oobe').confirmDiagnosticMode_";
151 const char kCheckDiagnosticModeOldAPI[] =
152 "$('show-apps-button').confirmDiagnosticMode_";
154 // Helper function for GetConsumerKioskAutoLaunchStatusCallback.
155 void ConsumerKioskAutoLaunchStatusCheck(
156 KioskAppManager::ConsumerKioskAutoLaunchStatus* out_status,
157 const base::Closure& runner_quit_task,
158 KioskAppManager::ConsumerKioskAutoLaunchStatus in_status) {
159 LOG(INFO) << "KioskAppManager::ConsumerKioskModeStatus = " << in_status;
160 *out_status = in_status;
161 runner_quit_task.Run();
164 // Helper KioskAppManager::EnableKioskModeCallback implementation.
165 void ConsumerKioskModeAutoStartLockCheck(
166 bool* out_locked,
167 const base::Closure& runner_quit_task,
168 bool in_locked) {
169 LOG(INFO) << "kiosk locked = " << in_locked;
170 *out_locked = in_locked;
171 runner_quit_task.Run();
174 // Helper function for WaitForNetworkTimeOut.
175 void OnNetworkWaitTimedOut(const base::Closure& runner_quit_task) {
176 runner_quit_task.Run();
179 // Helper function for LockFileThread.
180 void LockAndUnlock(scoped_ptr<base::Lock> lock) {
181 lock->Acquire();
182 lock->Release();
185 // Helper functions for CanConfigureNetwork mock.
186 class ScopedCanConfigureNetwork {
187 public:
188 ScopedCanConfigureNetwork(bool can_configure, bool needs_owner_auth)
189 : can_configure_(can_configure),
190 needs_owner_auth_(needs_owner_auth),
191 can_configure_network_callback_(
192 base::Bind(&ScopedCanConfigureNetwork::CanConfigureNetwork,
193 base::Unretained(this))),
194 needs_owner_auth_callback_(base::Bind(
195 &ScopedCanConfigureNetwork::NeedsOwnerAuthToConfigureNetwork,
196 base::Unretained(this))) {
197 AppLaunchController::SetCanConfigureNetworkCallbackForTesting(
198 &can_configure_network_callback_);
199 AppLaunchController::SetNeedOwnerAuthToConfigureNetworkCallbackForTesting(
200 &needs_owner_auth_callback_);
202 ~ScopedCanConfigureNetwork() {
203 AppLaunchController::SetCanConfigureNetworkCallbackForTesting(NULL);
204 AppLaunchController::SetNeedOwnerAuthToConfigureNetworkCallbackForTesting(
205 NULL);
208 bool CanConfigureNetwork() {
209 return can_configure_;
212 bool NeedsOwnerAuthToConfigureNetwork() {
213 return needs_owner_auth_;
216 private:
217 bool can_configure_;
218 bool needs_owner_auth_;
219 AppLaunchController::ReturnBoolCallback can_configure_network_callback_;
220 AppLaunchController::ReturnBoolCallback needs_owner_auth_callback_;
221 DISALLOW_COPY_AND_ASSIGN(ScopedCanConfigureNetwork);
224 // Helper class to wait until a js condition becomes true.
225 class JsConditionWaiter {
226 public:
227 JsConditionWaiter(content::WebContents* web_contents,
228 const std::string& js)
229 : web_contents_(web_contents),
230 js_(js) {
233 void Wait() {
234 if (CheckJs())
235 return;
237 base::RepeatingTimer<JsConditionWaiter> check_timer;
238 check_timer.Start(
239 FROM_HERE,
240 base::TimeDelta::FromMilliseconds(10),
241 this,
242 &JsConditionWaiter::OnTimer);
244 runner_ = new content::MessageLoopRunner;
245 runner_->Run();
248 private:
249 bool CheckJs() {
250 bool result;
251 CHECK(content::ExecuteScriptAndExtractBool(
252 web_contents_,
253 "window.domAutomationController.send(!!(" + js_ + "));",
254 &result));
255 return result;
258 void OnTimer() {
259 DCHECK(runner_.get());
260 if (CheckJs())
261 runner_->Quit();
264 content::WebContents* web_contents_;
265 const std::string js_;
266 scoped_refptr<content::MessageLoopRunner> runner_;
268 DISALLOW_COPY_AND_ASSIGN(JsConditionWaiter);
271 class KioskFakeDiskMountManager : public file_manager::FakeDiskMountManager {
272 public:
273 KioskFakeDiskMountManager() {}
275 ~KioskFakeDiskMountManager() override {}
277 void set_usb_mount_path(const std::string& usb_mount_path) {
278 usb_mount_path_ = usb_mount_path;
281 void MountUsbStick() {
282 DCHECK(!usb_mount_path_.empty());
283 MountPath(usb_mount_path_, "", "", chromeos::MOUNT_TYPE_DEVICE);
286 void UnMountUsbStick() {
287 DCHECK(!usb_mount_path_.empty());
288 UnmountPath(usb_mount_path_,
289 UNMOUNT_OPTIONS_NONE,
290 disks::DiskMountManager::UnmountPathCallback());
293 private:
294 std::string usb_mount_path_;
296 DISALLOW_COPY_AND_ASSIGN(KioskFakeDiskMountManager);
299 class AppDataLoadWaiter : public KioskAppManagerObserver {
300 public:
301 AppDataLoadWaiter(KioskAppManager* manager,
302 const std::string& app_id,
303 const std::string& version)
304 : runner_(NULL),
305 manager_(manager),
306 wait_type_(WAIT_FOR_CRX_CACHE),
307 loaded_(false),
308 quit_(false),
309 app_id_(app_id),
310 version_(version) {
311 manager_->AddObserver(this);
314 ~AppDataLoadWaiter() override { manager_->RemoveObserver(this); }
316 void Wait() {
317 wait_type_ = WAIT_FOR_CRX_CACHE;
318 if (quit_)
319 return;
320 runner_ = new content::MessageLoopRunner;
321 runner_->Run();
324 void WaitForAppData() {
325 wait_type_ = WAIT_FOR_APP_DATA;
326 if (quit_ || IsAppDataLoaded())
327 return;
328 runner_ = new content::MessageLoopRunner;
329 runner_->Run();
332 bool loaded() const { return loaded_; }
334 private:
335 enum WaitType {
336 WAIT_FOR_CRX_CACHE,
337 WAIT_FOR_APP_DATA,
340 // KioskAppManagerObserver overrides:
341 void OnKioskAppDataChanged(const std::string& app_id) override {
342 if (wait_type_ != WAIT_FOR_APP_DATA ||
343 app_id != app_id_ ||
344 !IsAppDataLoaded()) {
345 return;
348 loaded_ = true;
349 quit_ = true;
350 if (runner_.get())
351 runner_->Quit();
354 void OnKioskAppDataLoadFailure(const std::string& app_id) override {
355 if (wait_type_ != WAIT_FOR_APP_DATA || app_id != app_id_)
356 return;
358 loaded_ = false;
359 quit_ = true;
360 if (runner_.get())
361 runner_->Quit();
364 void OnKioskExtensionLoadedInCache(const std::string& app_id) override {
365 if (wait_type_ != WAIT_FOR_CRX_CACHE)
366 return;
368 std::string cached_version;
369 base::FilePath file_path;
370 if (!manager_->GetCachedCrx(app_id_, &file_path, &cached_version))
371 return;
372 if (version_ != cached_version)
373 return;
374 loaded_ = true;
375 quit_ = true;
376 if (runner_.get())
377 runner_->Quit();
380 void OnKioskExtensionDownloadFailed(const std::string& app_id) override {
381 if (wait_type_ != WAIT_FOR_CRX_CACHE)
382 return;
384 loaded_ = false;
385 quit_ = true;
386 if (runner_.get())
387 runner_->Quit();
390 bool IsAppDataLoaded() {
391 KioskAppManager::App app;
392 return manager_->GetApp(app_id_, &app) && !app.is_loading;
395 scoped_refptr<content::MessageLoopRunner> runner_;
396 KioskAppManager* manager_;
397 WaitType wait_type_;
398 bool loaded_;
399 bool quit_;
400 std::string app_id_;
401 std::string version_;
403 DISALLOW_COPY_AND_ASSIGN(AppDataLoadWaiter);
406 class CrosSettingsPermanentlyUntrustedMaker :
407 public DeviceSettingsService::Observer {
408 public:
409 CrosSettingsPermanentlyUntrustedMaker();
411 // DeviceSettingsService::Observer:
412 void OwnershipStatusChanged() override;
413 void DeviceSettingsUpdated() override;
414 void OnDeviceSettingsServiceShutdown() override;
416 private:
417 bool untrusted_check_running_;
418 base::RunLoop run_loop_;
420 void CheckIfUntrusted();
422 DISALLOW_COPY_AND_ASSIGN(CrosSettingsPermanentlyUntrustedMaker);
425 CrosSettingsPermanentlyUntrustedMaker::CrosSettingsPermanentlyUntrustedMaker()
426 : untrusted_check_running_(false) {
427 DeviceSettingsService::Get()->AddObserver(this);
429 policy::DevicePolicyCrosTestHelper().InstallOwnerKey();
430 DeviceSettingsService::Get()->OwnerKeySet(true);
432 run_loop_.Run();
435 void CrosSettingsPermanentlyUntrustedMaker::OwnershipStatusChanged() {
436 if (untrusted_check_running_)
437 return;
439 base::ThreadTaskRunnerHandle::Get()->PostTask(
440 FROM_HERE,
441 base::Bind(&CrosSettingsPermanentlyUntrustedMaker::CheckIfUntrusted,
442 base::Unretained(this)));
445 void CrosSettingsPermanentlyUntrustedMaker::DeviceSettingsUpdated() {
448 void CrosSettingsPermanentlyUntrustedMaker::OnDeviceSettingsServiceShutdown() {
451 void CrosSettingsPermanentlyUntrustedMaker::CheckIfUntrusted() {
452 untrusted_check_running_ = true;
453 const CrosSettingsProvider::TrustedStatus trusted_status =
454 CrosSettings::Get()->PrepareTrustedValues(
455 base::Bind(&CrosSettingsPermanentlyUntrustedMaker::CheckIfUntrusted,
456 base::Unretained(this)));
457 if (trusted_status == CrosSettingsProvider::TEMPORARILY_UNTRUSTED)
458 return;
459 untrusted_check_running_ = false;
461 if (trusted_status == CrosSettingsProvider::TRUSTED)
462 return;
464 DeviceSettingsService::Get()->RemoveObserver(this);
465 run_loop_.Quit();
468 } // namespace
470 class KioskTest : public OobeBaseTest {
471 public:
472 KioskTest() : use_consumer_kiosk_mode_(true),
473 fake_cws_(new FakeCWS) {
474 set_exit_when_last_browser_closes(false);
477 ~KioskTest() override {}
479 protected:
480 void SetUp() override {
481 test_app_id_ = kTestKioskApp;
482 set_test_app_version("1.0.0");
483 set_test_crx_file(test_app_id() + ".crx");
484 needs_background_networking_ = true;
485 mock_user_manager_.reset(new MockUserManager);
486 ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(true);
487 AppLaunchController::SkipSplashWaitForTesting();
488 AppLaunchController::SetNetworkWaitForTesting(kTestNetworkTimeoutSeconds);
490 OobeBaseTest::SetUp();
493 void TearDown() override {
494 ProfileHelper::SetAlwaysReturnPrimaryUserForTesting(false);
495 OobeBaseTest::TearDown();
498 void SetUpOnMainThread() override {
499 OobeBaseTest::SetUpOnMainThread();
500 // Needed to avoid showing Gaia screen instead of owner signin for
501 // consumer network down test cases.
502 StartupUtils::MarkDeviceRegistered(base::Closure());
505 void TearDownOnMainThread() override {
506 AppLaunchController::SetNetworkTimeoutCallbackForTesting(NULL);
507 AppLaunchSigninScreen::SetUserManagerForTesting(NULL);
509 OobeBaseTest::TearDownOnMainThread();
511 // Clean up while main thread still runs.
512 // See http://crbug.com/176659.
513 KioskAppManager::Get()->CleanUp();
516 void SetUpCommandLine(base::CommandLine* command_line) override {
517 OobeBaseTest::SetUpCommandLine(command_line);
518 fake_cws_->Init(embedded_test_server());
521 void LaunchApp(const std::string& app_id, bool diagnostic_mode) {
522 bool new_kiosk_ui = KioskAppMenuHandler::EnableNewKioskUI();
523 GetLoginUI()->CallJavascriptFunction(new_kiosk_ui ?
524 kLaunchAppForTestNewAPI : kLaunchAppForTestOldAPI,
525 base::StringValue(app_id),
526 base::FundamentalValue(diagnostic_mode));
529 void ReloadKioskApps() {
530 SetupTestAppUpdateCheck();
532 // Remove then add to ensure NOTIFICATION_KIOSK_APPS_LOADED fires.
533 KioskAppManager::Get()->RemoveApp(test_app_id_);
534 KioskAppManager::Get()->AddApp(test_app_id_);
537 void FireKioskAppSettingsChanged() {
538 KioskAppManager::Get()->UpdateAppData();
541 void SetupTestAppUpdateCheck() {
542 if (!test_app_version().empty()) {
543 fake_cws_->SetUpdateCrx(
544 test_app_id(), test_crx_file(), test_app_version());
548 void ReloadAutolaunchKioskApps() {
549 SetupTestAppUpdateCheck();
551 KioskAppManager::Get()->AddApp(test_app_id_);
552 KioskAppManager::Get()->SetAutoLaunchApp(test_app_id_);
555 void StartUIForAppLaunch() {
556 if (use_consumer_kiosk_mode_)
557 EnableConsumerKioskMode();
559 // Start UI
560 chromeos::WizardController::SkipPostLoginScreensForTesting();
561 chromeos::WizardController* wizard_controller =
562 chromeos::WizardController::default_controller();
563 if (wizard_controller) {
564 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
565 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
566 } else {
567 // No wizard and running with an existing profile and it should land
568 // on account picker when new kiosk UI is enabled. Otherwise, just
569 // wait for the login signal from Gaia.
570 if (KioskAppMenuHandler::EnableNewKioskUI())
571 OobeScreenWaiter(OobeDisplay::SCREEN_ACCOUNT_PICKER).Wait();
572 else
573 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
577 void PrepareAppLaunch() {
578 // Start UI
579 StartUIForAppLaunch();
581 // Wait for the Kiosk App configuration to reload.
582 content::WindowedNotificationObserver apps_loaded_signal(
583 chrome::NOTIFICATION_KIOSK_APPS_LOADED,
584 content::NotificationService::AllSources());
585 ReloadKioskApps();
586 apps_loaded_signal.Wait();
589 void StartAppLaunchFromLoginScreen(const base::Closure& network_setup_cb) {
590 PrepareAppLaunch();
592 if (!network_setup_cb.is_null())
593 network_setup_cb.Run();
595 LaunchApp(test_app_id(), false);
598 const extensions::Extension* GetInstalledApp() {
599 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
600 return extensions::ExtensionSystem::Get(app_profile)->
601 extension_service()->GetInstalledExtension(test_app_id_);
604 const Version& GetInstalledAppVersion() {
605 return *GetInstalledApp()->version();
608 void WaitForAppLaunchWithOptions(bool check_launch_data, bool terminate_app) {
609 ExtensionTestMessageListener
610 launch_data_check_listener("launchData.isKioskSession = true", false);
612 // Wait for the Kiosk App to launch.
613 content::WindowedNotificationObserver(
614 chrome::NOTIFICATION_KIOSK_APP_LAUNCHED,
615 content::NotificationService::AllSources()).Wait();
617 // Default profile switches to app profile after app is launched.
618 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
619 ASSERT_TRUE(app_profile);
621 // Check ChromeOS preference is initialized.
622 EXPECT_TRUE(
623 static_cast<ProfileImpl*>(app_profile)->chromeos_preferences_);
625 // Check installer status.
626 EXPECT_EQ(chromeos::KioskAppLaunchError::NONE,
627 chromeos::KioskAppLaunchError::Get());
629 // Check if the kiosk webapp is really installed for the default profile.
630 const extensions::Extension* app =
631 extensions::ExtensionSystem::Get(app_profile)->
632 extension_service()->GetInstalledExtension(test_app_id_);
633 EXPECT_TRUE(app);
635 // App should appear with its window.
636 extensions::AppWindowRegistry* app_window_registry =
637 extensions::AppWindowRegistry::Get(app_profile);
638 extensions::AppWindow* window =
639 AppWindowWaiter(app_window_registry, test_app_id_).Wait();
640 EXPECT_TRUE(window);
642 // Login screen should be gone or fading out.
643 chromeos::LoginDisplayHost* login_display_host =
644 chromeos::LoginDisplayHostImpl::default_host();
645 EXPECT_TRUE(
646 login_display_host == NULL ||
647 login_display_host->GetNativeWindow()->layer()->GetTargetOpacity() ==
648 0.0f);
650 // Terminate the app.
651 if (terminate_app)
652 window->GetBaseWindow()->Close();
654 // Wait until the app terminates if it is still running.
655 if (!app_window_registry->GetAppWindowsForApp(test_app_id_).empty())
656 content::RunMessageLoop();
658 // Check that the app had been informed that it is running in a kiosk
659 // session.
660 if (check_launch_data)
661 EXPECT_TRUE(launch_data_check_listener.was_satisfied());
664 void WaitForAppLaunchSuccess() {
665 WaitForAppLaunchWithOptions(true /* check_launch_data */,
666 true /* terminate_app */);
669 void WaitForAppLaunchNetworkTimeout() {
670 if (GetAppLaunchController()->network_wait_timedout())
671 return;
673 scoped_refptr<content::MessageLoopRunner> runner =
674 new content::MessageLoopRunner;
676 base::Closure callback = base::Bind(
677 &OnNetworkWaitTimedOut, runner->QuitClosure());
678 AppLaunchController::SetNetworkTimeoutCallbackForTesting(&callback);
680 runner->Run();
682 CHECK(GetAppLaunchController()->network_wait_timedout());
683 AppLaunchController::SetNetworkTimeoutCallbackForTesting(NULL);
686 void EnableConsumerKioskMode() {
687 scoped_ptr<bool> locked(new bool(false));
688 scoped_refptr<content::MessageLoopRunner> runner =
689 new content::MessageLoopRunner;
690 KioskAppManager::Get()->EnableConsumerKioskAutoLaunch(
691 base::Bind(&ConsumerKioskModeAutoStartLockCheck,
692 locked.get(),
693 runner->QuitClosure()));
694 runner->Run();
695 EXPECT_TRUE(*locked.get());
698 KioskAppManager::ConsumerKioskAutoLaunchStatus
699 GetConsumerKioskModeStatus() {
700 KioskAppManager::ConsumerKioskAutoLaunchStatus status =
701 static_cast<KioskAppManager::ConsumerKioskAutoLaunchStatus>(-1);
702 scoped_refptr<content::MessageLoopRunner> runner =
703 new content::MessageLoopRunner;
704 KioskAppManager::Get()->GetConsumerKioskAutoLaunchStatus(
705 base::Bind(&ConsumerKioskAutoLaunchStatusCheck,
706 &status,
707 runner->QuitClosure()));
708 runner->Run();
709 CHECK_NE(status,
710 static_cast<KioskAppManager::ConsumerKioskAutoLaunchStatus>(-1));
711 return status;
714 void RunAppLaunchNetworkDownTest() {
715 mock_user_manager()->SetActiveUser(kTestOwnerEmail);
716 AppLaunchSigninScreen::SetUserManagerForTesting(mock_user_manager());
718 // Mock network could be configured with owner's password.
719 ScopedCanConfigureNetwork can_configure_network(true, true);
721 // Start app launch and wait for network connectivity timeout.
722 StartAppLaunchFromLoginScreen(SimulateNetworkOfflineClosure());
723 OobeScreenWaiter splash_waiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH);
724 splash_waiter.Wait();
725 WaitForAppLaunchNetworkTimeout();
727 // Configure network link should be visible.
728 JsExpect("$('splash-config-network').hidden == false");
730 // Set up fake user manager with an owner for the test.
731 static_cast<LoginDisplayHostImpl*>(LoginDisplayHostImpl::default_host())
732 ->GetOobeUI()->ShowOobeUI(false);
734 // Configure network should bring up lock screen for owner.
735 OobeScreenWaiter lock_screen_waiter(OobeDisplay::SCREEN_ACCOUNT_PICKER);
736 static_cast<AppLaunchSplashScreenActor::Delegate*>(GetAppLaunchController())
737 ->OnConfigureNetwork();
738 lock_screen_waiter.Wait();
740 // There should be only one owner pod on this screen.
741 JsExpect("$('pod-row').alwaysFocusSinglePod");
743 // A network error screen should be shown after authenticating.
744 OobeScreenWaiter error_screen_waiter(OobeDisplay::SCREEN_ERROR_MESSAGE);
745 static_cast<AppLaunchSigninScreen::Delegate*>(GetAppLaunchController())
746 ->OnOwnerSigninSuccess();
747 error_screen_waiter.Wait();
749 ASSERT_TRUE(GetAppLaunchController()->showing_network_dialog());
751 SimulateNetworkOnline();
752 WaitForAppLaunchSuccess();
755 AppLaunchController* GetAppLaunchController() {
756 return chromeos::LoginDisplayHostImpl::default_host()
757 ->GetAppLaunchController();
760 // Returns a lock that is holding a task on the FILE thread. Any tasks posted
761 // to the FILE thread after this call will be blocked until the returned
762 // lock is released.
763 // This can be used to prevent app installation from completing until some
764 // other conditions are checked and triggered. For example, this can be used
765 // to trigger the network screen during app launch without racing with the
766 // app launching process itself.
767 scoped_ptr<base::AutoLock> LockFileThread() {
768 scoped_ptr<base::Lock> lock(new base::Lock);
769 scoped_ptr<base::AutoLock> auto_lock(new base::AutoLock(*lock));
770 content::BrowserThread::PostTask(
771 content::BrowserThread::FILE, FROM_HERE,
772 base::Bind(&LockAndUnlock, base::Passed(&lock)));
773 return auto_lock.Pass();
776 MockUserManager* mock_user_manager() { return mock_user_manager_.get(); }
778 void set_test_app_id(const std::string& test_app_id) {
779 test_app_id_ = test_app_id;
781 const std::string& test_app_id() const { return test_app_id_; }
782 void set_test_app_version(const std::string& version) {
783 test_app_version_ = version;
785 const std::string& test_app_version() const { return test_app_version_; }
786 void set_test_crx_file(const std::string& filename) {
787 test_crx_file_ = filename;
789 const std::string& test_crx_file() const { return test_crx_file_; }
790 FakeCWS* fake_cws() { return fake_cws_.get(); }
792 void set_use_consumer_kiosk_mode(bool use) {
793 use_consumer_kiosk_mode_ = use;
796 private:
797 bool use_consumer_kiosk_mode_;
798 std::string test_app_id_;
799 std::string test_app_version_;
800 std::string test_crx_file_;
801 scoped_ptr<FakeCWS> fake_cws_;
802 scoped_ptr<MockUserManager> mock_user_manager_;
804 DISALLOW_COPY_AND_ASSIGN(KioskTest);
807 IN_PROC_BROWSER_TEST_F(KioskTest, InstallAndLaunchApp) {
808 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
809 WaitForAppLaunchSuccess();
810 KioskAppManager::App app;
811 ASSERT_TRUE(KioskAppManager::Get()->GetApp(test_app_id(), &app));
812 EXPECT_FALSE(app.was_auto_launched_with_zero_delay);
815 IN_PROC_BROWSER_TEST_F(KioskTest, ZoomSupport) {
816 ExtensionTestMessageListener
817 app_window_loaded_listener("appWindowLoaded", false);
818 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
819 app_window_loaded_listener.WaitUntilSatisfied();
821 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
822 ASSERT_TRUE(app_profile);
824 extensions::AppWindowRegistry* app_window_registry =
825 extensions::AppWindowRegistry::Get(app_profile);
826 extensions::AppWindow* window =
827 AppWindowWaiter(app_window_registry, test_app_id()).Wait();
828 ASSERT_TRUE(window);
830 // Gets the original width of the app window.
831 int original_width;
832 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
833 window->web_contents(),
834 "window.domAutomationController.setAutomationId(0);"
835 "window.domAutomationController.send(window.innerWidth);",
836 &original_width));
838 native_app_window::NativeAppWindowViews* native_app_window_views =
839 static_cast<native_app_window::NativeAppWindowViews*>(
840 window->GetBaseWindow());
841 ui::AcceleratorTarget* accelerator_target =
842 static_cast<ui::AcceleratorTarget*>(native_app_window_views);
844 // Zoom in. Text is bigger and content window width becomes smaller.
845 accelerator_target->AcceleratorPressed(ui::Accelerator(
846 ui::VKEY_ADD, ui::EF_CONTROL_DOWN));
847 int width_zoomed_in;
848 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
849 window->web_contents(),
850 "window.domAutomationController.setAutomationId(0);"
851 "window.domAutomationController.send(window.innerWidth);",
852 &width_zoomed_in));
853 DCHECK_LT(width_zoomed_in, original_width);
855 // Go back to normal. Window width is restored.
856 accelerator_target->AcceleratorPressed(ui::Accelerator(
857 ui::VKEY_0, ui::EF_CONTROL_DOWN));
858 int width_zoom_normal;
859 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
860 window->web_contents(),
861 "window.domAutomationController.setAutomationId(0);"
862 "window.domAutomationController.send(window.innerWidth);",
863 &width_zoom_normal));
864 DCHECK_EQ(width_zoom_normal, original_width);
866 // Zoom out. Text is smaller and content window width becomes larger.
867 accelerator_target->AcceleratorPressed(ui::Accelerator(
868 ui::VKEY_SUBTRACT, ui::EF_CONTROL_DOWN));
869 int width_zoomed_out;
870 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
871 window->web_contents(),
872 "window.domAutomationController.setAutomationId(0);"
873 "window.domAutomationController.send(window.innerWidth);",
874 &width_zoomed_out));
875 DCHECK_GT(width_zoomed_out, original_width);
877 // Terminate the app.
878 window->GetBaseWindow()->Close();
879 content::RunAllPendingInMessageLoop();
882 IN_PROC_BROWSER_TEST_F(KioskTest, NotSignedInWithGAIAAccount) {
883 // Tests that the kiosk session is not considered to be logged in with a GAIA
884 // account.
885 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
886 WaitForAppLaunchSuccess();
888 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
889 ASSERT_TRUE(app_profile);
890 EXPECT_FALSE(
891 SigninManagerFactory::GetForProfile(app_profile)->IsAuthenticated());
894 IN_PROC_BROWSER_TEST_F(KioskTest, PRE_LaunchAppNetworkDown) {
895 // Tests the network down case for the initial app download and launch.
896 RunAppLaunchNetworkDownTest();
899 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppNetworkDown) {
900 // Tests the network down case for launching an existing app that is
901 // installed in PRE_LaunchAppNetworkDown.
902 RunAppLaunchNetworkDownTest();
905 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppWithNetworkConfigAccelerator) {
906 ScopedCanConfigureNetwork can_configure_network(true, false);
908 // Block app loading until the network screen is shown.
909 scoped_ptr<base::AutoLock> lock = LockFileThread();
911 // Start app launch and wait for network connectivity timeout.
912 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
913 OobeScreenWaiter splash_waiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH);
914 splash_waiter.Wait();
916 // A network error screen should be shown after authenticating.
917 OobeScreenWaiter error_screen_waiter(OobeDisplay::SCREEN_ERROR_MESSAGE);
918 // Simulate Ctrl+Alt+N accelerator.
919 GetLoginUI()->CallJavascriptFunction(
920 "cr.ui.Oobe.handleAccelerator",
921 base::StringValue("app_launch_network_config"));
922 error_screen_waiter.Wait();
923 ASSERT_TRUE(GetAppLaunchController()->showing_network_dialog());
925 // Continue button should be visible since we are online.
926 JsExpect("$('continue-network-config-btn').hidden == false");
928 // Click on [Continue] button.
929 ASSERT_TRUE(content::ExecuteScript(
930 GetLoginUI()->GetWebContents(),
931 "(function() {"
932 "var e = new Event('click');"
933 "$('continue-network-config-btn').dispatchEvent(e);"
934 "})();"));
936 // Let app launching resume.
937 lock.reset();
939 WaitForAppLaunchSuccess();
942 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppNetworkDownConfigureNotAllowed) {
943 // Mock network could not be configured.
944 ScopedCanConfigureNetwork can_configure_network(false, true);
946 // Start app launch and wait for network connectivity timeout.
947 StartAppLaunchFromLoginScreen(SimulateNetworkOfflineClosure());
948 OobeScreenWaiter splash_waiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH);
949 splash_waiter.Wait();
950 WaitForAppLaunchNetworkTimeout();
952 // Configure network link should not be visible.
953 JsExpect("$('splash-config-network').hidden == true");
955 // Network becomes online and app launch is resumed.
956 SimulateNetworkOnline();
957 WaitForAppLaunchSuccess();
960 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppNetworkPortal) {
961 // Mock network could be configured without the owner password.
962 ScopedCanConfigureNetwork can_configure_network(true, false);
964 // Start app launch with network portal state.
965 StartAppLaunchFromLoginScreen(SimulateNetworkPortalClosure());
966 OobeScreenWaiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH)
967 .WaitNoAssertCurrentScreen();
968 WaitForAppLaunchNetworkTimeout();
970 // Network error should show up automatically since this test does not
971 // require owner auth to configure network.
972 OobeScreenWaiter(OobeDisplay::SCREEN_ERROR_MESSAGE).Wait();
974 ASSERT_TRUE(GetAppLaunchController()->showing_network_dialog());
975 SimulateNetworkOnline();
976 WaitForAppLaunchSuccess();
979 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchAppUserCancel) {
980 // Make fake_cws_ return empty update response.
981 set_test_app_version("");
982 StartAppLaunchFromLoginScreen(SimulateNetworkOfflineClosure());
983 OobeScreenWaiter splash_waiter(OobeDisplay::SCREEN_APP_LAUNCH_SPLASH);
984 splash_waiter.Wait();
986 CrosSettings::Get()->SetBoolean(
987 kAccountsPrefDeviceLocalAccountAutoLoginBailoutEnabled, true);
988 content::WindowedNotificationObserver signal(
989 chrome::NOTIFICATION_APP_TERMINATING,
990 content::NotificationService::AllSources());
991 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
992 base::StringValue("app_launch_bailout"));
993 signal.Wait();
994 EXPECT_EQ(chromeos::KioskAppLaunchError::USER_CANCEL,
995 chromeos::KioskAppLaunchError::Get());
998 IN_PROC_BROWSER_TEST_F(KioskTest, LaunchInDiagnosticMode) {
999 PrepareAppLaunch();
1000 SimulateNetworkOnline();
1002 LaunchApp(kTestKioskApp, true);
1004 content::WebContents* login_contents = GetLoginUI()->GetWebContents();
1006 bool new_kiosk_ui = KioskAppMenuHandler::EnableNewKioskUI();
1007 JsConditionWaiter(login_contents, new_kiosk_ui ?
1008 kCheckDiagnosticModeNewAPI : kCheckDiagnosticModeOldAPI).Wait();
1010 std::string diagnosticMode(new_kiosk_ui ?
1011 kCheckDiagnosticModeNewAPI : kCheckDiagnosticModeOldAPI);
1012 ASSERT_TRUE(content::ExecuteScript(
1013 login_contents,
1014 "(function() {"
1015 "var e = new Event('click');" +
1016 diagnosticMode + "."
1017 "okButton_.dispatchEvent(e);"
1018 "})();"));
1020 WaitForAppLaunchSuccess();
1023 IN_PROC_BROWSER_TEST_F(KioskTest, AutolaunchWarningCancel) {
1024 EnableConsumerKioskMode();
1026 chromeos::WizardController::SkipPostLoginScreensForTesting();
1027 chromeos::WizardController* wizard_controller =
1028 chromeos::WizardController::default_controller();
1029 CHECK(wizard_controller);
1031 // Start login screen after configuring auto launch app since the warning
1032 // is triggered when switching to login screen.
1033 wizard_controller->AdvanceToScreen(WizardController::kNetworkScreenName);
1034 ReloadAutolaunchKioskApps();
1035 EXPECT_FALSE(KioskAppManager::Get()->GetAutoLaunchApp().empty());
1036 EXPECT_FALSE(KioskAppManager::Get()->IsAutoLaunchEnabled());
1037 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1039 // Wait for the auto launch warning come up.
1040 content::WindowedNotificationObserver(
1041 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
1042 content::NotificationService::AllSources()).Wait();
1043 GetLoginUI()->CallJavascriptFunction(
1044 "login.AutolaunchScreen.confirmAutoLaunchForTesting",
1045 base::FundamentalValue(false));
1047 // Wait for the auto launch warning to go away.
1048 content::WindowedNotificationObserver(
1049 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_COMPLETED,
1050 content::NotificationService::AllSources()).Wait();
1052 EXPECT_FALSE(KioskAppManager::Get()->IsAutoLaunchEnabled());
1055 IN_PROC_BROWSER_TEST_F(KioskTest, AutolaunchWarningConfirm) {
1056 EnableConsumerKioskMode();
1058 chromeos::WizardController::SkipPostLoginScreensForTesting();
1059 chromeos::WizardController* wizard_controller =
1060 chromeos::WizardController::default_controller();
1061 CHECK(wizard_controller);
1063 // Start login screen after configuring auto launch app since the warning
1064 // is triggered when switching to login screen.
1065 wizard_controller->AdvanceToScreen(WizardController::kNetworkScreenName);
1066 ReloadAutolaunchKioskApps();
1067 EXPECT_FALSE(KioskAppManager::Get()->GetAutoLaunchApp().empty());
1068 EXPECT_FALSE(KioskAppManager::Get()->IsAutoLaunchEnabled());
1069 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1071 // Wait for the auto launch warning come up.
1072 content::WindowedNotificationObserver(
1073 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
1074 content::NotificationService::AllSources()).Wait();
1075 GetLoginUI()->CallJavascriptFunction(
1076 "login.AutolaunchScreen.confirmAutoLaunchForTesting",
1077 base::FundamentalValue(true));
1079 // Wait for the auto launch warning to go away.
1080 content::WindowedNotificationObserver(
1081 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_COMPLETED,
1082 content::NotificationService::AllSources()).Wait();
1084 EXPECT_FALSE(KioskAppManager::Get()->GetAutoLaunchApp().empty());
1085 EXPECT_TRUE(KioskAppManager::Get()->IsAutoLaunchEnabled());
1087 WaitForAppLaunchSuccess();
1089 KioskAppManager::App app;
1090 ASSERT_TRUE(KioskAppManager::Get()->GetApp(test_app_id(), &app));
1091 EXPECT_TRUE(app.was_auto_launched_with_zero_delay);
1094 IN_PROC_BROWSER_TEST_F(KioskTest, KioskEnableCancel) {
1095 chromeos::WizardController::SkipPostLoginScreensForTesting();
1096 chromeos::WizardController* wizard_controller =
1097 chromeos::WizardController::default_controller();
1098 CHECK(wizard_controller);
1100 // Check Kiosk mode status.
1101 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
1102 GetConsumerKioskModeStatus());
1104 // Wait for the login UI to come up and switch to the kiosk_enable screen.
1105 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1106 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
1107 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
1108 base::StringValue("kiosk_enable"));
1110 // Wait for the kiosk_enable screen to show and cancel the screen.
1111 content::WindowedNotificationObserver(
1112 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_VISIBLE,
1113 content::NotificationService::AllSources()).Wait();
1114 GetLoginUI()->CallJavascriptFunction(
1115 "login.KioskEnableScreen.enableKioskForTesting",
1116 base::FundamentalValue(false));
1118 // Wait for the kiosk_enable screen to disappear.
1119 content::WindowedNotificationObserver(
1120 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_COMPLETED,
1121 content::NotificationService::AllSources()).Wait();
1123 // Check that the status still says configurable.
1124 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
1125 GetConsumerKioskModeStatus());
1128 IN_PROC_BROWSER_TEST_F(KioskTest, KioskEnableConfirmed) {
1129 // Start UI, find menu entry for this app and launch it.
1130 chromeos::WizardController::SkipPostLoginScreensForTesting();
1131 chromeos::WizardController* wizard_controller =
1132 chromeos::WizardController::default_controller();
1133 CHECK(wizard_controller);
1135 // Check Kiosk mode status.
1136 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
1137 GetConsumerKioskModeStatus());
1139 // Wait for the login UI to come up and switch to the kiosk_enable screen.
1140 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1141 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
1142 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
1143 base::StringValue("kiosk_enable"));
1145 // Wait for the kiosk_enable screen to show and cancel the screen.
1146 content::WindowedNotificationObserver(
1147 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_VISIBLE,
1148 content::NotificationService::AllSources()).Wait();
1149 GetLoginUI()->CallJavascriptFunction(
1150 "login.KioskEnableScreen.enableKioskForTesting",
1151 base::FundamentalValue(true));
1153 // Wait for the signal that indicates Kiosk Mode is enabled.
1154 content::WindowedNotificationObserver(
1155 chrome::NOTIFICATION_KIOSK_ENABLED,
1156 content::NotificationService::AllSources()).Wait();
1157 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_ENABLED,
1158 GetConsumerKioskModeStatus());
1161 IN_PROC_BROWSER_TEST_F(KioskTest, KioskEnableAfter2ndSigninScreen) {
1162 chromeos::WizardController::SkipPostLoginScreensForTesting();
1163 chromeos::WizardController* wizard_controller =
1164 chromeos::WizardController::default_controller();
1165 CHECK(wizard_controller);
1167 // Check Kiosk mode status.
1168 EXPECT_EQ(KioskAppManager::CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
1169 GetConsumerKioskModeStatus());
1171 // Wait for the login UI to come up and switch to the kiosk_enable screen.
1172 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1173 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
1174 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
1175 base::StringValue("kiosk_enable"));
1177 // Wait for the kiosk_enable screen to show and cancel the screen.
1178 content::WindowedNotificationObserver(
1179 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_VISIBLE,
1180 content::NotificationService::AllSources()).Wait();
1181 GetLoginUI()->CallJavascriptFunction(
1182 "login.KioskEnableScreen.enableKioskForTesting",
1183 base::FundamentalValue(false));
1185 // Wait for the kiosk_enable screen to disappear.
1186 content::WindowedNotificationObserver(
1187 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_COMPLETED,
1188 content::NotificationService::AllSources()).Wait();
1190 // Show signin screen again.
1191 chromeos::LoginDisplayHostImpl::default_host()->StartSignInScreen(
1192 LoginScreenContext());
1193 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
1195 // Show kiosk enable screen again.
1196 GetLoginUI()->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
1197 base::StringValue("kiosk_enable"));
1199 // And it should show up.
1200 content::WindowedNotificationObserver(
1201 chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_VISIBLE,
1202 content::NotificationService::AllSources()).Wait();
1205 IN_PROC_BROWSER_TEST_F(KioskTest, DoNotLaunchWhenUntrusted) {
1206 PrepareAppLaunch();
1207 SimulateNetworkOnline();
1209 // Make cros settings untrusted.
1210 CrosSettingsPermanentlyUntrustedMaker();
1212 // Check that the attempt to start a kiosk app fails with an error.
1213 LaunchApp(test_app_id(), false);
1214 bool ignored = false;
1215 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
1216 GetLoginUI()->GetWebContents(),
1217 "if (cr.ui.Oobe.getInstance().errorMessageWasShownForTesting_) {"
1218 " window.domAutomationController.send(true);"
1219 "} else {"
1220 " cr.ui.Oobe.showSignInError = function("
1221 " loginAttempts, message, link, helpId) {"
1222 " window.domAutomationController.send(true);"
1223 " };"
1224 "}",
1225 &ignored));
1228 // Verifies that a consumer device does not auto-launch kiosk mode when cros
1229 // settings are untrusted.
1230 IN_PROC_BROWSER_TEST_F(KioskTest, NoConsumerAutoLaunchWhenUntrusted) {
1231 EnableConsumerKioskMode();
1233 // Wait for and confirm the auto-launch warning.
1234 chromeos::WizardController::SkipPostLoginScreensForTesting();
1235 chromeos::WizardController* wizard_controller =
1236 chromeos::WizardController::default_controller();
1237 ASSERT_TRUE(wizard_controller);
1238 wizard_controller->AdvanceToScreen(WizardController::kNetworkScreenName);
1239 ReloadAutolaunchKioskApps();
1240 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1241 content::WindowedNotificationObserver(
1242 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
1243 content::NotificationService::AllSources()).Wait();
1244 GetLoginUI()->CallJavascriptFunction(
1245 "login.AutolaunchScreen.confirmAutoLaunchForTesting",
1246 base::FundamentalValue(true));
1248 // Make cros settings untrusted.
1249 CrosSettingsPermanentlyUntrustedMaker();
1251 // Check that the attempt to auto-launch a kiosk app fails with an error.
1252 OobeScreenWaiter(OobeDisplay::SCREEN_ERROR_MESSAGE).Wait();
1255 // Verifies that an enterprise device does not auto-launch kiosk mode when cros
1256 // settings are untrusted.
1257 IN_PROC_BROWSER_TEST_F(KioskTest, NoEnterpriseAutoLaunchWhenUntrusted) {
1258 PrepareAppLaunch();
1259 SimulateNetworkOnline();
1261 // Make cros settings untrusted.
1262 CrosSettingsPermanentlyUntrustedMaker();
1264 // Trigger the code that handles auto-launch on enterprise devices. This would
1265 // normally be called from ShowLoginWizard(), which runs so early that it is
1266 // not possible to inject an auto-launch policy before it runs.
1267 LoginDisplayHost* login_display_host = LoginDisplayHostImpl::default_host();
1268 ASSERT_TRUE(login_display_host);
1269 login_display_host->StartAppLaunch(test_app_id(), false, true);
1271 // Check that no launch has started.
1272 EXPECT_FALSE(login_display_host->GetAppLaunchController());
1275 class KioskUpdateTest : public KioskTest {
1276 public:
1277 KioskUpdateTest() {}
1278 ~KioskUpdateTest() override {}
1280 protected:
1281 void SetUp() override {
1282 fake_disk_mount_manager_ = new KioskFakeDiskMountManager();
1283 disks::DiskMountManager::InitializeForTesting(fake_disk_mount_manager_);
1285 KioskTest::SetUp();
1288 void TearDown() override {
1289 disks::DiskMountManager::Shutdown();
1291 KioskTest::TearDown();
1294 void SetUpOnMainThread() override { KioskTest::SetUpOnMainThread(); }
1296 void PreCacheApp(const std::string& app_id,
1297 const std::string& version,
1298 const std::string& crx_file) {
1299 set_test_app_id(app_id);
1300 set_test_app_version(version);
1301 set_test_crx_file(crx_file);
1303 KioskAppManager* manager = KioskAppManager::Get();
1304 AppDataLoadWaiter waiter(manager, app_id, version);
1305 ReloadKioskApps();
1306 waiter.Wait();
1307 EXPECT_TRUE(waiter.loaded());
1308 std::string cached_version;
1309 base::FilePath file_path;
1310 EXPECT_TRUE(manager->GetCachedCrx(app_id, &file_path, &cached_version));
1311 EXPECT_EQ(version, cached_version);
1314 void UpdateExternalCache(const std::string& version,
1315 const std::string& crx_file) {
1316 set_test_app_version(version);
1317 set_test_crx_file(crx_file);
1318 SetupTestAppUpdateCheck();
1320 KioskAppManager* manager = KioskAppManager::Get();
1321 AppDataLoadWaiter waiter(manager, test_app_id(), version);
1322 KioskAppManager::Get()->UpdateExternalCache();
1323 waiter.Wait();
1324 EXPECT_TRUE(waiter.loaded());
1325 std::string cached_version;
1326 base::FilePath file_path;
1327 EXPECT_TRUE(
1328 manager->GetCachedCrx(test_app_id(), &file_path, &cached_version));
1329 EXPECT_EQ(version, cached_version);
1332 void SetupFakeDiskMountManagerMountPath(const std::string mount_path) {
1333 base::FilePath test_data_dir;
1334 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
1335 test_data_dir = test_data_dir.AppendASCII(mount_path);
1336 fake_disk_mount_manager_->set_usb_mount_path(test_data_dir.value());
1339 void SimulateUpdateAppFromUsbStick(const std::string& usb_mount_path,
1340 bool* app_update_notified,
1341 bool* update_success) {
1342 SetupFakeDiskMountManagerMountPath(usb_mount_path);
1343 KioskAppExternalUpdateWaiter waiter(KioskAppManager::Get(), test_app_id());
1344 fake_disk_mount_manager_->MountUsbStick();
1345 waiter.Wait();
1346 fake_disk_mount_manager_->UnMountUsbStick();
1347 *update_success = waiter.update_success();
1348 *app_update_notified = waiter.app_update_notified();
1351 void PreCacheAndLaunchApp(const std::string& app_id,
1352 const std::string& version,
1353 const std::string& crx_file) {
1354 set_test_app_id(app_id);
1355 set_test_app_version(version);
1356 set_test_crx_file(crx_file);
1357 PrepareAppLaunch();
1358 SimulateNetworkOnline();
1359 LaunchApp(test_app_id(), false);
1360 WaitForAppLaunchSuccess();
1361 EXPECT_EQ(version, GetInstalledAppVersion().GetString());
1364 private:
1365 class KioskAppExternalUpdateWaiter : public KioskAppManagerObserver {
1366 public:
1367 KioskAppExternalUpdateWaiter(KioskAppManager* manager,
1368 const std::string& app_id)
1369 : runner_(NULL),
1370 manager_(manager),
1371 app_id_(app_id),
1372 quit_(false),
1373 update_success_(false),
1374 app_update_notified_(false) {
1375 manager_->AddObserver(this);
1378 ~KioskAppExternalUpdateWaiter() override { manager_->RemoveObserver(this); }
1380 void Wait() {
1381 if (quit_)
1382 return;
1383 runner_ = new content::MessageLoopRunner;
1384 runner_->Run();
1387 bool update_success() const { return update_success_; }
1389 bool app_update_notified() const { return app_update_notified_; }
1391 private:
1392 // KioskAppManagerObserver overrides:
1393 void OnKioskAppCacheUpdated(const std::string& app_id) override {
1394 if (app_id_ != app_id)
1395 return;
1396 app_update_notified_ = true;
1399 void OnKioskAppExternalUpdateComplete(bool success) override {
1400 quit_ = true;
1401 update_success_ = success;
1402 if (runner_.get())
1403 runner_->Quit();
1406 scoped_refptr<content::MessageLoopRunner> runner_;
1407 KioskAppManager* manager_;
1408 const std::string app_id_;
1409 bool quit_;
1410 bool update_success_;
1411 bool app_update_notified_;
1413 DISALLOW_COPY_AND_ASSIGN(KioskAppExternalUpdateWaiter);
1416 // Owned by DiskMountManager.
1417 KioskFakeDiskMountManager* fake_disk_mount_manager_;
1419 DISALLOW_COPY_AND_ASSIGN(KioskUpdateTest);
1422 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_LaunchOfflineEnabledAppNoNetwork) {
1423 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1424 "1.0.0",
1425 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1428 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, LaunchOfflineEnabledAppNoNetwork) {
1429 set_test_app_id(kTestOfflineEnabledKioskApp);
1430 StartUIForAppLaunch();
1431 SimulateNetworkOffline();
1432 LaunchApp(test_app_id(), false);
1433 WaitForAppLaunchSuccess();
1435 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1438 IN_PROC_BROWSER_TEST_F(KioskUpdateTest,
1439 PRE_LaunchCachedOfflineEnabledAppNoNetwork) {
1440 PreCacheApp(kTestOfflineEnabledKioskApp,
1441 "1.0.0",
1442 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1445 IN_PROC_BROWSER_TEST_F(KioskUpdateTest,
1446 LaunchCachedOfflineEnabledAppNoNetwork) {
1447 set_test_app_id(kTestOfflineEnabledKioskApp);
1448 EXPECT_TRUE(
1449 KioskAppManager::Get()->HasCachedCrx(kTestOfflineEnabledKioskApp));
1450 StartUIForAppLaunch();
1451 SimulateNetworkOffline();
1452 LaunchApp(test_app_id(), false);
1453 WaitForAppLaunchSuccess();
1455 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1458 // Network offline, app v1.0 has run before, has cached v2.0 crx and v2.0 should
1459 // be installed and launched during next launch.
1460 IN_PROC_BROWSER_TEST_F(KioskUpdateTest,
1461 PRE_LaunchCachedNewVersionOfflineEnabledAppNoNetwork) {
1462 // Install and launch v1 app.
1463 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1464 "1.0.0",
1465 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1466 // Update cache for v2 app.
1467 UpdateExternalCache("2.0.0",
1468 std::string(kTestOfflineEnabledKioskApp) + ".crx");
1469 // The installed app is still in v1.
1470 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1473 IN_PROC_BROWSER_TEST_F(KioskUpdateTest,
1474 LaunchCachedNewVersionOfflineEnabledAppNoNetwork) {
1475 set_test_app_id(kTestOfflineEnabledKioskApp);
1476 EXPECT_TRUE(KioskAppManager::Get()->HasCachedCrx(test_app_id()));
1478 StartUIForAppLaunch();
1479 SimulateNetworkOffline();
1480 LaunchApp(test_app_id(), false);
1481 WaitForAppLaunchSuccess();
1483 // v2 app should have been installed.
1484 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1487 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_LaunchOfflineEnabledAppNoUpdate) {
1488 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1489 "1.0.0",
1490 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1493 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, LaunchOfflineEnabledAppNoUpdate) {
1494 set_test_app_id(kTestOfflineEnabledKioskApp);
1495 fake_cws()->SetNoUpdate(test_app_id());
1497 StartUIForAppLaunch();
1498 SimulateNetworkOnline();
1499 LaunchApp(test_app_id(), false);
1500 WaitForAppLaunchSuccess();
1502 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1505 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_LaunchOfflineEnabledAppHasUpdate) {
1506 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1507 "1.0.0",
1508 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1511 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, LaunchOfflineEnabledAppHasUpdate) {
1512 set_test_app_id(kTestOfflineEnabledKioskApp);
1513 fake_cws()->SetUpdateCrx(
1514 test_app_id(), "ajoggoflpgplnnjkjamcmbepjdjdnpdp.crx", "2.0.0");
1516 StartUIForAppLaunch();
1517 SimulateNetworkOnline();
1518 LaunchApp(test_app_id(), false);
1519 WaitForAppLaunchSuccess();
1521 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1524 // Pre-cache v1 kiosk app, then launch the app without network,
1525 // plug in usb stick with a v2 app for offline updating.
1526 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_UsbStickUpdateAppNoNetwork) {
1527 PreCacheApp(kTestOfflineEnabledKioskApp,
1528 "1.0.0",
1529 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1531 set_test_app_id(kTestOfflineEnabledKioskApp);
1532 StartUIForAppLaunch();
1533 SimulateNetworkOffline();
1534 LaunchApp(test_app_id(), false);
1535 WaitForAppLaunchSuccess();
1536 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1538 // Simulate mounting of usb stick with v2 app on the stick.
1539 bool update_success;
1540 bool app_update_notified;
1541 SimulateUpdateAppFromUsbStick(
1542 kFakeUsbMountPathUpdatePass, &app_update_notified, &update_success);
1543 EXPECT_TRUE(update_success);
1544 EXPECT_TRUE(app_update_notified);
1546 // The v2 kiosk app is loaded into external cache, but won't be installed
1547 // until next time the device is started.
1548 base::FilePath crx_path;
1549 std::string cached_version;
1550 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1551 test_app_id(), &crx_path, &cached_version));
1552 EXPECT_EQ("2.0.0", cached_version);
1553 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1556 // Restart the device, verify the app has been updated to v2.
1557 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppNoNetwork) {
1558 // Verify the kiosk app has been updated to v2.
1559 set_test_app_id(kTestOfflineEnabledKioskApp);
1560 StartUIForAppLaunch();
1561 SimulateNetworkOffline();
1562 LaunchApp(test_app_id(), false);
1563 WaitForAppLaunchSuccess();
1564 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1567 // Usb stick is plugged in without a manifest file on it.
1568 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppNoManifest) {
1569 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1570 "1.0.0",
1571 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1572 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1574 // Simulate mounting of usb stick with v2 app on the stick.
1575 bool update_success;
1576 bool app_update_notified;
1577 SimulateUpdateAppFromUsbStick(
1578 kFakeUsbMountPathNoManifest, &app_update_notified, &update_success);
1579 EXPECT_FALSE(update_success);
1581 // Kiosk app is not updated.
1582 base::FilePath crx_path;
1583 std::string cached_version;
1584 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1585 test_app_id(), &crx_path, &cached_version));
1586 EXPECT_EQ("1.0.0", cached_version);
1589 // Usb stick is plugged in with a bad manifest file on it.
1590 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppBadManifest) {
1591 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1592 "1.0.0",
1593 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1594 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1596 // Simulate mounting of usb stick with v2 app on the stick.
1597 bool update_success;
1598 bool app_update_notified;
1599 SimulateUpdateAppFromUsbStick(
1600 kFakeUsbMountPathBadManifest, &app_update_notified, &update_success);
1601 EXPECT_FALSE(update_success);
1603 // Kiosk app is not updated.
1604 base::FilePath crx_path;
1605 std::string cached_version;
1606 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1607 test_app_id(), &crx_path, &cached_version));
1608 EXPECT_EQ("1.0.0", cached_version);
1611 // Usb stick is plugged in with a lower version of crx file specified in
1612 // manifest.
1613 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppLowerAppVersion) {
1614 // Precache v2 version of app.
1615 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1616 "2.0.0",
1617 std::string(kTestOfflineEnabledKioskApp) + ".crx");
1618 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1620 // Simulate mounting of usb stick with v1 app on the stick.
1621 bool update_success;
1622 bool app_update_notified;
1623 SimulateUpdateAppFromUsbStick(
1624 kFakeUsbMountPathLowerAppVersion, &app_update_notified, &update_success);
1625 EXPECT_FALSE(update_success);
1627 // Kiosk app is NOT updated to the lower version.
1628 base::FilePath crx_path;
1629 std::string cached_version;
1630 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1631 test_app_id(), &crx_path, &cached_version));
1632 EXPECT_EQ("2.0.0", cached_version);
1635 // Usb stick is plugged in with a v1 crx file, although the manifest says
1636 // this is a v3 version.
1637 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppLowerCrxVersion) {
1638 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1639 "2.0.0",
1640 std::string(kTestOfflineEnabledKioskApp) + ".crx");
1641 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1643 // Simulate mounting of usb stick with v1 crx file on the stick, although
1644 // the manifest says it is v3 app.
1645 bool update_success;
1646 bool app_update_notified;
1647 SimulateUpdateAppFromUsbStick(
1648 kFakeUsbMountPathLowerCrxVersion, &app_update_notified, &update_success);
1649 EXPECT_FALSE(update_success);
1651 // Kiosk app is NOT updated to the lower version.
1652 base::FilePath crx_path;
1653 std::string cached_version;
1654 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1655 test_app_id(), &crx_path, &cached_version));
1656 EXPECT_EQ("2.0.0", cached_version);
1659 // Usb stick is plugged in with a bad crx file.
1660 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, UsbStickUpdateAppBadCrx) {
1661 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1662 "1.0.0",
1663 std::string(kTestOfflineEnabledKioskApp) + "_v1.crx");
1664 EXPECT_EQ("1.0.0", GetInstalledAppVersion().GetString());
1666 // Simulate mounting of usb stick with v1 crx file on the stick, although
1667 // the manifest says it is v3 app.
1668 bool update_success;
1669 bool app_update_notified;
1670 SimulateUpdateAppFromUsbStick(
1671 kFakeUsbMountPathBadCrx, &app_update_notified, &update_success);
1672 EXPECT_FALSE(update_success);
1674 // Kiosk app is NOT updated.
1675 base::FilePath crx_path;
1676 std::string cached_version;
1677 EXPECT_TRUE(KioskAppManager::Get()->GetCachedCrx(
1678 test_app_id(), &crx_path, &cached_version));
1679 EXPECT_EQ("1.0.0", cached_version);
1682 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_PermissionChange) {
1683 PreCacheAndLaunchApp(kTestOfflineEnabledKioskApp,
1684 "2.0.0",
1685 std::string(kTestOfflineEnabledKioskApp) + ".crx");
1688 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PermissionChange) {
1689 set_test_app_id(kTestOfflineEnabledKioskApp);
1690 set_test_app_version("2.0.0");
1691 set_test_crx_file(test_app_id() + "_v2_permission_change.crx");
1693 StartUIForAppLaunch();
1694 SimulateNetworkOnline();
1695 LaunchApp(test_app_id(), false);
1696 WaitForAppLaunchSuccess();
1698 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1701 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PRE_PreserveLocalData) {
1702 // Installs v1 app and writes some local data.
1703 set_test_app_id(kTestLocalFsKioskApp);
1704 set_test_app_version("1.0.0");
1705 set_test_crx_file(test_app_id() + ".crx");
1707 extensions::ResultCatcher catcher;
1708 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
1709 WaitForAppLaunchWithOptions(true /* check_launch_data */,
1710 false /* terminate_app */);
1711 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
1714 IN_PROC_BROWSER_TEST_F(KioskUpdateTest, PreserveLocalData) {
1715 // Update existing v1 app installed in PRE_PreserveLocalData to v2
1716 // that reads and verifies the local data.
1717 set_test_app_id(kTestLocalFsKioskApp);
1718 set_test_app_version("2.0.0");
1719 set_test_crx_file(test_app_id() + "_v2_read_and_verify_data.crx");
1720 extensions::ResultCatcher catcher;
1721 StartAppLaunchFromLoginScreen(SimulateNetworkOnlineClosure());
1722 WaitForAppLaunchWithOptions(true /* check_launch_data */,
1723 false /* terminate_app */);
1725 EXPECT_EQ("2.0.0", GetInstalledAppVersion().GetString());
1726 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
1729 class KioskEnterpriseTest : public KioskTest {
1730 protected:
1731 KioskEnterpriseTest() {
1732 set_use_consumer_kiosk_mode(false);
1735 void SetUpInProcessBrowserTestFixture() override {
1736 device_policy_test_helper_.MarkAsEnterpriseOwned();
1737 device_policy_test_helper_.InstallOwnerKey();
1739 KioskTest::SetUpInProcessBrowserTestFixture();
1742 void SetUpOnMainThread() override {
1743 KioskTest::SetUpOnMainThread();
1745 // Configure OAuth authentication.
1746 GaiaUrls* gaia_urls = GaiaUrls::GetInstance();
1748 // This token satisfies the userinfo.email request from
1749 // DeviceOAuth2TokenService used in token validation.
1750 FakeGaia::AccessTokenInfo userinfo_token_info;
1751 userinfo_token_info.token = kTestUserinfoToken;
1752 userinfo_token_info.scopes.insert(
1753 "https://www.googleapis.com/auth/userinfo.email");
1754 userinfo_token_info.audience = gaia_urls->oauth2_chrome_client_id();
1755 userinfo_token_info.email = kTestEnterpriseServiceAccountId;
1756 fake_gaia_->IssueOAuthToken(kTestRefreshToken, userinfo_token_info);
1758 // The any-api access token for accessing the token minting endpoint.
1759 FakeGaia::AccessTokenInfo login_token_info;
1760 login_token_info.token = kTestLoginToken;
1761 login_token_info.scopes.insert(GaiaConstants::kAnyApiOAuth2Scope);
1762 login_token_info.audience = gaia_urls->oauth2_chrome_client_id();
1763 fake_gaia_->IssueOAuthToken(kTestRefreshToken, login_token_info);
1765 // This is the access token requested by the app via the identity API.
1766 FakeGaia::AccessTokenInfo access_token_info;
1767 access_token_info.token = kTestAccessToken;
1768 access_token_info.scopes.insert(kTestAppScope);
1769 access_token_info.audience = kTestClientId;
1770 access_token_info.email = kTestEnterpriseServiceAccountId;
1771 fake_gaia_->IssueOAuthToken(kTestLoginToken, access_token_info);
1773 DeviceOAuth2TokenService* token_service =
1774 DeviceOAuth2TokenServiceFactory::Get();
1775 token_service->SetAndSaveRefreshToken(
1776 kTestRefreshToken, DeviceOAuth2TokenService::StatusCallback());
1777 base::RunLoop().RunUntilIdle();
1780 static void StorePolicyCallback(const base::Closure& callback, bool result) {
1781 ASSERT_TRUE(result);
1782 callback.Run();
1785 void ConfigureKioskAppInPolicy(const std::string& account_id,
1786 const std::string& app_id,
1787 const std::string& update_url) {
1788 em::DeviceLocalAccountsProto* accounts =
1789 device_policy_test_helper_.device_policy()->payload()
1790 .mutable_device_local_accounts();
1791 em::DeviceLocalAccountInfoProto* account = accounts->add_account();
1792 account->set_account_id(account_id);
1793 account->set_type(
1794 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_KIOSK_APP);
1795 account->mutable_kiosk_app()->set_app_id(app_id);
1796 if (!update_url.empty())
1797 account->mutable_kiosk_app()->set_update_url(update_url);
1798 accounts->set_auto_login_id(account_id);
1799 em::PolicyData& policy_data =
1800 device_policy_test_helper_.device_policy()->policy_data();
1801 policy_data.set_service_account_identity(kTestEnterpriseServiceAccountId);
1802 device_policy_test_helper_.device_policy()->Build();
1804 base::RunLoop run_loop;
1805 DBusThreadManager::Get()->GetSessionManagerClient()->StoreDevicePolicy(
1806 device_policy_test_helper_.device_policy()->GetBlob(),
1807 base::Bind(&KioskEnterpriseTest::StorePolicyCallback,
1808 run_loop.QuitClosure()));
1809 run_loop.Run();
1811 DeviceSettingsService::Get()->Load();
1814 policy::DevicePolicyCrosTestHelper device_policy_test_helper_;
1816 private:
1817 DISALLOW_COPY_AND_ASSIGN(KioskEnterpriseTest);
1820 IN_PROC_BROWSER_TEST_F(KioskEnterpriseTest, EnterpriseKioskApp) {
1821 // Prepare Fake CWS to serve app crx.
1822 set_test_app_id(kTestEnterpriseKioskApp);
1823 set_test_app_version("1.0.0");
1824 set_test_crx_file(test_app_id() + ".crx");
1825 SetupTestAppUpdateCheck();
1827 // Configure kTestEnterpriseKioskApp in device policy.
1828 ConfigureKioskAppInPolicy(kTestEnterpriseAccountId,
1829 kTestEnterpriseKioskApp,
1830 "");
1832 PrepareAppLaunch();
1833 LaunchApp(kTestEnterpriseKioskApp, false);
1835 // Wait for the Kiosk App to launch.
1836 content::WindowedNotificationObserver(
1837 chrome::NOTIFICATION_KIOSK_APP_LAUNCHED,
1838 content::NotificationService::AllSources()).Wait();
1840 // Check installer status.
1841 EXPECT_EQ(chromeos::KioskAppLaunchError::NONE,
1842 chromeos::KioskAppLaunchError::Get());
1844 // Wait for the window to appear.
1845 extensions::AppWindow* window =
1846 AppWindowWaiter(
1847 extensions::AppWindowRegistry::Get(
1848 ProfileManager::GetPrimaryUserProfile()),
1849 kTestEnterpriseKioskApp).Wait();
1850 ASSERT_TRUE(window);
1852 // Check whether the app can retrieve an OAuth2 access token.
1853 std::string result;
1854 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
1855 window->web_contents(),
1856 "chrome.identity.getAuthToken({ 'interactive': false }, function(token) {"
1857 " window.domAutomationController.setAutomationId(0);"
1858 " window.domAutomationController.send(token);"
1859 "});",
1860 &result));
1861 EXPECT_EQ(kTestAccessToken, result);
1863 // Verify that the session is not considered to be logged in with a GAIA
1864 // account.
1865 Profile* app_profile = ProfileManager::GetPrimaryUserProfile();
1866 ASSERT_TRUE(app_profile);
1867 EXPECT_FALSE(
1868 SigninManagerFactory::GetForProfile(app_profile)->IsAuthenticated());
1870 // Terminate the app.
1871 window->GetBaseWindow()->Close();
1872 content::RunAllPendingInMessageLoop();
1875 IN_PROC_BROWSER_TEST_F(KioskEnterpriseTest, PrivateStore) {
1876 set_test_app_id(kTestEnterpriseKioskApp);
1878 const char kPrivateStoreUpdate[] = "/private_store_update";
1879 net::test_server::EmbeddedTestServer private_server;
1880 ASSERT_TRUE(private_server.InitializeAndWaitUntilReady());
1882 // |private_server| serves crx from test data dir.
1883 base::FilePath test_data_dir;
1884 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
1885 private_server.ServeFilesFromDirectory(test_data_dir);
1887 FakeCWS private_store;
1888 private_store.InitAsPrivateStore(&private_server, kPrivateStoreUpdate);
1889 private_store.SetUpdateCrx(kTestEnterpriseKioskApp,
1890 std::string(kTestEnterpriseKioskApp) + ".crx",
1891 "1.0.0");
1893 // Configure kTestEnterpriseKioskApp in device policy.
1894 ConfigureKioskAppInPolicy(kTestEnterpriseAccountId,
1895 kTestEnterpriseKioskApp,
1896 private_server.GetURL(kPrivateStoreUpdate).spec());
1898 // Meta should be able to be extracted from crx before launching.
1899 KioskAppManager* manager = KioskAppManager::Get();
1900 AppDataLoadWaiter waiter(manager, kTestEnterpriseKioskApp, std::string());
1901 waiter.WaitForAppData();
1903 PrepareAppLaunch();
1904 LaunchApp(kTestEnterpriseKioskApp, false);
1905 WaitForAppLaunchWithOptions(false /* check_launch_data */,
1906 true /* terminate_app */);
1908 // Private store should serve crx and CWS should not.
1909 DCHECK_GT(private_store.GetUpdateCheckCountAndReset(), 0);
1910 DCHECK_EQ(0, fake_cws()->GetUpdateCheckCountAndReset());
1913 // Specialized test fixture for testing kiosk mode on the
1914 // hidden WebUI initialization flow for slow hardware.
1915 class KioskHiddenWebUITest : public KioskTest,
1916 public ash::DesktopBackgroundControllerObserver {
1917 public:
1918 KioskHiddenWebUITest() : wallpaper_loaded_(false) {}
1920 // KioskTest overrides:
1921 void SetUpCommandLine(base::CommandLine* command_line) override {
1922 KioskTest::SetUpCommandLine(command_line);
1923 command_line->AppendSwitch(switches::kDisableBootAnimation);
1926 void SetUpOnMainThread() override {
1927 KioskTest::SetUpOnMainThread();
1928 ash::Shell::GetInstance()->desktop_background_controller()
1929 ->AddObserver(this);
1932 void TearDownOnMainThread() override {
1933 ash::Shell::GetInstance()->desktop_background_controller()
1934 ->RemoveObserver(this);
1935 KioskTest::TearDownOnMainThread();
1938 void WaitForWallpaper() {
1939 if (!wallpaper_loaded_) {
1940 runner_ = new content::MessageLoopRunner;
1941 runner_->Run();
1945 bool wallpaper_loaded() const { return wallpaper_loaded_; }
1947 // ash::DesktopBackgroundControllerObserver overrides:
1948 void OnWallpaperDataChanged() override {
1949 wallpaper_loaded_ = true;
1950 if (runner_.get())
1951 runner_->Quit();
1954 bool wallpaper_loaded_;
1955 scoped_refptr<content::MessageLoopRunner> runner_;
1957 DISALLOW_COPY_AND_ASSIGN(KioskHiddenWebUITest);
1960 IN_PROC_BROWSER_TEST_F(KioskHiddenWebUITest, AutolaunchWarning) {
1961 // Add a device owner.
1962 FakeChromeUserManager* user_manager = new FakeChromeUserManager();
1963 user_manager->AddUser(kTestOwnerEmail);
1964 ScopedUserManagerEnabler enabler(user_manager);
1966 // Set kiosk app to autolaunch.
1967 EnableConsumerKioskMode();
1968 WizardController::SkipPostLoginScreensForTesting();
1969 WizardController* wizard_controller = WizardController::default_controller();
1970 CHECK(wizard_controller);
1972 // Start login screen after configuring auto launch app since the warning
1973 // is triggered when switching to login screen.
1974 wizard_controller->AdvanceToScreen(WizardController::kNetworkScreenName);
1975 ReloadAutolaunchKioskApps();
1976 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1978 EXPECT_FALSE(KioskAppManager::Get()->GetAutoLaunchApp().empty());
1979 EXPECT_FALSE(KioskAppManager::Get()->IsAutoLaunchEnabled());
1981 // Wait for the auto launch warning come up.
1982 content::WindowedNotificationObserver(
1983 chrome::NOTIFICATION_KIOSK_AUTOLAUNCH_WARNING_VISIBLE,
1984 content::NotificationService::AllSources()).Wait();
1986 // Wait for the wallpaper to load.
1987 WaitForWallpaper();
1988 EXPECT_TRUE(wallpaper_loaded());
1991 } // namespace chromeos