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.
5 #include "base/message_loop/message_loop.h"
6 #include "base/observer_list.h"
7 #include "base/prefs/testing_pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile_info_cache.h"
11 #include "chrome/browser/signin/fake_signin_manager.h"
12 #include "chrome/browser/signin/signin_names_io_thread.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "chrome/test/base/testing_profile_manager.h"
16 #include "components/signin/core/browser/signin_manager.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 class SigninNamesOnIOThreadTest
: public testing::Test
{
24 SigninNamesOnIOThreadTest();
25 void SetUp() override
;
26 void TearDown() override
;
28 void SimulateSignin(const base::string16
& email
);
29 void AddNewProfile(const base::string16
& name
, const base::string16
& email
);
31 base::MessageLoop message_loop_
;
32 content::TestBrowserThread ui_thread_
;
33 content::TestBrowserThread io_thread_
;
34 scoped_ptr
<TestingProfile
> profile_
;
35 FakeSigninManager
* signin_manager_
;
36 TestingProfileManager testing_profile_manager_
;
37 SigninNamesOnIOThread signin_names_
;
40 SigninNamesOnIOThreadTest::SigninNamesOnIOThreadTest()
41 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
42 io_thread_(content::BrowserThread::IO
, &message_loop_
),
43 testing_profile_manager_(
44 TestingBrowserProcess::GetGlobal()) {
47 void SigninNamesOnIOThreadTest::SetUp() {
48 ASSERT_TRUE(testing_profile_manager_
.SetUp());
49 TestingProfile::Builder builder
;
50 builder
.AddTestingFactory(SigninManagerFactory::GetInstance(),
51 FakeSigninManagerBase::Build
);
52 profile_
= builder
.Build();
53 signin_manager_
= static_cast<FakeSigninManager
*>(
54 SigninManagerFactory::GetForProfile(profile_
.get()));
57 void SigninNamesOnIOThreadTest::TearDown() {
58 signin_names_
.ReleaseResourcesOnUIThread();
61 void SigninNamesOnIOThreadTest::SimulateSignin(const base::string16
& email
) {
62 signin_manager_
->SignIn(base::UTF16ToUTF8(email
), "password");
65 void SigninNamesOnIOThreadTest::AddNewProfile(const base::string16
& name
,
66 const base::string16
& email
) {
67 ProfileInfoCache
* cache
= testing_profile_manager_
.profile_info_cache();
68 const base::FilePath
& user_data_dir
= cache
->GetUserDataDir();
70 const base::FilePath profile_dir
=
71 user_data_dir
.Append(base::UTF16ToUTF8(name
));
73 const base::FilePath profile_dir
= user_data_dir
.Append(name
);
75 cache
->AddProfileToCache(profile_dir
, name
, email
, 0, std::string());
78 TEST_F(SigninNamesOnIOThreadTest
, Basic
) {
79 ASSERT_EQ(0u, signin_names_
.GetEmails().size());
82 TEST_F(SigninNamesOnIOThreadTest
, Signin
) {
83 const base::string16 email
= base::UTF8ToUTF16("foo@gmail.com");
84 SimulateSignin(email
);
86 const SigninNamesOnIOThread::EmailSet
& emails
= signin_names_
.GetEmails();
87 ASSERT_EQ(1u, emails
.size());
88 ASSERT_EQ(1u, emails
.count(email
));
91 TEST_F(SigninNamesOnIOThreadTest
, Signout
) {
92 const base::string16 email
= base::UTF8ToUTF16("foo@gmail.com");
93 SimulateSignin(email
);
94 signin_manager_
->SignOut(signin_metrics::SIGNOUT_TEST
);
96 const SigninNamesOnIOThread::EmailSet
& emails
= signin_names_
.GetEmails();
97 ASSERT_EQ(0u, emails
.size());
100 TEST_F(SigninNamesOnIOThreadTest
, StartWithConnectedProfiles
) {
101 // Setup a couple of connected profiles, and one unconnected.
102 const base::string16 email1
= base::UTF8ToUTF16("foo@gmail.com");
103 const base::string16 email2
= base::UTF8ToUTF16("bar@gmail.com");
104 AddNewProfile(base::UTF8ToUTF16("foo"), email1
);
105 AddNewProfile(base::UTF8ToUTF16("bar"), email2
);
106 AddNewProfile(base::UTF8ToUTF16("toto"), base::string16());
108 SigninNamesOnIOThread signin_names
;
110 const SigninNamesOnIOThread::EmailSet
& emails
= signin_names
.GetEmails();
111 ASSERT_EQ(2u, emails
.size());
112 ASSERT_EQ(1u, emails
.count(email1
));
113 ASSERT_EQ(1u, emails
.count(email2
));
115 signin_names
.ReleaseResourcesOnUIThread();