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/prefs/testing_pref_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile_info_cache.h"
10 #include "chrome/browser/signin/signin_manager.h"
11 #include "chrome/browser/signin/signin_names_io_thread.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "chrome/test/base/testing_profile_manager.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
22 class SigninNamesOnIOThreadTest
: public testing::Test
{
24 SigninNamesOnIOThreadTest();
25 virtual void SetUp() OVERRIDE
;
26 virtual void TearDown() OVERRIDE
;
28 void SimulateSignin(const base::string16
& email
);
29 void SimulateSignout(const base::string16
& email
);
30 void AddNewProfile(const base::string16
& name
, const base::string16
& email
);
32 base::MessageLoop message_loop_
;
33 content::TestBrowserThread ui_thread_
;
34 content::TestBrowserThread io_thread_
;
35 TestingProfileManager testing_profile_manager_
;
36 SigninNamesOnIOThread signin_names_
;
39 SigninNamesOnIOThreadTest::SigninNamesOnIOThreadTest()
40 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
41 io_thread_(content::BrowserThread::IO
, &message_loop_
),
42 testing_profile_manager_(
43 TestingBrowserProcess::GetGlobal()) {
46 void SigninNamesOnIOThreadTest::SetUp() {
47 ASSERT_TRUE(testing_profile_manager_
.SetUp());
50 void SigninNamesOnIOThreadTest::TearDown() {
51 signin_names_
.ReleaseResourcesOnUIThread();
54 void SigninNamesOnIOThreadTest::SimulateSignin(const base::string16
& email
) {
55 GoogleServiceSigninSuccessDetails
details(
56 base::UTF16ToUTF8(email
), "password");
57 content::NotificationService::current()->Notify(
58 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL
,
59 content::Source
<Profile
>(NULL
),
60 content::Details
<const GoogleServiceSigninSuccessDetails
>(&details
));
63 void SigninNamesOnIOThreadTest::SimulateSignout(const base::string16
& email
) {
64 GoogleServiceSignoutDetails
details(base::UTF16ToUTF8(email
));
65 content::NotificationService::current()->Notify(
66 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT
,
67 content::Source
<Profile
>(NULL
),
68 content::Details
<const GoogleServiceSignoutDetails
>(&details
));
71 void SigninNamesOnIOThreadTest::AddNewProfile(const base::string16
& name
,
72 const base::string16
& email
) {
73 ProfileInfoCache
* cache
= testing_profile_manager_
.profile_info_cache();
74 const base::FilePath
& user_data_dir
= cache
->GetUserDataDir();
76 const base::FilePath profile_dir
=
77 user_data_dir
.Append(base::UTF16ToUTF8(name
));
79 const base::FilePath profile_dir
= user_data_dir
.Append(name
);
81 cache
->AddProfileToCache(profile_dir
, name
, email
, 0, std::string());
86 TEST_F(SigninNamesOnIOThreadTest
, Basic
) {
87 ASSERT_EQ(0u, signin_names_
.GetEmails().size());
90 TEST_F(SigninNamesOnIOThreadTest
, Signin
) {
91 const base::string16 email
= base::UTF8ToUTF16("foo@gmail.com");
92 SimulateSignin(email
);
94 const SigninNamesOnIOThread::EmailSet
& emails
= signin_names_
.GetEmails();
95 ASSERT_EQ(1u, emails
.size());
96 ASSERT_EQ(1u, emails
.count(email
));
99 TEST_F(SigninNamesOnIOThreadTest
, Signout
) {
100 const base::string16 email
= base::UTF8ToUTF16("foo@gmail.com");
101 SimulateSignin(email
);
102 SimulateSignout(email
);
104 const SigninNamesOnIOThread::EmailSet
& emails
= signin_names_
.GetEmails();
105 ASSERT_EQ(0u, emails
.size());
108 TEST_F(SigninNamesOnIOThreadTest
, HandleUnknownSignout
) {
109 const base::string16 email
= base::UTF8ToUTF16("foo@gmail.com");
110 SimulateSignin(email
);
111 SimulateSignout(base::UTF8ToUTF16("bar@gmail.com"));
113 const SigninNamesOnIOThread::EmailSet
& emails
= signin_names_
.GetEmails();
114 ASSERT_EQ(1u, emails
.size());
115 ASSERT_EQ(1u, emails
.count(email
));
118 TEST_F(SigninNamesOnIOThreadTest
, StartWithConnectedProfiles
) {
119 // Setup a couple of connected profiles, and one unconnected.
120 const base::string16 email1
= base::UTF8ToUTF16("foo@gmail.com");
121 const base::string16 email2
= base::UTF8ToUTF16("bar@gmail.com");
122 AddNewProfile(base::UTF8ToUTF16("foo"), email1
);
123 AddNewProfile(base::UTF8ToUTF16("bar"), email2
);
124 AddNewProfile(base::UTF8ToUTF16("toto"), base::string16());
126 SigninNamesOnIOThread signin_names
;
128 const SigninNamesOnIOThread::EmailSet
& emails
= signin_names
.GetEmails();
129 ASSERT_EQ(2u, emails
.size());
130 ASSERT_EQ(1u, emails
.count(email1
));
131 ASSERT_EQ(1u, emails
.count(email2
));
133 signin_names
.ReleaseResourcesOnUIThread();