1 // Copyright 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.
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/captive_portal_window_proxy.h"
10 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
11 #include "chrome/browser/chromeos/login/login_manager_test.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/webui_login_view.h"
15 #include "chrome/browser/chromeos/net/network_portal_detector.h"
16 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chromeos/chromeos_switches.h"
24 const char kStubEthernetServicePath[] = "eth1";
26 // Stub implementation of CaptivePortalWindowProxyDelegate, does
27 // nothing and used to instantiate CaptivePortalWindowProxy.
28 class CaptivePortalWindowProxyStubDelegate
29 : public CaptivePortalWindowProxyDelegate {
31 CaptivePortalWindowProxyStubDelegate(): num_portal_notifications_(0) {
34 virtual ~CaptivePortalWindowProxyStubDelegate() {
37 virtual void OnPortalDetected() OVERRIDE {
38 ++num_portal_notifications_;
41 int num_portal_notifications() const { return num_portal_notifications_; }
44 int num_portal_notifications_;
49 class CaptivePortalWindowTest : public InProcessBrowserTest {
51 void ShowIfRedirected() {
52 captive_portal_window_proxy_->ShowIfRedirected();
56 captive_portal_window_proxy_->Show();
60 captive_portal_window_proxy_->Close();
64 captive_portal_window_proxy_->OnRedirected();
67 void OnOriginalURLLoaded() {
68 captive_portal_window_proxy_->OnOriginalURLLoaded();
71 void CheckState(bool is_shown, int num_portal_notifications) {
72 bool actual_is_shown = (CaptivePortalWindowProxy::STATE_DISPLAYED ==
73 captive_portal_window_proxy_->GetState());
74 ASSERT_EQ(is_shown, actual_is_shown);
75 ASSERT_EQ(num_portal_notifications, delegate_.num_portal_notifications());
78 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
79 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
80 command_line->AppendSwitch(chromeos::switches::kLoginManager);
83 virtual void SetUpOnMainThread() OVERRIDE {
84 host_ = LoginDisplayHostImpl::default_host();
86 content::WebContents* web_contents =
87 LoginDisplayHostImpl::default_host()->GetWebUILoginView()->
89 captive_portal_window_proxy_.reset(
90 new CaptivePortalWindowProxy(&delegate_, web_contents));
93 virtual void CleanUpOnMainThread() OVERRIDE {
94 captive_portal_window_proxy_.reset();
95 base::MessageLoopForUI::current()->DeleteSoon(FROM_HERE, host_);
96 base::MessageLoopForUI::current()->RunUntilIdle();
100 scoped_ptr<CaptivePortalWindowProxy> captive_portal_window_proxy_;
101 CaptivePortalWindowProxyStubDelegate delegate_;
103 LoginDisplayHost* host_;
106 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, Show) {
110 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, ShowClose) {
111 CheckState(false, 0);
117 CheckState(false, 0);
120 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, OnRedirected) {
121 CheckState(false, 0);
124 CheckState(false, 0);
130 CheckState(false, 1);
133 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, OnOriginalURLLoaded) {
134 CheckState(false, 0);
137 CheckState(false, 0);
142 OnOriginalURLLoaded();
143 CheckState(false, 1);
146 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, MultipleCalls) {
147 CheckState(false, 0);
150 CheckState(false, 0);
156 CheckState(false, 0);
159 CheckState(false, 1);
161 OnOriginalURLLoaded();
162 CheckState(false, 1);
171 CheckState(false, 2);
173 OnOriginalURLLoaded();
174 CheckState(false, 2);
177 class CaptivePortalWindowCtorDtorTest : public LoginManagerTest {
179 CaptivePortalWindowCtorDtorTest()
180 : LoginManagerTest(false) {}
181 virtual ~CaptivePortalWindowCtorDtorTest() {}
183 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
184 LoginManagerTest::SetUpInProcessBrowserTestFixture();
186 network_portal_detector_ = new NetworkPortalDetectorTestImpl();
187 NetworkPortalDetector::InitializeForTesting(network_portal_detector_);
188 NetworkPortalDetector::CaptivePortalState portal_state;
189 portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
190 portal_state.response_code = 200;
191 network_portal_detector_->SetDefaultNetworkPathForTesting(
192 kStubEthernetServicePath);
193 network_portal_detector_->SetDetectionResultsForTesting(
194 kStubEthernetServicePath, portal_state);
198 NetworkPortalDetectorTestImpl* network_portal_detector() {
199 return network_portal_detector_;
202 PortalDetectorStrategy::StrategyId strategy_id() {
203 return network_portal_detector_->strategy_id();
207 NetworkPortalDetectorTestImpl* network_portal_detector_;
209 DISALLOW_COPY_AND_ASSIGN(CaptivePortalWindowCtorDtorTest);
212 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowCtorDtorTest, PRE_OpenPortalDialog) {
213 StartupUtils::MarkOobeCompleted();
216 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowCtorDtorTest, OpenPortalDialog) {
217 LoginDisplayHostImpl* host =
218 static_cast<LoginDisplayHostImpl*>(LoginDisplayHostImpl::default_host());
220 OobeUI* oobe = host->GetOobeUI();
222 ErrorScreenActor* actor = oobe->GetErrorScreenActor();
225 // Error screen asks portal detector to change detection strategy.
226 ErrorScreen error_screen(NULL, actor);
228 ASSERT_EQ(PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN, strategy_id());
229 network_portal_detector()->NotifyObserversForTesting();
230 OobeScreenWaiter(OobeDisplay::SCREEN_ERROR_MESSAGE).Wait();
231 ASSERT_EQ(PortalDetectorStrategy::STRATEGY_ID_ERROR_SCREEN, strategy_id());
233 actor->ShowCaptivePortal();
236 } // namespace chromeos