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.
4
5 #include "base/command_line.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/chromeos/login/existing_user_controller.h"
8 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
9 #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
10 #include "chrome/browser/chromeos/login/webui_login_display.h"
11 #include "chrome/browser/chromeos/login/wizard_controller.h"
12 #include "chrome/browser/lifetime/application_lifetime.h"
13 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chromeos/chromeos_switches.h"
17 #include "content/public/test/test_utils.h"
18 #include "google_apis/gaia/fake_gaia.h"
19 #include "google_apis/gaia/gaia_switches.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
21 #include "net/test/embedded_test_server/http_response.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/base/test/ui_controls.h"
24 #include "ui/views/widget/widget.h"
25
26 using namespace net::test_server;
27
28 namespace chromeos {
29
30 class OobeTest : public InProcessBrowserTest {
31 public:
OobeTest()32 OobeTest() {}
~OobeTest()33 virtual ~OobeTest() {}
34
SetUpCommandLine(CommandLine * command_line)35 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
36 command_line->AppendSwitch(chromeos::switches::kLoginManager);
37 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
38 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
39 command_line->AppendSwitchASCII(
40 chromeos::switches::kAuthExtensionPath, "gaia_auth");
41 }
42
SetUpOnMainThread()43 virtual void SetUpOnMainThread() OVERRIDE {
44 CHECK(embedded_test_server()->InitializeAndWaitUntilReady());
45 embedded_test_server()->RegisterRequestHandler(
46 base::Bind(&FakeGaia::HandleRequest, base::Unretained(&fake_gaia_)));
47 LOG(INFO) << "Set up http server at " << embedded_test_server()->base_url();
48
49 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
50 ::switches::kGaiaUrl, embedded_test_server()->base_url().spec());
51 }
52
CleanUpOnMainThread()53 virtual void CleanUpOnMainThread() OVERRIDE {
54 // If the login display is still showing, exit gracefully.
55 if (LoginDisplayHostImpl::default_host()) {
56 base::MessageLoop::current()->PostTask(FROM_HERE,
57 base::Bind(&chrome::AttemptExit));
58 content::RunMessageLoop();
59 }
60 }
61
GetLoginDisplay()62 chromeos::WebUILoginDisplay* GetLoginDisplay() {
63 chromeos::ExistingUserController* controller =
64 chromeos::ExistingUserController::current_controller();
65 CHECK(controller);
66 return static_cast<chromeos::WebUILoginDisplay*>(
67 controller->login_display());
68 }
69
GetLoginWindowWidget()70 views::Widget* GetLoginWindowWidget() {
71 return static_cast<chromeos::LoginDisplayHostImpl*>(
72 chromeos::LoginDisplayHostImpl::default_host())
73 ->login_window_for_test();
74 }
75
76 private:
77 FakeGaia fake_gaia_;
78 DISALLOW_COPY_AND_ASSIGN(OobeTest);
79 };
80
IN_PROC_BROWSER_TEST_F(OobeTest,NewUser)81 IN_PROC_BROWSER_TEST_F(OobeTest, NewUser) {
82 chromeos::WizardController::SkipPostLoginScreensForTesting();
83 chromeos::WizardController* wizard_controller =
84 chromeos::WizardController::default_controller();
85 CHECK(wizard_controller);
86 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
87
88 content::WindowedNotificationObserver(
89 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
90 content::NotificationService::AllSources()).Wait();
91
92 // TODO(glotov): mock GAIA server (test_server()) should support
93 // username/password configuration.
94 GetLoginDisplay()->ShowSigninScreenForCreds("username", "password");
95
96 content::WindowedNotificationObserver(
97 chrome::NOTIFICATION_SESSION_STARTED,
98 content::NotificationService::AllSources()).Wait();
99 }
100
IN_PROC_BROWSER_TEST_F(OobeTest,Accelerator)101 IN_PROC_BROWSER_TEST_F(OobeTest, Accelerator) {
102 chromeos::WizardController::SkipPostLoginScreensForTesting();
103 chromeos::WizardController* wizard_controller =
104 chromeos::WizardController::default_controller();
105 CHECK(wizard_controller);
106 wizard_controller->SkipToLoginForTesting(LoginScreenContext());
107
108 content::WindowedNotificationObserver(
109 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
110 content::NotificationService::AllSources()).Wait();
111
112 gfx::NativeWindow login_window = GetLoginWindowWidget()->GetNativeWindow();
113
114 ui_controls::SendKeyPress(login_window,
115 ui::VKEY_E,
116 true, // control
117 false, // shift
118 true, // alt
119 false); // command
120 OobeScreenWaiter(OobeDisplay::SCREEN_OOBE_ENROLLMENT).Wait();
121 }
122
123 } // namespace chromeos
124