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