Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / signin / chrome_signin_client_unittest.cc
blobc97a0a9095cee0a34087bbfc2207decce887c146
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"
7 #include "base/bind.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)
20 namespace {
22 class MockNetworkChangeNotifierNeverOffline :
23 public net::NetworkChangeNotifier {
24 public:
25 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() const
26 override {
27 return NetworkChangeNotifier::CONNECTION_3G;
31 class MockNetworkChangeNotifierOfflineUntilChange :
32 public net::NetworkChangeNotifier {
33 public:
34 MockNetworkChangeNotifierOfflineUntilChange() : online_(false) {}
35 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() const
36 override {
37 return online_ ? net::NetworkChangeNotifier::CONNECTION_3G
38 : net::NetworkChangeNotifier::CONNECTION_NONE;
41 void GoOnline() {
42 online_ = true;
43 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
44 net::NetworkChangeNotifier::CONNECTION_3G);
47 private:
48 bool online_;
51 class CallbackTester {
52 public:
53 CallbackTester() : called_(0) {}
55 void Increment();
56 bool WasCalledExactlyOnce();
58 private:
59 int called_;
62 void CallbackTester::Increment() {
63 called_++;
66 bool CallbackTester::WasCalledExactlyOnce() {
67 return called_ == 1;
70 } // namespace
72 class ChromeSigninClientTest : public testing::Test {
73 public:
74 ChromeSigninClientTest() {}
75 void SetUp() override;
77 Profile* profile() { return profile_.get(); }
78 SigninClient* signin_client() { return signin_client_; }
80 private:
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.
107 SetUp();
109 CallbackTester tester;
110 signin_client()->DelayNetworkCall(base::Bind(&CallbackTester::Increment,
111 base::Unretained(&tester)));
112 ASSERT_FALSE(tester.WasCalledExactlyOnce());
113 mock->GoOnline();
114 base::RunLoop().RunUntilIdle();
115 ASSERT_TRUE(tester.WasCalledExactlyOnce());
118 #endif