1 // Copyright 2015 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.
5 #include "chrome/browser/signin/chrome_signin_client.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/signin/chrome_signin_client_factory.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "net/base/network_change_notifier.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 // ChromeOS has its own network delay logic.
18 #if !defined(OS_CHROMEOS)
22 class MockNetworkChangeNotifierNeverOffline
:
23 public net::NetworkChangeNotifier
{
25 net::NetworkChangeNotifier::ConnectionType
GetCurrentConnectionType() const
27 return NetworkChangeNotifier::CONNECTION_3G
;
31 class MockNetworkChangeNotifierOfflineUntilChange
:
32 public net::NetworkChangeNotifier
{
34 MockNetworkChangeNotifierOfflineUntilChange() : online_(false) {}
35 net::NetworkChangeNotifier::ConnectionType
GetCurrentConnectionType() const
37 return online_
? net::NetworkChangeNotifier::CONNECTION_3G
38 : net::NetworkChangeNotifier::CONNECTION_NONE
;
43 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
44 net::NetworkChangeNotifier::CONNECTION_3G
);
51 class CallbackTester
{
53 CallbackTester() : called_(0) {}
56 bool WasCalledExactlyOnce();
62 void CallbackTester::Increment() {
66 bool CallbackTester::WasCalledExactlyOnce() {
72 class ChromeSigninClientTest
: public testing::Test
{
74 ChromeSigninClientTest() {}
75 void SetUp() override
;
77 Profile
* profile() { return profile_
.get(); }
78 SigninClient
* signin_client() { return signin_client_
; }
81 scoped_ptr
<Profile
> profile_
;
82 SigninClient
* signin_client_
;
83 content::TestBrowserThreadBundle thread_bundle_
;
86 void ChromeSigninClientTest::SetUp() {
87 // Create a signed-in profile.
88 TestingProfile::Builder builder
;
89 profile_
= builder
.Build();
91 signin_client_
= ChromeSigninClientFactory::GetForProfile(profile());
94 TEST_F(ChromeSigninClientTest
, DelayNetworkCallRunsImmediatelyWithNetwork
) {
95 scoped_ptr
<net::NetworkChangeNotifier
>
96 mock(new MockNetworkChangeNotifierNeverOffline
);
97 CallbackTester tester
;
98 signin_client()->DelayNetworkCall(base::Bind(&CallbackTester::Increment
,
99 base::Unretained(&tester
)));
100 ASSERT_TRUE(tester
.WasCalledExactlyOnce());
103 TEST_F(ChromeSigninClientTest
, DelayNetworkCallRunsAfterNetworkFound
) {
104 scoped_ptr
<MockNetworkChangeNotifierOfflineUntilChange
>
105 mock(new MockNetworkChangeNotifierOfflineUntilChange());
106 // Install a SigninClient after the NetworkChangeNotifier's created.
109 CallbackTester tester
;
110 signin_client()->DelayNetworkCall(base::Bind(&CallbackTester::Increment
,
111 base::Unretained(&tester
)));
112 ASSERT_FALSE(tester
.WasCalledExactlyOnce());
114 base::RunLoop().RunUntilIdle();
115 ASSERT_TRUE(tester
.WasCalledExactlyOnce());