1 // Copyright 2014 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.
8 #include "base/logging.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "chrome/browser/chromeos/login/test/oobe_base_test.h"
14 #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
15 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
16 #include "chrome/browser/chromeos/login/ui/oobe_display.h"
17 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
18 #include "chrome/browser/chromeos/login/wizard_controller.h"
19 #include "chrome/browser/chromeos/policy/device_policy_builder.h"
20 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
21 #include "chrome/browser/chromeos/profiles/profile_helper.h"
22 #include "chrome/browser/chromeos/settings/cros_settings.h"
23 #include "chrome/browser/ui/webui/chromeos/login/network_state_informer.h"
24 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
25 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
26 #include "chromeos/dbus/dbus_thread_manager.h"
27 #include "chromeos/dbus/fake_session_manager_client.h"
28 #include "chromeos/dbus/fake_shill_manager_client.h"
29 #include "chromeos/dbus/session_manager_client.h"
30 #include "chromeos/dbus/shill_manager_client.h"
31 #include "chromeos/dbus/shill_service_client.h"
32 #include "chromeos/settings/cros_settings_names.h"
33 #include "content/public/browser/web_contents.h"
34 #include "content/public/test/browser_test_utils.h"
35 #include "dbus/object_path.h"
36 #include "policy/proto/device_management_backend.pb.h"
37 #include "testing/gtest/include/gtest/gtest.h"
44 void ErrorCallbackFunction(const std::string
& error_name
,
45 const std::string
& error_message
) {
46 LOG(ERROR
) << "Shill Error: " << error_name
<< " : " << error_message
;
51 // Boolean parameter is used to run this test for webview (true) and for
52 // iframe (false) GAIA sign in.
53 class DeviceDisablingTest
54 : public OobeBaseTest
,
55 public NetworkStateInformer::NetworkStateInformerObserver
,
56 public testing::WithParamInterface
<bool> {
58 DeviceDisablingTest();
60 // Sets up a device state blob that indicates the device is disabled, triggers
61 // a policy plus device state fetch and waits for it to succeed.
62 void MarkDisabledAndWaitForPolicyFetch();
64 std::string
GetCurrentScreenName(content::WebContents
* web_contents
);
67 base::RunLoop network_state_change_wait_run_loop_
;
71 void SetUpInProcessBrowserTestFixture() override
;
72 void SetUpOnMainThread() override
;
74 // NetworkStateInformer::NetworkStateInformerObserver:
75 void UpdateState(NetworkError::ErrorReason reason
) override
;
77 FakeSessionManagerClient
* fake_session_manager_client_
;
78 policy::DevicePolicyCrosTestHelper test_helper_
;
80 DISALLOW_COPY_AND_ASSIGN(DeviceDisablingTest
);
84 DeviceDisablingTest::DeviceDisablingTest()
85 : fake_session_manager_client_(new FakeSessionManagerClient
) {
86 set_use_webview(GetParam());
89 void DeviceDisablingTest::MarkDisabledAndWaitForPolicyFetch() {
90 base::RunLoop run_loop
;
91 // Set up an |observer| that will wait for the disabled setting to change.
92 scoped_ptr
<CrosSettings::ObserverSubscription
> observer
=
93 CrosSettings::Get()->AddSettingsObserver(
95 run_loop
.QuitClosure());
96 // Prepare a policy fetch response that indicates the device is disabled.
97 test_helper_
.device_policy()->policy_data().mutable_device_state()->
98 set_device_mode(enterprise_management::DeviceState::DEVICE_MODE_DISABLED
);
99 test_helper_
.device_policy()->Build();
100 fake_session_manager_client_
->set_device_policy(
101 test_helper_
.device_policy()->GetBlob());
102 // Trigger a policy fetch.
103 fake_session_manager_client_
->OnPropertyChangeComplete(true);
104 // Wait for the policy fetch to complete and the disabled setting to change.
108 std::string
DeviceDisablingTest::GetCurrentScreenName(
109 content::WebContents
* web_contents
) {
110 std::string screen_name
;
111 if (!content::ExecuteScriptAndExtractString(
113 "domAutomationController.send(Oobe.getInstance().currentScreen.id);",
120 void DeviceDisablingTest::SetUpInProcessBrowserTestFixture() {
121 OobeBaseTest::SetUpInProcessBrowserTestFixture();
123 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
124 scoped_ptr
<SessionManagerClient
>(fake_session_manager_client_
));
126 test_helper_
.InstallOwnerKey();
127 test_helper_
.MarkAsEnterpriseOwned();
130 void DeviceDisablingTest::SetUpOnMainThread() {
131 OobeBaseTest::SetUpOnMainThread();
133 // Set up fake networks.
134 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
135 SetupDefaultEnvironment();
138 void DeviceDisablingTest::UpdateState(NetworkError::ErrorReason reason
) {
139 network_state_change_wait_run_loop_
.Quit();
142 IN_PROC_BROWSER_TEST_P(DeviceDisablingTest
, DisableDuringNormalOperation
) {
143 // Mark the device as disabled and wait until cros settings update.
144 MarkDisabledAndWaitForPolicyFetch();
146 // Verify that the device disabled screen is being shown.
147 WizardController
* const wizard_controller
=
148 WizardController::default_controller();
149 ASSERT_TRUE(wizard_controller
);
150 EXPECT_EQ(wizard_controller
->GetScreen(
151 WizardController::kDeviceDisabledScreenName
),
152 wizard_controller
->current_screen());
155 // Verifies that device disabling works when the ephemeral users policy is
156 // enabled. This case warrants its own test because the UI behaves somewhat
157 // differently when the policy is set: A background job runs on startup that
158 // causes the UI to try and show the login screen after some delay. It must
159 // be ensured that the login screen does not show and does not clobber the
161 IN_PROC_BROWSER_TEST_P(DeviceDisablingTest
, DisableWithEphemeralUsers
) {
162 // Connect to the fake Ethernet network. This ensures that Chrome OS will not
163 // try to show the offline error screen.
164 base::RunLoop connect_run_loop
;
165 DBusThreadManager::Get()->GetShillServiceClient()->Connect(
166 dbus::ObjectPath("/service/eth1"),
167 connect_run_loop
.QuitClosure(),
168 base::Bind(&ErrorCallbackFunction
));
169 connect_run_loop
.Run();
171 // Skip to the login screen.
172 WizardController
* wizard_controller
= WizardController::default_controller();
173 ASSERT_TRUE(wizard_controller
);
174 wizard_controller
->SkipToLoginForTesting(LoginScreenContext());
175 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN
).Wait();
177 // Mark the device as disabled and wait until cros settings update.
178 MarkDisabledAndWaitForPolicyFetch();
180 // When the ephemeral users policy is enabled, Chrome OS removes any non-owner
181 // cryptohomes on startup. At the end of that process, JavaScript attempts to
182 // show the login screen. Simulate this.
183 const LoginDisplayHostImpl
* const host
=
184 static_cast<LoginDisplayHostImpl
*>(LoginDisplayHostImpl::default_host());
186 WebUILoginView
* const webui_login_view
= host
->GetWebUILoginView();
187 ASSERT_TRUE(webui_login_view
);
188 content::WebContents
* const web_contents
= webui_login_view
->GetWebContents();
189 ASSERT_TRUE(web_contents
);
190 ASSERT_TRUE(content::ExecuteScript(web_contents
,
191 "Oobe.showAddUserForTesting();"));
193 // The login profile is scrubbed before attempting to show the login screen.
194 // Wait for the scrubbing to finish.
195 base::RunLoop run_loop
;
196 ProfileHelper::Get()->ClearSigninProfile(run_loop
.QuitClosure());
198 base::RunLoop().RunUntilIdle();
200 // Verify that the login screen was not shown and the device disabled screen
201 // is still being shown instead.
202 EXPECT_EQ(OobeUI::kScreenDeviceDisabled
, GetCurrentScreenName(web_contents
));
204 // Disconnect from the fake Ethernet network.
205 OobeUI
* const oobe_ui
= host
->GetOobeUI();
206 ASSERT_TRUE(oobe_ui
);
207 const scoped_refptr
<NetworkStateInformer
> network_state_informer
=
208 oobe_ui
->network_state_informer_for_test();
209 ASSERT_TRUE(network_state_informer
);
210 network_state_informer
->AddObserver(this);
211 SigninScreenHandler
* const signin_screen_handler
=
212 oobe_ui
->signin_screen_handler_for_test();
213 ASSERT_TRUE(signin_screen_handler
);
214 signin_screen_handler
->ZeroOfflineTimeoutForTesting();
215 SimulateNetworkOffline();
216 network_state_change_wait_run_loop_
.Run();
217 network_state_informer
->RemoveObserver(this);
218 base::RunLoop().RunUntilIdle();
220 // Verify that the offline error screen was not shown and the device disabled
221 // screen is still being shown instead.
222 EXPECT_EQ(OobeUI::kScreenDeviceDisabled
, GetCurrentScreenName(web_contents
));
225 INSTANTIATE_TEST_CASE_P(DeviceDisablingSuite
,
229 } // namespace system
230 } // namespace chromeos