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"
43 void ErrorCallbackFunction(const std::string
& error_name
,
44 const std::string
& error_message
) {
45 LOG(ERROR
) << "Shill Error: " << error_name
<< " : " << error_message
;
50 class DeviceDisablingTest
51 : public OobeBaseTest
,
52 public NetworkStateInformer::NetworkStateInformerObserver
{
54 DeviceDisablingTest();
56 // Sets up a device state blob that indicates the device is disabled, triggers
57 // a policy plus device state fetch and waits for it to succeed.
58 void MarkDisabledAndWaitForPolicyFetch();
60 std::string
GetCurrentScreenName(content::WebContents
* web_contents
);
63 base::RunLoop network_state_change_wait_run_loop_
;
67 void SetUpInProcessBrowserTestFixture() override
;
68 void SetUpOnMainThread() override
;
70 // NetworkStateInformer::NetworkStateInformerObserver:
71 void UpdateState(NetworkError::ErrorReason reason
) override
;
73 FakeSessionManagerClient
* fake_session_manager_client_
;
74 policy::DevicePolicyCrosTestHelper test_helper_
;
76 DISALLOW_COPY_AND_ASSIGN(DeviceDisablingTest
);
80 DeviceDisablingTest::DeviceDisablingTest()
81 : fake_session_manager_client_(new FakeSessionManagerClient
) {
84 void DeviceDisablingTest::MarkDisabledAndWaitForPolicyFetch() {
85 base::RunLoop run_loop
;
86 // Set up an |observer| that will wait for the disabled setting to change.
87 scoped_ptr
<CrosSettings::ObserverSubscription
> observer
=
88 CrosSettings::Get()->AddSettingsObserver(
90 run_loop
.QuitClosure());
91 // Prepare a policy fetch response that indicates the device is disabled.
92 test_helper_
.device_policy()->policy_data().mutable_device_state()->
93 set_device_mode(enterprise_management::DeviceState::DEVICE_MODE_DISABLED
);
94 test_helper_
.device_policy()->Build();
95 fake_session_manager_client_
->set_device_policy(
96 test_helper_
.device_policy()->GetBlob());
97 // Trigger a policy fetch.
98 fake_session_manager_client_
->OnPropertyChangeComplete(true);
99 // Wait for the policy fetch to complete and the disabled setting to change.
103 std::string
DeviceDisablingTest::GetCurrentScreenName(
104 content::WebContents
* web_contents
) {
105 std::string screen_name
;
106 if (!content::ExecuteScriptAndExtractString(
108 "domAutomationController.send(Oobe.getInstance().currentScreen.id);",
115 void DeviceDisablingTest::SetUpInProcessBrowserTestFixture() {
116 OobeBaseTest::SetUpInProcessBrowserTestFixture();
118 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
119 scoped_ptr
<SessionManagerClient
>(fake_session_manager_client_
));
121 test_helper_
.InstallOwnerKey();
122 test_helper_
.MarkAsEnterpriseOwned();
125 void DeviceDisablingTest::SetUpOnMainThread() {
126 OobeBaseTest::SetUpOnMainThread();
128 // Set up fake networks.
129 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()->
130 SetupDefaultEnvironment();
133 void DeviceDisablingTest::UpdateState(NetworkError::ErrorReason reason
) {
134 network_state_change_wait_run_loop_
.Quit();
137 IN_PROC_BROWSER_TEST_F(DeviceDisablingTest
, DisableDuringNormalOperation
) {
138 // Mark the device as disabled and wait until cros settings update.
139 MarkDisabledAndWaitForPolicyFetch();
141 // Verify that the device disabled screen is being shown.
142 WizardController
* const wizard_controller
=
143 WizardController::default_controller();
144 ASSERT_TRUE(wizard_controller
);
145 EXPECT_EQ(wizard_controller
->GetScreen(
146 WizardController::kDeviceDisabledScreenName
),
147 wizard_controller
->current_screen());
150 // Verifies that device disabling works when the ephemeral users policy is
151 // enabled. This case warrants its own test because the UI behaves somewhat
152 // differently when the policy is set: A background job runs on startup that
153 // causes the UI to try and show the login screen after some delay. It must
154 // be ensured that the login screen does not show and does not clobber the
156 IN_PROC_BROWSER_TEST_F(DeviceDisablingTest
, DisableWithEphemeralUsers
) {
157 // Connect to the fake Ethernet network. This ensures that Chrome OS will not
158 // try to show the offline error screen.
159 base::RunLoop connect_run_loop
;
160 DBusThreadManager::Get()->GetShillServiceClient()->Connect(
161 dbus::ObjectPath("/service/eth1"),
162 connect_run_loop
.QuitClosure(),
163 base::Bind(&ErrorCallbackFunction
));
164 connect_run_loop
.Run();
166 // Skip to the login screen.
167 WizardController
* wizard_controller
= WizardController::default_controller();
168 ASSERT_TRUE(wizard_controller
);
169 wizard_controller
->SkipToLoginForTesting(LoginScreenContext());
170 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN
).Wait();
172 // Mark the device as disabled and wait until cros settings update.
173 MarkDisabledAndWaitForPolicyFetch();
175 // When the ephemeral users policy is enabled, Chrome OS removes any non-owner
176 // cryptohomes on startup. At the end of that process, JavaScript attempts to
177 // show the login screen. Simulate this.
178 const LoginDisplayHostImpl
* const host
=
179 static_cast<LoginDisplayHostImpl
*>(LoginDisplayHostImpl::default_host());
181 WebUILoginView
* const webui_login_view
= host
->GetWebUILoginView();
182 ASSERT_TRUE(webui_login_view
);
183 content::WebContents
* const web_contents
= webui_login_view
->GetWebContents();
184 ASSERT_TRUE(web_contents
);
185 ASSERT_TRUE(content::ExecuteScript(web_contents
,
186 "Oobe.showAddUserForTesting();"));
188 // The login profile is scrubbed before attempting to show the login screen.
189 // Wait for the scrubbing to finish.
190 base::RunLoop run_loop
;
191 ProfileHelper::Get()->ClearSigninProfile(run_loop
.QuitClosure());
193 base::RunLoop().RunUntilIdle();
195 // Verify that the login screen was not shown and the device disabled screen
196 // is still being shown instead.
197 EXPECT_EQ(OobeUI::kScreenDeviceDisabled
, GetCurrentScreenName(web_contents
));
199 // Disconnect from the fake Ethernet network.
200 OobeUI
* const oobe_ui
= host
->GetOobeUI();
201 ASSERT_TRUE(oobe_ui
);
202 const scoped_refptr
<NetworkStateInformer
> network_state_informer
=
203 oobe_ui
->network_state_informer_for_test();
204 ASSERT_TRUE(network_state_informer
);
205 network_state_informer
->AddObserver(this);
206 SigninScreenHandler
* const signin_screen_handler
=
207 oobe_ui
->signin_screen_handler_for_test();
208 ASSERT_TRUE(signin_screen_handler
);
209 signin_screen_handler
->ZeroOfflineTimeoutForTesting();
210 SimulateNetworkOffline();
211 network_state_change_wait_run_loop_
.Run();
212 network_state_informer
->RemoveObserver(this);
213 base::RunLoop().RunUntilIdle();
215 // Verify that the offline error screen was not shown and the device disabled
216 // screen is still being shown instead.
217 EXPECT_EQ(OobeUI::kScreenDeviceDisabled
, GetCurrentScreenName(web_contents
));
220 } // namespace system
221 } // namespace chromeos