1 // Copyright (c) 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.
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/histogram_base.h"
13 #include "base/metrics/histogram_samples.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/run_loop.h"
16 #include "chrome/browser/captive_portal/captive_portal_detector.h"
17 #include "chrome/browser/captive_portal/testing_utils.h"
18 #include "chrome/browser/chromeos/net/network_portal_detector_impl.h"
19 #include "chrome/browser/chromeos/net/network_portal_detector_test_utils.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "chromeos/chromeos_switches.h"
22 #include "chromeos/dbus/dbus_thread_manager.h"
23 #include "chromeos/dbus/shill_device_client.h"
24 #include "chromeos/dbus/shill_service_client.h"
25 #include "chromeos/network/network_state.h"
26 #include "chromeos/network/network_state_handler.h"
27 #include "chromeos/network/shill_property_util.h"
28 #include "content/public/test/test_browser_thread_bundle.h"
29 #include "dbus/object_path.h"
30 #include "net/base/net_errors.h"
31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "third_party/cros_system_api/dbus/service_constants.h"
35 using testing::AnyNumber;
43 // Service paths for stub network devices.
44 const char kStubEthernet[] = "stub_ethernet";
45 const char kStubWireless1[] = "stub_wifi1";
46 const char kStubWireless2[] = "stub_wifi2";
47 const char kStubCellular[] = "stub_cellular";
49 void ErrorCallbackFunction(const std::string& error_name,
50 const std::string& error_message) {
51 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
54 class MockObserver : public NetworkPortalDetector::Observer {
56 virtual ~MockObserver() {}
58 MOCK_METHOD2(OnPortalDetectionCompleted,
59 void(const NetworkState* network,
60 const NetworkPortalDetector::CaptivePortalState& state));
65 class NetworkPortalDetectorImplTest
66 : public testing::Test,
67 public captive_portal::CaptivePortalDetectorTestBase {
69 virtual void SetUp() {
70 CommandLine* cl = CommandLine::ForCurrentProcess();
71 cl->AppendSwitch(switches::kDisableNetworkPortalNotification);
73 DBusThreadManager::InitializeWithStub();
74 base::StatisticsRecorder::Initialize();
75 SetupNetworkHandler();
77 profile_.reset(new TestingProfile());
78 network_portal_detector_.reset(
79 new NetworkPortalDetectorImpl(profile_->GetRequestContext()));
80 network_portal_detector_->Enable(false);
82 set_detector(network_portal_detector_->captive_portal_detector_.get());
84 // Prevents flakiness due to message loop delays.
85 set_time_ticks(base::TimeTicks::Now());
87 if (base::HistogramBase* histogram =
88 base::StatisticsRecorder::FindHistogram(
89 "CaptivePortal.OOBE.DetectionResult")) {
90 original_samples_.reset(histogram->SnapshotSamples().release());
94 virtual void TearDown() {
95 network_portal_detector_.reset();
97 NetworkHandler::Shutdown();
98 DBusThreadManager::Shutdown();
99 PortalDetectorStrategy::reset_fields_for_testing();
102 void CheckPortalState(NetworkPortalDetector::CaptivePortalStatus status,
104 const std::string& service_path) {
105 NetworkPortalDetector::CaptivePortalState state =
106 network_portal_detector()->GetCaptivePortalState(service_path);
107 ASSERT_EQ(status, state.status);
108 ASSERT_EQ(response_code, state.response_code);
111 void CheckRequestTimeoutAndCompleteAttempt(int expected_attempt_count,
112 int expected_request_timeout_sec,
115 ASSERT_TRUE(is_state_checking_for_portal());
116 ASSERT_EQ(expected_attempt_count, attempt_count());
117 ASSERT_EQ(base::TimeDelta::FromSeconds(expected_request_timeout_sec),
118 get_next_attempt_timeout());
119 CompleteURLFetch(net_error, status_code, NULL);
122 Profile* profile() { return profile_.get(); }
124 NetworkPortalDetectorImpl* network_portal_detector() {
125 return network_portal_detector_.get();
128 NetworkPortalDetectorImpl::State state() {
129 return network_portal_detector()->state();
132 bool start_detection_if_idle() {
133 return network_portal_detector()->StartDetectionIfIdle();
136 void enable_error_screen_strategy() {
137 network_portal_detector()->OnErrorScreenShow();
140 void disable_error_screen_strategy() {
141 network_portal_detector()->OnErrorScreenHide();
144 void stop_detection() { network_portal_detector()->StopDetection(); }
146 bool attempt_timeout_is_cancelled() {
147 return network_portal_detector()->AttemptTimeoutIsCancelledForTesting();
150 base::TimeDelta get_next_attempt_timeout() {
151 return network_portal_detector()->strategy_->GetNextAttemptTimeout();
154 void set_next_attempt_timeout(const base::TimeDelta& timeout) {
155 PortalDetectorStrategy::set_next_attempt_timeout_for_testing(timeout);
158 bool is_state_idle() {
159 return (NetworkPortalDetectorImpl::STATE_IDLE == state());
162 bool is_state_portal_detection_pending() {
163 return (NetworkPortalDetectorImpl::STATE_PORTAL_CHECK_PENDING == state());
166 bool is_state_checking_for_portal() {
167 return (NetworkPortalDetectorImpl::STATE_CHECKING_FOR_PORTAL == state());
170 const base::TimeDelta& next_attempt_delay() {
171 return network_portal_detector()->next_attempt_delay_for_testing();
174 int attempt_count() {
175 return network_portal_detector()->attempt_count_for_testing();
178 void set_attempt_count(int ac) {
179 network_portal_detector()->set_attempt_count_for_testing(ac);
182 void set_delay_till_next_attempt(const base::TimeDelta& delta) {
183 PortalDetectorStrategy::set_delay_till_next_attempt_for_testing(delta);
186 void set_time_ticks(const base::TimeTicks& time_ticks) {
187 network_portal_detector()->set_time_ticks_for_testing(time_ticks);
190 void SetBehindPortal(const std::string& service_path) {
191 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
192 dbus::ObjectPath(service_path),
193 shill::kStateProperty,
194 base::StringValue(shill::kStatePortal),
195 base::Bind(&base::DoNothing),
196 base::Bind(&ErrorCallbackFunction));
197 base::RunLoop().RunUntilIdle();
200 void SetNetworkDeviceEnabled(const std::string& type, bool enabled) {
201 NetworkHandler::Get()->network_state_handler()->SetTechnologyEnabled(
202 NetworkTypePattern::Primitive(type),
204 network_handler::ErrorCallback());
205 base::RunLoop().RunUntilIdle();
208 void SetConnected(const std::string& service_path) {
209 DBusThreadManager::Get()->GetShillServiceClient()->Connect(
210 dbus::ObjectPath(service_path),
211 base::Bind(&base::DoNothing),
212 base::Bind(&ErrorCallbackFunction));
213 base::RunLoop().RunUntilIdle();
216 void SetDisconnected(const std::string& service_path) {
217 DBusThreadManager::Get()->GetShillServiceClient()->Disconnect(
218 dbus::ObjectPath(service_path),
219 base::Bind(&*base::DoNothing),
220 base::Bind(&ErrorCallbackFunction));
221 base::RunLoop().RunUntilIdle();
224 scoped_ptr<EnumHistogramChecker> MakeResultHistogramChecker() {
225 return scoped_ptr<EnumHistogramChecker>(
226 new EnumHistogramChecker(
227 "CaptivePortal.OOBE.DetectionResult",
228 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_COUNT,
229 original_samples_.get()));
233 void SetupDefaultShillState() {
234 base::RunLoop().RunUntilIdle();
235 ShillServiceClient::TestInterface* service_test =
236 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
237 service_test->ClearServices();
238 const bool add_to_visible = true;
239 const bool add_to_watchlist = true;
240 service_test->AddService(kStubEthernet,
242 shill::kTypeEthernet,
246 service_test->AddService(kStubWireless1,
252 service_test->AddService(kStubWireless2,
258 service_test->AddService(kStubCellular,
260 shill::kTypeCellular,
266 void SetupNetworkHandler() {
267 SetupDefaultShillState();
268 NetworkHandler::Initialize();
271 content::TestBrowserThreadBundle thread_bundle_;
272 scoped_ptr<TestingProfile> profile_;
273 scoped_ptr<NetworkPortalDetectorImpl> network_portal_detector_;
274 scoped_ptr<base::HistogramSamples> original_samples_;
277 TEST_F(NetworkPortalDetectorImplTest, NoPortal) {
278 ASSERT_TRUE(is_state_idle());
280 SetConnected(kStubWireless1);
282 ASSERT_TRUE(is_state_checking_for_portal());
284 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, kStubWireless1);
286 CompleteURLFetch(net::OK, 204, NULL);
288 ASSERT_TRUE(is_state_idle());
290 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubWireless1);
292 MakeResultHistogramChecker()
293 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
297 TEST_F(NetworkPortalDetectorImplTest, Portal) {
298 ASSERT_TRUE(is_state_idle());
300 // Check HTTP 200 response code.
301 SetConnected(kStubWireless1);
302 ASSERT_TRUE(is_state_checking_for_portal());
304 CompleteURLFetch(net::OK, 200, NULL);
306 ASSERT_TRUE(is_state_idle());
308 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, kStubWireless1);
310 // Check HTTP 301 response code.
311 SetConnected(kStubWireless2);
312 ASSERT_TRUE(is_state_checking_for_portal());
314 CompleteURLFetch(net::OK, 301, NULL);
316 ASSERT_TRUE(is_state_idle());
318 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 301, kStubWireless2);
320 // Check HTTP 302 response code.
321 SetConnected(kStubEthernet);
322 ASSERT_TRUE(is_state_checking_for_portal());
324 CompleteURLFetch(net::OK, 302, NULL);
326 ASSERT_TRUE(is_state_idle());
328 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 302, kStubEthernet);
331 MakeResultHistogramChecker()
332 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 3)
336 TEST_F(NetworkPortalDetectorImplTest, Online2Offline) {
337 ASSERT_TRUE(is_state_idle());
339 MockObserver observer;
340 network_portal_detector()->AddObserver(&observer);
342 NetworkPortalDetector::CaptivePortalState offline_state;
343 offline_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE;
345 // WiFi is in online state.
347 // When transitioning to a connected state, the network will transition to
348 // connecting states which will set the default network to NULL. This may
349 // get triggered multiple times.
350 EXPECT_CALL(observer, OnPortalDetectionCompleted(_, offline_state))
353 // Expect a single transition to an online state.
354 NetworkPortalDetector::CaptivePortalState online_state;
355 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
356 online_state.response_code = 204;
357 EXPECT_CALL(observer, OnPortalDetectionCompleted(_, online_state)).Times(1);
359 SetConnected(kStubWireless1);
360 ASSERT_TRUE(is_state_checking_for_portal());
362 CompleteURLFetch(net::OK, 204, NULL);
363 ASSERT_TRUE(is_state_idle());
365 // Check that observer was notified about online state.
366 Mock::VerifyAndClearExpectations(&observer);
369 // WiFi is turned off.
371 EXPECT_CALL(observer, OnPortalDetectionCompleted(NULL, offline_state))
374 SetDisconnected(kStubWireless1);
375 ASSERT_TRUE(is_state_idle());
377 // Check that observer was notified about offline state.
378 Mock::VerifyAndClearExpectations(&observer);
381 network_portal_detector()->RemoveObserver(&observer);
384 MakeResultHistogramChecker()
385 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
389 TEST_F(NetworkPortalDetectorImplTest, TwoNetworks) {
390 ASSERT_TRUE(is_state_idle());
392 SetConnected(kStubWireless1);
393 ASSERT_TRUE(is_state_checking_for_portal());
395 // WiFi is in portal state.
396 CompleteURLFetch(net::OK, 200, NULL);
397 ASSERT_TRUE(is_state_idle());
399 SetConnected(kStubEthernet);
400 ASSERT_TRUE(is_state_checking_for_portal());
402 // ethernet is in online state.
403 CompleteURLFetch(net::OK, 204, NULL);
404 ASSERT_TRUE(is_state_idle());
406 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubEthernet);
408 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, kStubWireless1);
411 MakeResultHistogramChecker()
412 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
413 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 1)
417 TEST_F(NetworkPortalDetectorImplTest, NetworkChanged) {
418 ASSERT_TRUE(is_state_idle());
420 SetConnected(kStubWireless1);
422 // WiFi is in portal state.
423 fetcher()->set_response_code(200);
424 ASSERT_TRUE(is_state_checking_for_portal());
426 // Active network is changed during portal detection for WiFi.
427 SetConnected(kStubEthernet);
429 // Portal detection for WiFi is cancelled, portal detection for
430 // ethernet is initiated.
431 ASSERT_TRUE(is_state_checking_for_portal());
433 // ethernet is in online state.
434 CompleteURLFetch(net::OK, 204, NULL);
435 ASSERT_TRUE(is_state_idle());
437 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubEthernet);
439 // As active network was changed during portal detection for wifi
440 // network, it's state must be unknown.
442 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, kStubWireless1);
445 MakeResultHistogramChecker()
446 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
450 TEST_F(NetworkPortalDetectorImplTest, NetworkStateNotChanged) {
451 ASSERT_TRUE(is_state_idle());
453 SetConnected(kStubWireless1);
454 ASSERT_TRUE(is_state_checking_for_portal());
456 CompleteURLFetch(net::OK, 204, NULL);
458 ASSERT_TRUE(is_state_idle());
460 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubWireless1);
462 SetConnected(kStubWireless1);
463 ASSERT_TRUE(is_state_idle());
466 MakeResultHistogramChecker()
467 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
471 TEST_F(NetworkPortalDetectorImplTest, NetworkStateChanged) {
472 // Test for Portal -> Online -> Portal network state transitions.
473 ASSERT_TRUE(is_state_idle());
475 SetBehindPortal(kStubWireless1);
476 ASSERT_TRUE(is_state_checking_for_portal());
478 CompleteURLFetch(net::OK, 200, NULL);
480 ASSERT_TRUE(is_state_idle());
482 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, kStubWireless1);
484 SetConnected(kStubWireless1);
485 ASSERT_TRUE(is_state_checking_for_portal());
487 CompleteURLFetch(net::OK, 204, NULL);
489 ASSERT_TRUE(is_state_idle());
491 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubWireless1);
493 SetBehindPortal(kStubWireless1);
494 ASSERT_TRUE(is_state_checking_for_portal());
496 CompleteURLFetch(net::OK, 200, NULL);
498 ASSERT_TRUE(is_state_idle());
500 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, kStubWireless1);
503 MakeResultHistogramChecker()
504 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
505 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 2)
509 TEST_F(NetworkPortalDetectorImplTest, PortalDetectionTimeout) {
510 ASSERT_TRUE(is_state_idle());
512 // For instantaneous timeout.
513 set_next_attempt_timeout(base::TimeDelta::FromSeconds(0));
515 ASSERT_TRUE(is_state_idle());
516 ASSERT_EQ(0, attempt_count());
518 SetConnected(kStubWireless1);
519 base::RunLoop().RunUntilIdle();
521 // First portal detection timeouts, next portal detection is
523 ASSERT_TRUE(is_state_portal_detection_pending());
524 ASSERT_EQ(1, attempt_count());
525 ASSERT_EQ(base::TimeDelta::FromSeconds(3), next_attempt_delay());
527 ASSERT_TRUE(MakeResultHistogramChecker()->Check());
530 TEST_F(NetworkPortalDetectorImplTest, PortalDetectionRetryAfter) {
531 ASSERT_TRUE(is_state_idle());
533 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n";
535 ASSERT_TRUE(is_state_idle());
536 ASSERT_EQ(0, attempt_count());
538 SetConnected(kStubWireless1);
539 ASSERT_TRUE(is_state_checking_for_portal());
540 CompleteURLFetch(net::OK, 503, retry_after);
542 // First portal detection completed, next portal detection is
543 // scheduled after 101 seconds.
544 ASSERT_TRUE(is_state_portal_detection_pending());
545 ASSERT_EQ(1, attempt_count());
546 ASSERT_EQ(base::TimeDelta::FromSeconds(101), next_attempt_delay());
548 ASSERT_TRUE(MakeResultHistogramChecker()->Check());
551 TEST_F(NetworkPortalDetectorImplTest, PortalDetectorRetryAfterIsSmall) {
552 ASSERT_TRUE(is_state_idle());
554 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 1\n\n";
556 ASSERT_TRUE(is_state_idle());
557 ASSERT_EQ(0, attempt_count());
559 SetConnected(kStubWireless1);
560 CompleteURLFetch(net::OK, 503, retry_after);
562 // First portal detection completed, next portal detection is
563 // scheduled after 3 seconds (due to minimum time between detection
565 ASSERT_TRUE(is_state_portal_detection_pending());
566 ASSERT_EQ(1, attempt_count());
567 ASSERT_EQ(base::TimeDelta::FromSeconds(3), next_attempt_delay());
569 ASSERT_TRUE(MakeResultHistogramChecker()->Check());
572 TEST_F(NetworkPortalDetectorImplTest, FirstAttemptFailed) {
573 ASSERT_TRUE(is_state_idle());
575 set_delay_till_next_attempt(base::TimeDelta());
576 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 0\n\n";
578 ASSERT_TRUE(is_state_idle());
579 ASSERT_EQ(0, attempt_count());
581 SetConnected(kStubWireless1);
583 CompleteURLFetch(net::OK, 503, retry_after);
584 ASSERT_TRUE(is_state_portal_detection_pending());
585 ASSERT_EQ(1, attempt_count());
586 ASSERT_EQ(base::TimeDelta::FromSeconds(0), next_attempt_delay());
588 // To run CaptivePortalDetector::DetectCaptivePortal().
589 base::RunLoop().RunUntilIdle();
591 CompleteURLFetch(net::OK, 204, NULL);
592 ASSERT_TRUE(is_state_idle());
593 ASSERT_EQ(2, attempt_count());
595 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubWireless1);
598 MakeResultHistogramChecker()
599 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
603 TEST_F(NetworkPortalDetectorImplTest, AllAttemptsFailed) {
604 ASSERT_TRUE(is_state_idle());
606 set_delay_till_next_attempt(base::TimeDelta());
607 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 0\n\n";
609 ASSERT_TRUE(is_state_idle());
610 ASSERT_EQ(0, attempt_count());
612 SetConnected(kStubWireless1);
614 CompleteURLFetch(net::OK, 503, retry_after);
615 ASSERT_TRUE(is_state_portal_detection_pending());
616 ASSERT_EQ(1, attempt_count());
617 ASSERT_EQ(base::TimeDelta::FromSeconds(0), next_attempt_delay());
619 // To run CaptivePortalDetector::DetectCaptivePortal().
620 base::RunLoop().RunUntilIdle();
622 CompleteURLFetch(net::OK, 503, retry_after);
623 ASSERT_TRUE(is_state_portal_detection_pending());
624 ASSERT_EQ(2, attempt_count());
625 ASSERT_EQ(base::TimeDelta::FromSeconds(0), next_attempt_delay());
627 // To run CaptivePortalDetector::DetectCaptivePortal().
628 base::RunLoop().RunUntilIdle();
630 CompleteURLFetch(net::OK, 503, retry_after);
631 ASSERT_TRUE(is_state_idle());
632 ASSERT_EQ(3, attempt_count());
633 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE,
638 MakeResultHistogramChecker()
639 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE, 1)
643 TEST_F(NetworkPortalDetectorImplTest, ProxyAuthRequired) {
644 ASSERT_TRUE(is_state_idle());
645 set_delay_till_next_attempt(base::TimeDelta());
647 SetConnected(kStubWireless1);
648 CompleteURLFetch(net::OK, 407, NULL);
649 ASSERT_EQ(1, attempt_count());
650 ASSERT_TRUE(is_state_idle());
652 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED,
656 ASSERT_TRUE(MakeResultHistogramChecker()
657 ->Expect(NetworkPortalDetector::
658 CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED,
663 TEST_F(NetworkPortalDetectorImplTest, NoResponseButBehindPortal) {
664 ASSERT_TRUE(is_state_idle());
665 set_delay_till_next_attempt(base::TimeDelta());
667 SetBehindPortal(kStubWireless1);
668 ASSERT_TRUE(is_state_checking_for_portal());
671 net::ERR_CONNECTION_CLOSED, net::URLFetcher::RESPONSE_CODE_INVALID, NULL);
672 ASSERT_EQ(1, attempt_count());
673 ASSERT_TRUE(is_state_portal_detection_pending());
675 // To run CaptivePortalDetector::DetectCaptivePortal().
676 base::RunLoop().RunUntilIdle();
679 net::ERR_CONNECTION_CLOSED, net::URLFetcher::RESPONSE_CODE_INVALID, NULL);
680 ASSERT_EQ(2, attempt_count());
681 ASSERT_TRUE(is_state_portal_detection_pending());
683 // To run CaptivePortalDetector::DetectCaptivePortal().
684 base::RunLoop().RunUntilIdle();
687 net::ERR_CONNECTION_CLOSED, net::URLFetcher::RESPONSE_CODE_INVALID, NULL);
688 ASSERT_EQ(3, attempt_count());
689 ASSERT_TRUE(is_state_idle());
691 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL,
692 net::URLFetcher::RESPONSE_CODE_INVALID,
696 MakeResultHistogramChecker()
697 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 1)
701 TEST_F(NetworkPortalDetectorImplTest,
702 DisableErrorScreenStrategyWhilePendingRequest) {
703 ASSERT_TRUE(is_state_idle());
704 set_attempt_count(3);
705 enable_error_screen_strategy();
706 ASSERT_TRUE(is_state_portal_detection_pending());
707 disable_error_screen_strategy();
709 // To run CaptivePortalDetector::DetectCaptivePortal().
710 base::MessageLoop::current()->RunUntilIdle();
712 ASSERT_TRUE(MakeResultHistogramChecker()->Check());
715 TEST_F(NetworkPortalDetectorImplTest, ErrorScreenStrategyForOnlineNetwork) {
716 ASSERT_TRUE(is_state_idle());
717 set_delay_till_next_attempt(base::TimeDelta());
719 SetConnected(kStubWireless1);
720 enable_error_screen_strategy();
721 // To run CaptivePortalDetector::DetectCaptivePortal().
722 base::RunLoop().RunUntilIdle();
723 CompleteURLFetch(net::OK, 204, NULL);
725 ASSERT_TRUE(is_state_portal_detection_pending());
727 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubWireless1);
729 // To run CaptivePortalDetector::DetectCaptivePortal().
730 base::RunLoop().RunUntilIdle();
732 CompleteURLFetch(net::OK, 204, NULL);
734 ASSERT_TRUE(is_state_portal_detection_pending());
736 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubWireless1);
738 // To run CaptivePortalDetector::DetectCaptivePortal().
739 base::RunLoop().RunUntilIdle();
741 disable_error_screen_strategy();
743 ASSERT_TRUE(is_state_portal_detection_pending());
744 // To run CaptivePortalDetector::DetectCaptivePortal().
745 base::RunLoop().RunUntilIdle();
746 ASSERT_TRUE(is_state_checking_for_portal());
747 CompleteURLFetch(net::OK, 204, NULL);
750 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubWireless1);
753 MakeResultHistogramChecker()
754 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
758 TEST_F(NetworkPortalDetectorImplTest, ErrorScreenStrategyForPortalNetwork) {
759 ASSERT_TRUE(is_state_idle());
760 set_delay_till_next_attempt(base::TimeDelta());
762 enable_error_screen_strategy();
763 SetConnected(kStubWireless1);
766 net::ERR_CONNECTION_CLOSED, net::URLFetcher::RESPONSE_CODE_INVALID, NULL);
767 ASSERT_EQ(1, attempt_count());
768 ASSERT_TRUE(is_state_portal_detection_pending());
770 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, kStubWireless1);
772 // To run CaptivePortalDetector::DetectCaptivePortal().
773 base::RunLoop().RunUntilIdle();
776 net::ERR_CONNECTION_CLOSED, net::URLFetcher::RESPONSE_CODE_INVALID, NULL);
777 ASSERT_EQ(2, attempt_count());
778 ASSERT_TRUE(is_state_portal_detection_pending());
780 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, kStubWireless1);
782 // To run CaptivePortalDetector::DetectCaptivePortal().
783 base::RunLoop().RunUntilIdle();
785 CompleteURLFetch(net::OK, 200, NULL);
786 ASSERT_EQ(3, attempt_count());
787 ASSERT_TRUE(is_state_portal_detection_pending());
789 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, kStubWireless1);
791 // To run CaptivePortalDetector::DetectCaptivePortal().
792 base::RunLoop().RunUntilIdle();
794 disable_error_screen_strategy();
796 ASSERT_TRUE(is_state_portal_detection_pending());
798 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, kStubWireless1);
801 MakeResultHistogramChecker()
802 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 1)
806 TEST_F(NetworkPortalDetectorImplTest, DetectionTimeoutIsCancelled) {
807 ASSERT_TRUE(is_state_idle());
808 set_delay_till_next_attempt(base::TimeDelta());
810 SetConnected(kStubWireless1);
811 ASSERT_TRUE(is_state_checking_for_portal());
813 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, kStubWireless1);
817 ASSERT_TRUE(is_state_idle());
818 ASSERT_TRUE(attempt_timeout_is_cancelled());
820 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, kStubWireless1);
822 ASSERT_TRUE(MakeResultHistogramChecker()->Check());
825 TEST_F(NetworkPortalDetectorImplTest, TestDetectionRestart) {
826 ASSERT_TRUE(is_state_idle());
827 set_delay_till_next_attempt(base::TimeDelta());
829 // First portal detection attempts determines ONLINE state.
830 SetConnected(kStubWireless1);
831 ASSERT_TRUE(is_state_checking_for_portal());
832 ASSERT_FALSE(start_detection_if_idle());
834 CompleteURLFetch(net::OK, 204, NULL);
837 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, kStubWireless1);
838 ASSERT_TRUE(is_state_idle());
840 // First portal detection attempts determines PORTAL state.
841 ASSERT_TRUE(start_detection_if_idle());
842 ASSERT_TRUE(is_state_portal_detection_pending());
843 ASSERT_FALSE(start_detection_if_idle());
845 base::RunLoop().RunUntilIdle();
846 ASSERT_TRUE(is_state_checking_for_portal());
847 CompleteURLFetch(net::OK, 200, NULL);
850 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, kStubWireless1);
851 ASSERT_TRUE(is_state_idle());
854 MakeResultHistogramChecker()
855 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
856 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 1)
860 TEST_F(NetworkPortalDetectorImplTest, RequestTimeouts) {
861 ASSERT_TRUE(is_state_idle());
862 set_delay_till_next_attempt(base::TimeDelta());
864 SetNetworkDeviceEnabled(shill::kTypeWifi, false);
865 SetConnected(kStubCellular);
867 // First portal detection attempt for cellular1 uses 5sec timeout.
868 CheckRequestTimeoutAndCompleteAttempt(
869 0, 5, net::ERR_CONNECTION_CLOSED, net::URLFetcher::RESPONSE_CODE_INVALID);
871 // Second portal detection attempt for cellular1 uses 10sec timeout.
872 ASSERT_TRUE(is_state_portal_detection_pending());
873 base::RunLoop().RunUntilIdle();
874 CheckRequestTimeoutAndCompleteAttempt(1,
876 net::ERR_CONNECTION_CLOSED,
877 net::URLFetcher::RESPONSE_CODE_INVALID);
879 // Third portal detection attempt for cellular1 uses 15sec timeout.
880 ASSERT_TRUE(is_state_portal_detection_pending());
881 base::RunLoop().RunUntilIdle();
882 CheckRequestTimeoutAndCompleteAttempt(2,
884 net::ERR_CONNECTION_CLOSED,
885 net::URLFetcher::RESPONSE_CODE_INVALID);
887 ASSERT_TRUE(is_state_idle());
889 // Check that in lazy detection for cellular1 15sec timeout is used.
890 enable_error_screen_strategy();
891 ASSERT_TRUE(is_state_portal_detection_pending());
892 base::RunLoop().RunUntilIdle();
893 CheckRequestTimeoutAndCompleteAttempt(0,
895 net::ERR_CONNECTION_CLOSED,
896 net::URLFetcher::RESPONSE_CODE_INVALID);
897 disable_error_screen_strategy();
898 ASSERT_TRUE(is_state_portal_detection_pending());
900 SetNetworkDeviceEnabled(shill::kTypeWifi, true);
901 SetConnected(kStubWireless1);
903 // First portal detection attempt for wifi1 uses 5sec timeout.
904 CheckRequestTimeoutAndCompleteAttempt(
905 0, 5, net::ERR_CONNECTION_CLOSED, net::URLFetcher::RESPONSE_CODE_INVALID);
907 // Second portal detection attempt for wifi1 also uses 5sec timeout.
908 ASSERT_TRUE(is_state_portal_detection_pending());
909 base::RunLoop().RunUntilIdle();
910 CheckRequestTimeoutAndCompleteAttempt(1, 10, net::OK, 204);
911 ASSERT_TRUE(is_state_idle());
913 // Check that in error screen strategy detection for wifi1 15sec
915 enable_error_screen_strategy();
916 ASSERT_TRUE(is_state_portal_detection_pending());
917 base::RunLoop().RunUntilIdle();
918 CheckRequestTimeoutAndCompleteAttempt(0, 15, net::OK, 204);
919 disable_error_screen_strategy();
920 ASSERT_TRUE(is_state_portal_detection_pending());
923 MakeResultHistogramChecker()
924 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE, 1)
925 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
929 TEST_F(NetworkPortalDetectorImplTest, StartDetectionIfIdle) {
930 ASSERT_TRUE(is_state_idle());
931 set_delay_till_next_attempt(base::TimeDelta());
932 SetConnected(kStubWireless1);
934 // First portal detection attempt for wifi1 uses 5sec timeout.
935 CheckRequestTimeoutAndCompleteAttempt(
936 0, 5, net::ERR_CONNECTION_CLOSED, net::URLFetcher::RESPONSE_CODE_INVALID);
937 ASSERT_TRUE(is_state_portal_detection_pending());
938 base::RunLoop().RunUntilIdle();
940 // Second portal detection attempt for wifi1 uses 10sec timeout.
941 CheckRequestTimeoutAndCompleteAttempt(1,
943 net::ERR_CONNECTION_CLOSED,
944 net::URLFetcher::RESPONSE_CODE_INVALID);
945 ASSERT_TRUE(is_state_portal_detection_pending());
946 base::RunLoop().RunUntilIdle();
948 // Second portal detection attempt for wifi1 uses 15sec timeout.
949 CheckRequestTimeoutAndCompleteAttempt(2,
951 net::ERR_CONNECTION_CLOSED,
952 net::URLFetcher::RESPONSE_CODE_INVALID);
953 ASSERT_TRUE(is_state_idle());
954 start_detection_if_idle();
956 ASSERT_TRUE(is_state_portal_detection_pending());
958 // First portal detection attempt for wifi1 uses 5sec timeout.
959 base::RunLoop().RunUntilIdle();
960 CheckRequestTimeoutAndCompleteAttempt(0, 5, net::OK, 204);
961 ASSERT_TRUE(is_state_idle());
964 MakeResultHistogramChecker()
965 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE, 1)
966 ->Expect(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 1)
970 } // namespace chromeos