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.
4
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/startup_utils.h"
11#include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
12#include "chrome/browser/chromeos/login/ui/captive_portal_window_proxy.h"
13#include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
14#include "chrome/browser/chromeos/login/ui/webui_login_view.h"
15#include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
16#include "chrome/test/base/in_process_browser_test.h"
17#include "chromeos/chromeos_switches.h"
18#include "chromeos/dbus/fake_shill_manager_client.h"
19#include "chromeos/network/portal_detector/network_portal_detector.h"
20
21namespace chromeos {
22
23namespace {
24
25// Stub implementation of CaptivePortalWindowProxyDelegate, does
26// nothing and used to instantiate CaptivePortalWindowProxy.
27class CaptivePortalWindowProxyStubDelegate
28    : public CaptivePortalWindowProxyDelegate {
29 public:
30  CaptivePortalWindowProxyStubDelegate(): num_portal_notifications_(0) {
31  }
32
33  virtual ~CaptivePortalWindowProxyStubDelegate() {
34  }
35
36  virtual void OnPortalDetected() OVERRIDE {
37    ++num_portal_notifications_;
38  }
39
40  int num_portal_notifications() const { return num_portal_notifications_; }
41
42 private:
43  int num_portal_notifications_;
44};
45
46}  // namespace
47
48class CaptivePortalWindowTest : public InProcessBrowserTest {
49 protected:
50  void ShowIfRedirected() {
51    captive_portal_window_proxy_->ShowIfRedirected();
52  }
53
54  void Show() {
55    captive_portal_window_proxy_->Show();
56  }
57
58  void Close() {
59    captive_portal_window_proxy_->Close();
60  }
61
62  void OnRedirected() {
63    captive_portal_window_proxy_->OnRedirected();
64  }
65
66  void OnOriginalURLLoaded() {
67    captive_portal_window_proxy_->OnOriginalURLLoaded();
68  }
69
70  void CheckState(bool is_shown, int num_portal_notifications) {
71    bool actual_is_shown = (CaptivePortalWindowProxy::STATE_DISPLAYED ==
72                            captive_portal_window_proxy_->GetState());
73    ASSERT_EQ(is_shown, actual_is_shown);
74    ASSERT_EQ(num_portal_notifications, delegate_.num_portal_notifications());
75  }
76
77  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
78    command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
79    command_line->AppendSwitch(chromeos::switches::kLoginManager);
80  }
81
82  virtual void SetUpOnMainThread() OVERRIDE {
83    host_ = LoginDisplayHostImpl::default_host();
84    CHECK(host_);
85    content::WebContents* web_contents =
86        LoginDisplayHostImpl::default_host()->GetWebUILoginView()->
87            GetWebContents();
88    captive_portal_window_proxy_.reset(
89        new CaptivePortalWindowProxy(&delegate_, web_contents));
90  }
91
92  virtual void TearDownOnMainThread() OVERRIDE {
93    captive_portal_window_proxy_.reset();
94    base::MessageLoopForUI::current()->DeleteSoon(FROM_HERE, host_);
95    base::MessageLoopForUI::current()->RunUntilIdle();
96  }
97
98 private:
99  scoped_ptr<CaptivePortalWindowProxy> captive_portal_window_proxy_;
100  CaptivePortalWindowProxyStubDelegate delegate_;
101
102  LoginDisplayHost* host_;
103};
104
105IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, Show) {
106  Show();
107}
108
109IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, ShowClose) {
110  CheckState(false, 0);
111
112  Show();
113  CheckState(true, 0);
114
115  Close();
116  CheckState(false, 0);
117}
118
119IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, OnRedirected) {
120  CheckState(false, 0);
121
122  ShowIfRedirected();
123  CheckState(false, 0);
124
125  OnRedirected();
126  CheckState(true, 1);
127
128  Close();
129  CheckState(false, 1);
130}
131
132IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, OnOriginalURLLoaded) {
133  CheckState(false, 0);
134
135  ShowIfRedirected();
136  CheckState(false, 0);
137
138  OnRedirected();
139  CheckState(true, 1);
140
141  OnOriginalURLLoaded();
142  CheckState(false, 1);
143}
144
145IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, MultipleCalls) {
146  CheckState(false, 0);
147
148  ShowIfRedirected();
149  CheckState(false, 0);
150
151  Show();
152  CheckState(true, 0);
153
154  Close();
155  CheckState(false, 0);
156
157  OnRedirected();
158  CheckState(false, 1);
159
160  OnOriginalURLLoaded();
161  CheckState(false, 1);
162
163  Show();
164  CheckState(true, 1);
165
166  OnRedirected();
167  CheckState(true, 2);
168
169  Close();
170  CheckState(false, 2);
171
172  OnOriginalURLLoaded();
173  CheckState(false, 2);
174}
175
176class CaptivePortalWindowCtorDtorTest : public LoginManagerTest {
177 public:
178  CaptivePortalWindowCtorDtorTest()
179      : LoginManagerTest(false) {}
180  virtual ~CaptivePortalWindowCtorDtorTest() {}
181
182  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
183    LoginManagerTest::SetUpInProcessBrowserTestFixture();
184
185    network_portal_detector_ = new NetworkPortalDetectorTestImpl();
186    NetworkPortalDetector::InitializeForTesting(network_portal_detector_);
187    NetworkPortalDetector::CaptivePortalState portal_state;
188    portal_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL;
189    portal_state.response_code = 200;
190    network_portal_detector_->SetDefaultNetworkForTesting(
191        FakeShillManagerClient::kFakeEthernetNetworkGuid);
192    network_portal_detector_->SetDetectionResultsForTesting(
193        FakeShillManagerClient::kFakeEthernetNetworkGuid,
194        portal_state);
195  }
196
197 protected:
198  NetworkPortalDetectorTestImpl* network_portal_detector() {
199    return network_portal_detector_;
200  }
201
202  PortalDetectorStrategy::StrategyId strategy_id() {
203    return network_portal_detector_->strategy_id();
204  }
205
206 private:
207  NetworkPortalDetectorTestImpl* network_portal_detector_;
208
209  DISALLOW_COPY_AND_ASSIGN(CaptivePortalWindowCtorDtorTest);
210};
211
212IN_PROC_BROWSER_TEST_F(CaptivePortalWindowCtorDtorTest, PRE_OpenPortalDialog) {
213  StartupUtils::MarkOobeCompleted();
214}
215
216IN_PROC_BROWSER_TEST_F(CaptivePortalWindowCtorDtorTest, OpenPortalDialog) {
217  LoginDisplayHostImpl* host =
218      static_cast<LoginDisplayHostImpl*>(LoginDisplayHostImpl::default_host());
219  ASSERT_TRUE(host);
220  OobeUI* oobe = host->GetOobeUI();
221  ASSERT_TRUE(oobe);
222  ErrorScreenActor* actor = oobe->GetErrorScreenActor();
223  ASSERT_TRUE(actor);
224
225  // Error screen asks portal detector to change detection strategy.
226  ErrorScreen error_screen(NULL, actor);
227
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());
232
233  actor->ShowCaptivePortal();
234}
235
236}  // namespace chromeos
237