OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" | 12 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" |
13 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 13 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
14 #include "chrome/browser/prefs/pref_service_syncable.h" | 14 #include "chrome/browser/prefs/pref_service_syncable.h" |
15 #include "chrome/browser/profiles/avatar_menu.h" | 15 #include "chrome/browser/profiles/avatar_menu.h" |
16 #include "chrome/browser/profiles/avatar_menu_observer.h" | 16 #include "chrome/browser/profiles/avatar_menu_observer.h" |
17 #include "chrome/browser/profiles/profile_info_cache.h" | 17 #include "chrome/browser/profiles/profile_info_cache.h" |
18 #include "chrome/browser/ui/ash/chrome_shell_delegate.h" | 18 #include "chrome/browser/ui/ash/chrome_shell_delegate.h" |
19 #include "chrome/common/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/test/base/testing_browser_process.h" | 21 #include "chrome/test/base/testing_browser_process.h" |
22 #include "chrome/test/base/testing_profile_manager.h" | 22 #include "chrome/test/base/testing_profile_manager.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 using base::ASCIIToUTF16; | 25 using base::ASCIIToUTF16; |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // As defined in /chromeos/dbus/cryptohome_client.cc. | |
30 static const char kUserIdHashSuffix[] = "-hash"; | |
31 | |
32 class MockObserver : public AvatarMenuObserver { | 29 class MockObserver : public AvatarMenuObserver { |
33 public: | 30 public: |
34 MockObserver() : count_(0) {} | 31 MockObserver() : count_(0) {} |
35 virtual ~MockObserver() {} | 32 virtual ~MockObserver() {} |
36 | 33 |
37 virtual void OnAvatarMenuChanged( | 34 virtual void OnAvatarMenuChanged( |
38 AvatarMenu* avatar_menu) OVERRIDE { | 35 AvatarMenu* avatar_menu) OVERRIDE { |
39 ++count_; | 36 ++count_; |
40 } | 37 } |
41 | 38 |
(...skipping 29 matching lines...) Expand all Loading... |
71 FakeUserManager* GetFakeUserManager() { | 68 FakeUserManager* GetFakeUserManager() { |
72 return static_cast<FakeUserManager*>(UserManager::Get()); | 69 return static_cast<FakeUserManager*>(UserManager::Get()); |
73 } | 70 } |
74 | 71 |
75 void AddProfile(base::string16 name, bool log_in) { | 72 void AddProfile(base::string16 name, bool log_in) { |
76 std::string email_string = base::UTF16ToASCII(name) + "@example.com"; | 73 std::string email_string = base::UTF16ToASCII(name) + "@example.com"; |
77 | 74 |
78 // Add a user to the fake user manager. | 75 // Add a user to the fake user manager. |
79 GetFakeUserManager()->AddUser(email_string); | 76 GetFakeUserManager()->AddUser(email_string); |
80 if (log_in) { | 77 if (log_in) { |
81 GetFakeUserManager()->UserLoggedIn( | 78 GetFakeUserManager()->LoginUser(email_string); |
82 email_string, | |
83 email_string + kUserIdHashSuffix, | |
84 false); | |
85 } | 79 } |
86 | 80 |
87 // Create a profile for the user. | 81 // Create a profile for the user. |
88 manager()->CreateTestingProfile( | 82 manager()->CreateTestingProfile(email_string); |
89 chrome::kProfileDirPrefix + email_string + kUserIdHashSuffix, | |
90 scoped_ptr<PrefServiceSyncable>(), | |
91 ASCIIToUTF16(email_string), 0, std::string(), | |
92 TestingProfile::TestingFactories()); | |
93 } | 83 } |
94 | 84 |
95 AvatarMenu* GetAvatarMenu() { | 85 AvatarMenu* GetAvatarMenu() { |
96 // Reset the MockObserver. | 86 // Reset the MockObserver. |
97 mock_observer_.reset(new MockObserver()); | 87 mock_observer_.reset(new MockObserver()); |
98 EXPECT_EQ(0, change_count()); | 88 EXPECT_EQ(0, change_count()); |
99 | 89 |
100 // Reset the menu. | 90 // Reset the menu. |
101 avatar_menu_.reset(new AvatarMenu( | 91 avatar_menu_.reset(new AvatarMenu( |
102 manager()->profile_info_cache(), | 92 manager()->profile_info_cache(), |
103 mock_observer_.get(), | 93 mock_observer_.get(), |
104 NULL)); | 94 NULL)); |
105 avatar_menu_->RebuildMenu(); | 95 avatar_menu_->RebuildMenu(); |
106 EXPECT_EQ(0, change_count()); | 96 EXPECT_EQ(0, change_count()); |
107 return avatar_menu_.get(); | 97 return avatar_menu_.get(); |
108 } | 98 } |
109 | 99 |
110 void ActiveUserChanged(ProfileHelper* profile_helper, | 100 void ActiveUserChanged(const base::string16& name) { |
111 const std::string& hash) { | 101 std::string email_string = base::UTF16ToASCII(name) + "@example.com"; |
112 profile_helper->ActiveUserHashChanged(hash); | 102 GetFakeUserManager()->SwitchActiveUser(email_string); |
113 } | 103 } |
114 | 104 |
115 TestingProfileManager* manager() { return &manager_; } | 105 TestingProfileManager* manager() { return &manager_; } |
116 | 106 |
117 int change_count() const { return mock_observer_->change_count(); } | 107 int change_count() const { return mock_observer_->change_count(); } |
118 | 108 |
119 private: | 109 private: |
120 TestingProfileManager manager_; | 110 TestingProfileManager manager_; |
121 scoped_ptr<MockObserver> mock_observer_; | 111 scoped_ptr<MockObserver> mock_observer_; |
122 scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_; | 112 scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 EXPECT_FALSE(menu->ShouldShowAddNewProfileLink()); | 202 EXPECT_FALSE(menu->ShouldShowAddNewProfileLink()); |
213 } | 203 } |
214 | 204 |
215 TEST_F(ProfileListChromeOSTest, ActiveItem) { | 205 TEST_F(ProfileListChromeOSTest, ActiveItem) { |
216 base::string16 name1(ASCIIToUTF16("p1.com")); | 206 base::string16 name1(ASCIIToUTF16("p1.com")); |
217 base::string16 name2(ASCIIToUTF16("p2.com")); | 207 base::string16 name2(ASCIIToUTF16("p2.com")); |
218 | 208 |
219 AddProfile(name1, true); | 209 AddProfile(name1, true); |
220 AddProfile(name2, true); | 210 AddProfile(name2, true); |
221 | 211 |
222 // Initialize ProfileHelper, it will be accessed from GetActiveProfileIndex. | 212 ActiveUserChanged(name1); |
223 std::string email_string = base::UTF16ToASCII(name1) + "@example.com"; | |
224 std::string hash = email_string + kUserIdHashSuffix; | |
225 ActiveUserChanged(ProfileHelper::Get(), hash); | |
226 | 213 |
227 AvatarMenu* menu = GetAvatarMenu(); | 214 AvatarMenu* menu = GetAvatarMenu(); |
228 | 215 |
229 ASSERT_EQ(2U, menu->GetNumberOfItems()); | 216 ASSERT_EQ(2U, menu->GetNumberOfItems()); |
230 // TODO(jeremy): Expand test to verify active profile index other than 0 | 217 // TODO(jeremy): Expand test to verify active profile index other than 0 |
231 // crbug.com/100871 | 218 // crbug.com/100871 |
232 ASSERT_EQ(0U, menu->GetActiveProfileIndex()); | 219 ASSERT_EQ(0U, menu->GetActiveProfileIndex()); |
233 } | 220 } |
234 | 221 |
235 TEST_F(ProfileListChromeOSTest, ModifyingNameResortsCorrectly) { | 222 TEST_F(ProfileListChromeOSTest, ModifyingNameResortsCorrectly) { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 | 297 |
311 // Should only show avatar menu with multiple users. | 298 // Should only show avatar menu with multiple users. |
312 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu()); | 299 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu()); |
313 | 300 |
314 AddProfile(name2, false); | 301 AddProfile(name2, false); |
315 | 302 |
316 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu()); | 303 EXPECT_FALSE(AvatarMenu::ShouldShowAvatarMenu()); |
317 } | 304 } |
318 | 305 |
319 } // namespace chromeos | 306 } // namespace chromeos |
OLD | NEW |