Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / net / network_portal_detector_impl_browsertest.cc
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/compiler_specific.h"
6 #include "base/macros.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chromeos/login/login_manager_test.h"
11 #include "chrome/browser/chromeos/login/startup_utils.h"
12 #include "chrome/browser/chromeos/net/network_portal_detector.h"
13 #include "chrome/browser/chromeos/net/network_portal_detector_impl.h"
14 #include "chrome/browser/chromeos/net/network_portal_detector_strategy.h"
15 #include "chrome/browser/chromeos/net/network_portal_detector_test_utils.h"
16 #include "chromeos/chromeos_switches.h"
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/shill_service_client.h"
19 #include "components/captive_portal/captive_portal_testing_utils.h"
20 #include "content/public/test/test_utils.h"
21 #include "dbus/object_path.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
23 #include "ui/message_center/message_center.h"
24 #include "ui/message_center/message_center_observer.h"
25
26 using base::MessageLoop;
27 using message_center::MessageCenter;
28 using message_center::MessageCenterObserver;
29
30 namespace chromeos {
31
32 namespace {
33
34 const char* kNotificationId =
35     NetworkPortalNotificationController::kNotificationId;
36 const char* kNotificationMetric =
37     NetworkPortalNotificationController::kNotificationMetric;
38 const char* kUserActionMetric =
39     NetworkPortalNotificationController::kUserActionMetric;
40
41 const char kTestUser[] = "test-user@gmail.com";
42 const char kWifi[] = "wifi";
43
44 void ErrorCallbackFunction(const std::string& error_name,
45                            const std::string& error_message) {
46   CHECK(false) << "Shill Error: " << error_name << " : " << error_message;
47 }
48
49 void SetConnected(const std::string& service_path) {
50   DBusThreadManager::Get()->GetShillServiceClient()->Connect(
51       dbus::ObjectPath(service_path),
52       base::Bind(&base::DoNothing),
53       base::Bind(&ErrorCallbackFunction));
54   base::RunLoop().RunUntilIdle();
55 }
56
57 class TestObserver : public MessageCenterObserver {
58  public:
59   TestObserver() : run_loop_(new base::RunLoop()) {
60     MessageCenter::Get()->AddObserver(this);
61   }
62
63   virtual ~TestObserver() {
64     MessageCenter::Get()->RemoveObserver(this);
65   }
66
67   void WaitAndReset() {
68     run_loop_->Run();
69     run_loop_.reset(new base::RunLoop());
70   }
71
72   virtual void OnNotificationDisplayed(
73       const std::string& notification_id,
74       const message_center::DisplaySource source)
75       OVERRIDE {
76     if (notification_id == kNotificationId)
77       MessageLoop::current()->PostTask(FROM_HERE, run_loop_->QuitClosure());
78   }
79
80   virtual void OnNotificationRemoved(const std::string& notification_id,
81                                      bool by_user) OVERRIDE {
82     if (notification_id == kNotificationId && by_user)
83       MessageLoop::current()->PostTask(FROM_HERE, run_loop_->QuitClosure());
84   }
85
86  private:
87   scoped_ptr<base::RunLoop> run_loop_;
88
89   DISALLOW_COPY_AND_ASSIGN(TestObserver);
90 };
91
92 }  // namespace
93
94 class NetworkPortalDetectorImplBrowserTest
95     : public LoginManagerTest,
96       public captive_portal::CaptivePortalDetectorTestBase {
97  public:
98   NetworkPortalDetectorImplBrowserTest()
99       : LoginManagerTest(false), network_portal_detector_(NULL) {}
100   virtual ~NetworkPortalDetectorImplBrowserTest() {}
101
102   virtual void SetUpOnMainThread() OVERRIDE {
103     LoginManagerTest::SetUpOnMainThread();
104
105     ShillServiceClient::TestInterface* service_test =
106         DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
107     service_test->ClearServices();
108     service_test->AddService(kWifi,
109                              kWifi,
110                              shill::kTypeEthernet,
111                              shill::kStateIdle,
112                              true /* add_to_visible */,
113                              true /* add_to_watchlist */);
114     DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
115         dbus::ObjectPath(kWifi),
116         shill::kStateProperty,
117         base::StringValue(shill::kStatePortal),
118         base::Bind(&base::DoNothing),
119         base::Bind(&ErrorCallbackFunction));
120
121     network_portal_detector_ = new NetworkPortalDetectorImpl(
122         g_browser_process->system_request_context());
123     NetworkPortalDetector::InitializeForTesting(network_portal_detector_);
124     network_portal_detector_->Enable(false /* start_detection */);
125     set_detector(network_portal_detector_->captive_portal_detector_.get());
126     PortalDetectorStrategy::set_delay_till_next_attempt_for_testing(
127         base::TimeDelta());
128     base::RunLoop().RunUntilIdle();
129   }
130
131   void RestartDetection() {
132     network_portal_detector_->StopDetection();
133     network_portal_detector_->StartDetection();
134     base::RunLoop().RunUntilIdle();
135   }
136
137   PortalDetectorStrategy* strategy() {
138     return network_portal_detector_->strategy_.get();
139   }
140
141   MessageCenter* message_center() { return MessageCenter::Get(); }
142
143  private:
144   NetworkPortalDetectorImpl* network_portal_detector_;
145
146   DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImplBrowserTest);
147 };
148
149 IN_PROC_BROWSER_TEST_F(NetworkPortalDetectorImplBrowserTest,
150                        PRE_InSessionDetection) {
151   RegisterUser(kTestUser);
152   StartupUtils::MarkOobeCompleted();
153   ASSERT_EQ(PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN, strategy()->Id());
154 }
155
156 IN_PROC_BROWSER_TEST_F(NetworkPortalDetectorImplBrowserTest,
157                        InSessionDetection) {
158   typedef NetworkPortalNotificationController Controller;
159
160   TestObserver observer;
161
162   EnumHistogramChecker ui_checker(
163       kNotificationMetric, Controller::NOTIFICATION_METRIC_COUNT, NULL);
164   EnumHistogramChecker action_checker(
165       kUserActionMetric, Controller::USER_ACTION_METRIC_COUNT, NULL);
166
167   LoginUser(kTestUser);
168   content::RunAllPendingInMessageLoop();
169
170   // User connects to wifi.
171   SetConnected(kWifi);
172
173   ASSERT_EQ(PortalDetectorStrategy::STRATEGY_ID_SESSION, strategy()->Id());
174
175   // No notification until portal detection is completed.
176   ASSERT_FALSE(message_center()->HasNotification(kNotificationId));
177   RestartDetection();
178   CompleteURLFetch(net::OK, 200, NULL);
179
180   // Check that wifi is marked as behind the portal and that notification
181   // is displayed.
182   ASSERT_TRUE(message_center()->HasNotification(kNotificationId));
183   ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL,
184             NetworkPortalDetector::Get()->GetCaptivePortalState(kWifi).status);
185
186   // Wait until notification is displayed.
187   observer.WaitAndReset();
188
189   ASSERT_TRUE(
190       ui_checker.Expect(Controller::NOTIFICATION_METRIC_DISPLAYED, 1)->Check());
191   ASSERT_TRUE(action_checker.Check());
192
193   // User explicitly closes the notification.
194   message_center()->RemoveNotification(kNotificationId, true);
195
196   // Wait until notification is closed.
197   observer.WaitAndReset();
198
199   ASSERT_TRUE(ui_checker.Check());
200   ASSERT_TRUE(
201       action_checker.Expect(Controller::USER_ACTION_METRIC_CLOSED, 1)->Check());
202 }
203
204 }  // namespace chromeos