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