OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 "chrome/browser/content_settings/cookie_settings.h" | |
6 #include "chrome/browser/prefs/pref_service.h" | |
7 #include "chrome/browser/profiles/profile.h" | |
8 #include "chrome/browser/profiles/profile_manager.h" | |
9 #include "chrome/browser/signin/signin_manager_factory.h" | |
10 #include "chrome/browser/signin/signin_manager_fake.h" | |
11 #include "chrome/browser/ui/sync/one_click_signin_helper.h" | |
12 #include "chrome/common/pref_names.h" | |
13 #include "chrome/test/base/testing_profile.h" | |
14 #include "content/public/browser/browser_context.h" | |
15 #include "content/public/browser/navigation_controller.h" | |
16 #include "content/public/browser/site_instance.h" | |
17 #include "content/public/browser/web_contents.h" | |
18 #include "content/test/test_browser_context.h" | |
19 #include "content/test/test_browser_thread.h" | |
20 #include "content/test/web_contents_tester.h" | |
21 #include "testing/gmock/include/gmock/gmock-actions.h" | |
22 #include "testing/gmock/include/gmock/gmock.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 namespace { | |
26 | |
27 class OneClickSigninHelperTest : public testing::Test { | |
28 public: | |
tfarina
2012/05/28 16:06:10
nit: indent one space.
Roger Tawa OOO till Jul 10th
2012/05/28 17:10:00
Done.
| |
29 OneClickSigninHelperTest() : signin_manager_(NULL) { | |
30 } | |
31 | |
32 virtual void TearDown() OVERRIDE; | |
33 | |
34 protected: | |
35 // Marks the current thread as the UI thread, so that calls that assume | |
36 // they are called on that thread work. | |
37 void MarkCurrentThreadAsUIThread(); | |
38 | |
39 // Creates a mock WebContents for tests. If |use_incognito| is true then | |
40 // a WebContents for an incognito profile is created. | |
41 content::WebContents* CreateMockWebContents(bool use_incognito); | |
42 | |
43 void EnableOneClick(bool enable); | |
44 | |
45 void AllowSigninCookies(bool enable); | |
46 | |
47 // Marks the profile as connected to the given account. If |username| is the | |
48 // empty string, the profile is not connected. | |
49 void ConnectProfileToAccount(const std::string& username); | |
50 | |
51 private: | |
52 // Members to fake that we are on the UI thread. | |
53 MessageLoop message_loop_; | |
54 scoped_ptr<content::TestBrowserThread> ui_thread_; | |
55 | |
56 // Mock objects used during tests. The objects need to be torn down in the | |
57 // correct order, see TearDown(). | |
58 scoped_ptr<content::WebContents> web_contents_; | |
59 scoped_ptr<TestingProfile> profile_; | |
60 SigninManager* signin_manager_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); | |
63 }; | |
64 | |
65 void OneClickSigninHelperTest::TearDown() { | |
66 // Destroy things in proper order. | |
67 web_contents_.reset(); | |
68 profile_.reset(); | |
69 ui_thread_.reset(); | |
70 } | |
71 | |
72 void OneClickSigninHelperTest::MarkCurrentThreadAsUIThread() { | |
73 ui_thread_.reset(new content::TestBrowserThread( | |
74 content::BrowserThread::UI, &message_loop_)); | |
75 } | |
76 | |
77 content::WebContents* OneClickSigninHelperTest::CreateMockWebContents( | |
78 bool use_incognito) { | |
79 EXPECT_TRUE(web_contents_.get() == NULL); | |
80 | |
81 profile_.reset(new TestingProfile()); | |
82 signin_manager_ = static_cast<SigninManager*>( | |
83 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | |
84 profile_.get(), FakeSigninManager::Build)); | |
85 | |
86 profile_->set_incognito(use_incognito); | |
87 web_contents_.reset(content::WebContentsTester::CreateTestWebContents( | |
88 profile_.get(), NULL)); | |
89 return web_contents_.get(); | |
90 } | |
91 | |
92 void OneClickSigninHelperTest::EnableOneClick(bool enable) { | |
93 PrefService* pref_service = profile_->GetPrefs(); | |
94 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); | |
95 } | |
96 | |
97 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) { | |
98 CookieSettings* cookie_settings = | |
99 CookieSettings::Factory::GetForProfile(profile_.get()); | |
100 cookie_settings->SetDefaultCookieSetting( | |
101 enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); | |
102 } | |
103 | |
104 void OneClickSigninHelperTest::ConnectProfileToAccount( | |
105 const std::string& username) { | |
106 signin_manager_->StartSignIn(username, std::string(), std::string(), | |
107 std::string()); | |
108 } | |
109 | |
110 } // namespace | |
111 | |
112 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) { | |
113 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, true)); | |
114 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, false)); | |
115 } | |
116 | |
117 TEST_F(OneClickSigninHelperTest, CanOffer) { | |
118 MarkCurrentThreadAsUIThread(); | |
119 content::WebContents* web_contents = CreateMockWebContents(false); | |
120 | |
121 EnableOneClick(true); | |
122 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, true)); | |
123 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | |
124 | |
125 EnableOneClick(false); | |
126 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | |
127 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | |
128 } | |
129 | |
130 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) { | |
131 MarkCurrentThreadAsUIThread(); | |
132 content::WebContents* web_contents = CreateMockWebContents(false); | |
133 ConnectProfileToAccount("foo@gmail.com"); | |
134 | |
135 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | |
136 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | |
137 } | |
138 | |
139 TEST_F(OneClickSigninHelperTest, CanOfferIncognito) { | |
140 MarkCurrentThreadAsUIThread(); | |
141 content::WebContents* web_contents = CreateMockWebContents(true); | |
142 | |
143 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | |
144 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | |
145 } | |
146 | |
147 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) { | |
148 MarkCurrentThreadAsUIThread(); | |
149 content::WebContents* web_contents = CreateMockWebContents(true); | |
150 AllowSigninCookies(false); | |
151 | |
152 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | |
153 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | |
154 } | |
OLD | NEW |