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 | 29 // Marks the current thread as the UI thread, so that calls that assume |
36 // they are called on that thread work. | 30 // they are called on that thread work. |
37 void MarkCurrentThreadAsUIThread(); | 31 void MarkCurrentThreadAsUIThread(); |
38 | 32 |
39 // Creates a mock WebContents for tests. If |use_incognito| is true then | 33 // Creates a mock WebContents for tests. If |use_incognito| is true then |
40 // a WebContents for an incognito profile is created. | 34 // a WebContents for an incognito profile is created. If |username| is |
41 content::WebContents* CreateMockWebContents(bool use_incognito); | 35 // is not empty, the profile of the mock WebContents will be connected to |
36 // the given account. | |
37 content::WebContents* CreateMockWebContents(bool use_incognito, | |
38 const std::string& username); | |
42 | 39 |
43 void EnableOneClick(bool enable); | 40 void EnableOneClick(bool enable); |
44 | 41 |
45 void AllowSigninCookies(bool enable); | 42 void AllowSigninCookies(bool enable); |
46 | 43 |
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: | 44 private: |
52 // Members to fake that we are on the UI thread. | 45 // Members to fake that we are on the UI thread. |
53 scoped_ptr<content::TestBrowserThread> ui_thread_; | 46 scoped_ptr<content::TestBrowserThread> ui_thread_; |
54 | 47 |
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); | 48 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); |
62 }; | 49 }; |
63 | 50 |
64 void OneClickSigninHelperTest::TearDown() { | 51 void OneClickSigninHelperTest::SetUp() { |
65 // Destroy things in proper order. | 52 // Don't call base class so that default browser context and test WebContents |
66 web_contents_.reset(); | 53 // are not created now. They will be created in CreateMockWebContents() |
67 profile_.reset(); | 54 // as needed. |
68 ui_thread_.reset(); | |
69 MessageLoop::current()->RunAllPending(); | |
70 ChromeRenderViewHostTestHarness::TearDown(); | |
71 } | 55 } |
72 | 56 |
73 void OneClickSigninHelperTest::MarkCurrentThreadAsUIThread() { | 57 void OneClickSigninHelperTest::MarkCurrentThreadAsUIThread() { |
74 ui_thread_.reset(new content::TestBrowserThread( | 58 ui_thread_.reset(new content::TestBrowserThread( |
75 content::BrowserThread::UI, &message_loop_)); | 59 content::BrowserThread::UI, &message_loop_)); |
76 } | 60 } |
77 | 61 |
78 content::WebContents* OneClickSigninHelperTest::CreateMockWebContents( | 62 content::WebContents* OneClickSigninHelperTest::CreateMockWebContents( |
79 bool use_incognito) { | 63 bool use_incognito, |
80 EXPECT_TRUE(web_contents_.get() == NULL); | 64 const std::string& username) { |
65 TestingProfile* testing_profile = new TestingProfile(); | |
66 browser_context_.reset(testing_profile); | |
81 | 67 |
82 profile_.reset(new TestingProfile()); | 68 testing_profile->set_incognito(use_incognito); |
83 signin_manager_ = static_cast<SigninManager*>( | 69 SigninManager* signin_manager = static_cast<SigninManager*>( |
84 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 70 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
85 profile_.get(), FakeSigninManager::Build)); | 71 testing_profile, FakeSigninManager::Build)); |
72 if (!username.empty()) { | |
73 signin_manager->StartSignIn(username, std::string(), std::string(), | |
74 std::string()); | |
75 } | |
86 | 76 |
87 profile_->set_incognito(use_incognito); | 77 return CreateTestWebContents(); |
88 web_contents_.reset(content::WebContentsTester::CreateTestWebContents( | |
89 profile_.get(), NULL)); | |
90 return web_contents_.get(); | |
91 } | 78 } |
92 | 79 |
93 void OneClickSigninHelperTest::EnableOneClick(bool enable) { | 80 void OneClickSigninHelperTest::EnableOneClick(bool enable) { |
94 PrefService* pref_service = profile_->GetPrefs(); | 81 PrefService* pref_service = Profile::FromBrowserContext( |
82 browser_context_.get())->GetPrefs(); | |
95 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); | 83 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); |
96 } | 84 } |
97 | 85 |
98 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) { | 86 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) { |
99 CookieSettings* cookie_settings = | 87 CookieSettings* cookie_settings = |
100 CookieSettings::Factory::GetForProfile(profile_.get()); | 88 CookieSettings::Factory::GetForProfile( |
89 Profile::FromBrowserContext(browser_context_.get())); | |
101 cookie_settings->SetDefaultCookieSetting( | 90 cookie_settings->SetDefaultCookieSetting( |
102 enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); | 91 enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); |
103 } | 92 } |
104 | 93 |
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 | 94 } // namespace |
112 | 95 |
113 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) { | 96 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) { |
97 MarkCurrentThreadAsUIThread(); | |
James Hawkins
2012/06/05 21:14:23
This method is now called in every test. The meth
Roger Tawa OOO till Jul 10th
2012/06/06 15:40:26
Done.
| |
114 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, true)); | 98 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, true)); |
115 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, false)); | 99 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, false)); |
116 } | 100 } |
117 | 101 |
118 TEST_F(OneClickSigninHelperTest, CanOffer) { | 102 TEST_F(OneClickSigninHelperTest, CanOffer) { |
119 MarkCurrentThreadAsUIThread(); | 103 MarkCurrentThreadAsUIThread(); |
120 content::WebContents* web_contents = CreateMockWebContents(false); | 104 content::WebContents* web_contents = CreateMockWebContents(false, ""); |
121 | 105 |
122 EnableOneClick(true); | 106 EnableOneClick(true); |
123 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, true)); | 107 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, true)); |
124 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | 108 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); |
125 | 109 |
126 EnableOneClick(false); | 110 EnableOneClick(false); |
127 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 111 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); |
128 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 112 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); |
129 } | 113 } |
130 | 114 |
131 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) { | 115 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) { |
132 MarkCurrentThreadAsUIThread(); | 116 MarkCurrentThreadAsUIThread(); |
133 content::WebContents* web_contents = CreateMockWebContents(false); | 117 content::WebContents* web_contents = CreateMockWebContents(false, |
134 ConnectProfileToAccount("foo@gmail.com"); | 118 "foo@gmail.com"); |
135 | 119 |
136 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 120 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); |
137 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | 121 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); |
138 } | 122 } |
139 | 123 |
140 TEST_F(OneClickSigninHelperTest, CanOfferIncognito) { | 124 TEST_F(OneClickSigninHelperTest, CanOfferIncognito) { |
141 MarkCurrentThreadAsUIThread(); | 125 MarkCurrentThreadAsUIThread(); |
142 content::WebContents* web_contents = CreateMockWebContents(true); | 126 content::WebContents* web_contents = CreateMockWebContents(true, ""); |
143 | 127 |
144 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 128 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); |
145 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 129 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); |
146 } | 130 } |
147 | 131 |
148 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) { | 132 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) { |
149 MarkCurrentThreadAsUIThread(); | 133 MarkCurrentThreadAsUIThread(); |
150 content::WebContents* web_contents = CreateMockWebContents(true); | 134 content::WebContents* web_contents = CreateMockWebContents(false, ""); |
151 AllowSigninCookies(false); | 135 AllowSigninCookies(false); |
152 | 136 |
153 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 137 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); |
154 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 138 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); |
155 } | 139 } |
OLD | NEW |