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.
5 #include "base/command_line.h"
6 #include "base/compiler_specific.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/chromeos/login/login_manager_test.h"
10 #include "chrome/browser/chromeos/login/screens/error_screen.h"
11 #include "chrome/browser/chromeos/login/screens/network_error_view.h"
12 #include "chrome/browser/chromeos/login/startup_utils.h"
13 #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
14 #include "chrome/browser/chromeos/login/ui/captive_portal_window_proxy.h"
15 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
16 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
17 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chromeos/chromeos_switches.h"
20 #include "chromeos/dbus/fake_shill_manager_client.h"
21 #include "chromeos/network/portal_detector/network_portal_detector.h"
27 // Stub implementation of CaptivePortalWindowProxyDelegate, does
28 // nothing and used to instantiate CaptivePortalWindowProxy.
29 class CaptivePortalWindowProxyStubDelegate
30 : public CaptivePortalWindowProxyDelegate
{
32 CaptivePortalWindowProxyStubDelegate(): num_portal_notifications_(0) {
35 ~CaptivePortalWindowProxyStubDelegate() override
{}
37 void OnPortalDetected() override
{ ++num_portal_notifications_
; }
39 int num_portal_notifications() const { return num_portal_notifications_
; }
42 int num_portal_notifications_
;
47 class CaptivePortalWindowTest
: public InProcessBrowserTest
{
49 void ShowIfRedirected() {
50 captive_portal_window_proxy_
->ShowIfRedirected();
54 captive_portal_window_proxy_
->Show();
58 captive_portal_window_proxy_
->Close();
62 captive_portal_window_proxy_
->OnRedirected();
65 void OnOriginalURLLoaded() {
66 captive_portal_window_proxy_
->OnOriginalURLLoaded();
69 void CheckState(bool is_shown
, int num_portal_notifications
) {
70 bool actual_is_shown
= (CaptivePortalWindowProxy::STATE_DISPLAYED
==
71 captive_portal_window_proxy_
->GetState());
72 ASSERT_EQ(is_shown
, actual_is_shown
);
73 ASSERT_EQ(num_portal_notifications
, delegate_
.num_portal_notifications());
76 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
77 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
78 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
81 void SetUpOnMainThread() override
{
82 host_
= LoginDisplayHostImpl::default_host();
84 content::WebContents
* web_contents
=
85 LoginDisplayHostImpl::default_host()->GetWebUILoginView()->
87 captive_portal_window_proxy_
.reset(
88 new CaptivePortalWindowProxy(&delegate_
, web_contents
));
91 void TearDownOnMainThread() override
{
92 captive_portal_window_proxy_
.reset();
93 base::MessageLoopForUI::current()->DeleteSoon(FROM_HERE
, host_
);
94 base::MessageLoopForUI::current()->RunUntilIdle();
98 scoped_ptr
<CaptivePortalWindowProxy
> captive_portal_window_proxy_
;
99 CaptivePortalWindowProxyStubDelegate delegate_
;
101 LoginDisplayHost
* host_
;
104 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest
, Show
) {
108 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest
, ShowClose
) {
109 CheckState(false, 0);
115 CheckState(false, 0);
118 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest
, OnRedirected
) {
119 CheckState(false, 0);
122 CheckState(false, 0);
128 CheckState(false, 1);
131 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest
, OnOriginalURLLoaded
) {
132 CheckState(false, 0);
135 CheckState(false, 0);
140 OnOriginalURLLoaded();
141 CheckState(false, 1);
144 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest
, MultipleCalls
) {
145 CheckState(false, 0);
148 CheckState(false, 0);
154 CheckState(false, 0);
157 CheckState(false, 1);
159 OnOriginalURLLoaded();
160 CheckState(false, 1);
169 CheckState(false, 2);
171 OnOriginalURLLoaded();
172 CheckState(false, 2);
175 class CaptivePortalWindowCtorDtorTest
: public LoginManagerTest
{
177 CaptivePortalWindowCtorDtorTest()
178 : LoginManagerTest(false) {}
179 ~CaptivePortalWindowCtorDtorTest() override
{}
181 void SetUpInProcessBrowserTestFixture() override
{
182 LoginManagerTest::SetUpInProcessBrowserTestFixture();
184 network_portal_detector_
= new NetworkPortalDetectorTestImpl();
185 NetworkPortalDetector::InitializeForTesting(network_portal_detector_
);
186 NetworkPortalDetector::CaptivePortalState portal_state
;
187 portal_state
.status
= NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL
;
188 portal_state
.response_code
= 200;
189 network_portal_detector_
->SetDefaultNetworkForTesting(
190 FakeShillManagerClient::kFakeEthernetNetworkGuid
);
191 network_portal_detector_
->SetDetectionResultsForTesting(
192 FakeShillManagerClient::kFakeEthernetNetworkGuid
,
197 NetworkPortalDetectorTestImpl
* network_portal_detector() {
198 return network_portal_detector_
;
201 PortalDetectorStrategy::StrategyId
strategy_id() {
202 return network_portal_detector_
->strategy_id();
206 NetworkPortalDetectorTestImpl
* network_portal_detector_
;
208 DISALLOW_COPY_AND_ASSIGN(CaptivePortalWindowCtorDtorTest
);
211 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowCtorDtorTest
, PRE_OpenPortalDialog
) {
212 StartupUtils::MarkOobeCompleted();
215 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowCtorDtorTest
, OpenPortalDialog
) {
216 LoginDisplayHostImpl
* host
=
217 static_cast<LoginDisplayHostImpl
*>(LoginDisplayHostImpl::default_host());
219 OobeUI
* oobe
= host
->GetOobeUI();
222 // Error screen asks portal detector to change detection strategy.
223 ErrorScreen
* error_screen
= oobe
->GetErrorScreen();
224 ASSERT_TRUE(error_screen
);
226 ASSERT_EQ(PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN
, strategy_id());
227 network_portal_detector()->NotifyObserversForTesting();
228 OobeScreenWaiter(OobeDisplay::SCREEN_ERROR_MESSAGE
).Wait();
229 ASSERT_EQ(PortalDetectorStrategy::STRATEGY_ID_ERROR_SCREEN
, strategy_id());
231 error_screen
->ShowCaptivePortal();
234 } // namespace chromeos