network_portal_detector_impl_browsertest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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/run_loop.h" 8#include "chrome/browser/browser_process.h" 9#include "chrome/browser/captive_portal/testing_utils.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 "chromeos/chromeos_switches.h" 16#include "chromeos/dbus/dbus_thread_manager.h" 17#include "chromeos/dbus/shill_service_client.h" 18#include "content/public/test/test_utils.h" 19#include "dbus/object_path.h" 20#include "third_party/cros_system_api/dbus/service_constants.h" 21#include "ui/message_center/message_center.h" 22 23namespace chromeos { 24 25namespace { 26 27const char kTestUser[] = "test-user@gmail.com"; 28const char kWifi[] = "wifi"; 29 30void ErrorCallbackFunction(const std::string& error_name, 31 const std::string& error_message) { 32 CHECK(false) << "Shill Error: " << error_name << " : " << error_message; 33} 34 35void SetConnected(const std::string& service_path) { 36 DBusThreadManager::Get()->GetShillServiceClient()->Connect( 37 dbus::ObjectPath(service_path), 38 base::Bind(&base::DoNothing), 39 base::Bind(&ErrorCallbackFunction)); 40 base::RunLoop().RunUntilIdle(); 41} 42 43} // namespace 44 45class NetworkPortalDetectorImplBrowserTest 46 : public LoginManagerTest, 47 public captive_portal::CaptivePortalDetectorTestBase { 48 public: 49 NetworkPortalDetectorImplBrowserTest() 50 : LoginManagerTest(false), network_portal_detector_(NULL) {} 51 virtual ~NetworkPortalDetectorImplBrowserTest() {} 52 53 virtual void SetUpOnMainThread() OVERRIDE { 54 LoginManagerTest::SetUpOnMainThread(); 55 56 ShillServiceClient::TestInterface* service_test = 57 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 58 service_test->ClearServices(); 59 service_test->AddService(kWifi, 60 kWifi, 61 shill::kTypeEthernet, 62 shill::kStateIdle, 63 true /* add_to_visible */, 64 true /* add_to_watchlist */); 65 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 66 dbus::ObjectPath(kWifi), 67 shill::kStateProperty, 68 base::StringValue(shill::kStatePortal), 69 base::Bind(&base::DoNothing), 70 base::Bind(&ErrorCallbackFunction)); 71 72 network_portal_detector_ = new NetworkPortalDetectorImpl( 73 g_browser_process->system_request_context()); 74 NetworkPortalDetector::InitializeForTesting(network_portal_detector_); 75 network_portal_detector_->Enable(false /* start_detection */); 76 set_detector(network_portal_detector_->captive_portal_detector_.get()); 77 PortalDetectorStrategy::set_delay_till_next_attempt_for_testing( 78 base::TimeDelta()); 79 base::RunLoop().RunUntilIdle(); 80 } 81 82 void RestartDetection() { 83 network_portal_detector_->StopDetection(); 84 network_portal_detector_->StartDetection(); 85 base::RunLoop().RunUntilIdle(); 86 } 87 88 PortalDetectorStrategy* strategy() { 89 return network_portal_detector_->strategy_.get(); 90 } 91 92 private: 93 NetworkPortalDetectorImpl* network_portal_detector_; 94 95 DISALLOW_COPY_AND_ASSIGN(NetworkPortalDetectorImplBrowserTest); 96}; 97 98IN_PROC_BROWSER_TEST_F(NetworkPortalDetectorImplBrowserTest, 99 PRE_InSessionDetection) { 100 RegisterUser(kTestUser); 101 StartupUtils::MarkOobeCompleted(); 102 ASSERT_EQ(PortalDetectorStrategy::STRATEGY_ID_LOGIN_SCREEN, strategy()->Id()); 103} 104 105IN_PROC_BROWSER_TEST_F(NetworkPortalDetectorImplBrowserTest, 106 InSessionDetection) { 107 LoginUser(kTestUser); 108 content::RunAllPendingInMessageLoop(); 109 SetConnected(kWifi); 110 111 ASSERT_EQ(PortalDetectorStrategy::STRATEGY_ID_SESSION, strategy()->Id()); 112 113 // No notification until portal detection is completed. 114 ASSERT_FALSE(message_center::MessageCenter::Get()->HasNotification( 115 NetworkPortalNotificationController::kNotificationId)); 116 RestartDetection(); 117 CompleteURLFetch(net::OK, 200, NULL); 118 119 // Check that wifi is marked as behind the portal and that notification 120 // is displayed. 121 ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 122 NetworkPortalDetector::Get()->GetCaptivePortalState(kWifi).status); 123 ASSERT_TRUE(message_center::MessageCenter::Get()->HasNotification( 124 NetworkPortalNotificationController::kNotificationId)); 125} 126 127} // namespace chromeos 128