Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / captive_portal_window_browsertest.cc
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.
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/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"
19
20 namespace chromeos {
21
22 namespace {
23
24 const char kStubEthernetServicePath[] = "eth1";
25
26 // Stub implementation of CaptivePortalWindowProxyDelegate, does
27 // nothing and used to instantiate CaptivePortalWindowProxy.
28 class CaptivePortalWindowProxyStubDelegate
29     : public CaptivePortalWindowProxyDelegate {
30  public:
31   CaptivePortalWindowProxyStubDelegate(): num_portal_notifications_(0) {
32   }
33
34   virtual ~CaptivePortalWindowProxyStubDelegate() {
35   }
36
37   virtual void OnPortalDetected() OVERRIDE {
38     ++num_portal_notifications_;
39   }
40
41   int num_portal_notifications() const { return num_portal_notifications_; }
42
43  private:
44   int num_portal_notifications_;
45 };
46
47 }  // namespace
48
49 class CaptivePortalWindowTest : public InProcessBrowserTest {
50  protected:
51   void ShowIfRedirected() {
52     captive_portal_window_proxy_->ShowIfRedirected();
53   }
54
55   void Show() {
56     captive_portal_window_proxy_->Show();
57   }
58
59   void Close() {
60     captive_portal_window_proxy_->Close();
61   }
62
63   void OnRedirected() {
64     captive_portal_window_proxy_->OnRedirected();
65   }
66
67   void OnOriginalURLLoaded() {
68     captive_portal_window_proxy_->OnOriginalURLLoaded();
69   }
70
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());
76   }
77
78   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
79     command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
80     command_line->AppendSwitch(chromeos::switches::kLoginManager);
81   }
82
83   virtual void SetUpOnMainThread() OVERRIDE {
84     host_ = LoginDisplayHostImpl::default_host();
85     CHECK(host_);
86     content::WebContents* web_contents =
87         LoginDisplayHostImpl::default_host()->GetWebUILoginView()->
88             GetWebContents();
89     captive_portal_window_proxy_.reset(
90         new CaptivePortalWindowProxy(&delegate_, web_contents));
91   }
92
93   virtual void CleanUpOnMainThread() OVERRIDE {
94     captive_portal_window_proxy_.reset();
95     base::MessageLoopForUI::current()->DeleteSoon(FROM_HERE, host_);
96     base::MessageLoopForUI::current()->RunUntilIdle();
97   }
98
99  private:
100   scoped_ptr<CaptivePortalWindowProxy> captive_portal_window_proxy_;
101   CaptivePortalWindowProxyStubDelegate delegate_;
102
103   LoginDisplayHost* host_;
104 };
105
106 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, Show) {
107   Show();
108 }
109
110 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, ShowClose) {
111   CheckState(false, 0);
112
113   Show();
114   CheckState(true, 0);
115
116   Close();
117   CheckState(false, 0);
118 }
119
120 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, OnRedirected) {
121   CheckState(false, 0);
122
123   ShowIfRedirected();
124   CheckState(false, 0);
125
126   OnRedirected();
127   CheckState(true, 1);
128
129   Close();
130   CheckState(false, 1);
131 }
132
133 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, OnOriginalURLLoaded) {
134   CheckState(false, 0);
135
136   ShowIfRedirected();
137   CheckState(false, 0);
138
139   OnRedirected();
140   CheckState(true, 1);
141
142   OnOriginalURLLoaded();
143   CheckState(false, 1);
144 }
145
146 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, MultipleCalls) {
147   CheckState(false, 0);
148
149   ShowIfRedirected();
150   CheckState(false, 0);
151
152   Show();
153   CheckState(true, 0);
154
155   Close();
156   CheckState(false, 0);
157
158   OnRedirected();
159   CheckState(false, 1);
160
161   OnOriginalURLLoaded();
162   CheckState(false, 1);
163
164   Show();
165   CheckState(true, 1);
166
167   OnRedirected();
168   CheckState(true, 2);
169
170   Close();
171   CheckState(false, 2);
172
173   OnOriginalURLLoaded();
174   CheckState(false, 2);
175 }
176
177 class CaptivePortalWindowCtorDtorTest : public LoginManagerTest {
178  public:
179   CaptivePortalWindowCtorDtorTest()
180       : LoginManagerTest(false) {}
181   virtual ~CaptivePortalWindowCtorDtorTest() {}
182
183   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
184     LoginManagerTest::SetUpInProcessBrowserTestFixture();
185
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);
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
212 IN_PROC_BROWSER_TEST_F(CaptivePortalWindowCtorDtorTest, PRE_OpenPortalDialog) {
213   StartupUtils::MarkOobeCompleted();
214 }
215
216 IN_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