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
3 // found in the LICENSE file.
7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/notifications/message_center_settings_controller.h"
10 #include "chrome/browser/prefs/pref_service_syncable.h"
11 #include "chrome/browser/profiles/profile_info_cache.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "chrome/test/base/testing_profile_manager.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/message_center/notifier_settings.h"
19 #if defined(OS_CHROMEOS)
20 #include "chrome/browser/chromeos/login/users/fake_user_manager.h"
21 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
24 class MessageCenterSettingsControllerTest
: public testing::Test
{
26 MessageCenterSettingsControllerTest()
27 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {};
28 virtual ~MessageCenterSettingsControllerTest() {};
30 base::FilePath
GetProfilePath(const std::string
& base_name
) {
31 return testing_profile_manager_
.profile_manager()->user_data_dir()
32 .AppendASCII(base_name
);
35 virtual void SetUp() OVERRIDE
{
36 ASSERT_TRUE(testing_profile_manager_
.SetUp());
39 virtual void CreateProfile(const std::string
& name
) {
40 testing_profile_manager_
.CreateTestingProfile(name
);
43 void CreateController() {
44 controller_
.reset(new MessageCenterSettingsController(
45 testing_profile_manager_
.profile_info_cache()));
48 void ResetController() {
52 MessageCenterSettingsController
* controller() { return controller_
.get(); }
55 TestingProfileManager testing_profile_manager_
;
56 scoped_ptr
<MessageCenterSettingsController
> controller_
;
57 content::TestBrowserThreadBundle thread_bundle_
;
59 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerTest
);
62 #if defined(OS_CHROMEOS)
63 // This value should be same as the one in fake_user_manager.cc
64 static const char kUserIdHashSuffix
[] = "-hash";
66 class MessageCenterSettingsControllerChromeOSTest
67 : public MessageCenterSettingsControllerTest
{
69 MessageCenterSettingsControllerChromeOSTest() {}
70 virtual ~MessageCenterSettingsControllerChromeOSTest() {}
72 virtual void SetUp() OVERRIDE
{
73 MessageCenterSettingsControllerTest::SetUp();
75 // Initialize the UserManager singleton to a fresh FakeUserManager instance.
76 user_manager_enabler_
.reset(
77 new chromeos::ScopedUserManagerEnabler(new chromeos::FakeUserManager
));
80 virtual void TearDown() OVERRIDE
{
82 MessageCenterSettingsControllerTest::TearDown();
85 virtual void CreateProfile(const std::string
& name
) OVERRIDE
{
86 MessageCenterSettingsControllerTest::CreateProfile(name
);
88 GetFakeUserManager()->AddUser(name
);
89 GetFakeUserManager()->UserLoggedIn(name
, name
+ kUserIdHashSuffix
, false);
92 void SwitchActiveUser(const std::string
& name
) {
93 GetFakeUserManager()->SwitchActiveUser(name
);
94 controller()->ActiveUserChanged(GetFakeUserManager()->GetActiveUser());
98 chromeos::FakeUserManager
* GetFakeUserManager() {
99 return static_cast<chromeos::FakeUserManager
*>(
100 user_manager::UserManager::Get());
103 scoped_ptr
<chromeos::ScopedUserManagerEnabler
> user_manager_enabler_
;
105 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerChromeOSTest
);
107 #endif // OS_CHROMEOS
109 #if !defined(OS_CHROMEOS)
110 TEST_F(MessageCenterSettingsControllerTest
, NotifierGroups
) {
111 CreateProfile("Profile-1");
112 CreateProfile("Profile-2");
115 EXPECT_EQ(controller()->GetNotifierGroupCount(), 2u);
117 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
118 base::UTF8ToUTF16("Profile-1"));
119 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 0u);
121 EXPECT_EQ(controller()->GetNotifierGroupAt(1).name
,
122 base::UTF8ToUTF16("Profile-2"));
123 EXPECT_EQ(controller()->GetNotifierGroupAt(1).index
, 1u);
125 EXPECT_EQ(controller()->GetActiveNotifierGroup().name
,
126 base::UTF8ToUTF16("Profile-1"));
127 EXPECT_EQ(controller()->GetActiveNotifierGroup().index
, 0u);
129 controller()->SwitchToNotifierGroup(1);
130 EXPECT_EQ(controller()->GetActiveNotifierGroup().name
,
131 base::UTF8ToUTF16("Profile-2"));
132 EXPECT_EQ(controller()->GetActiveNotifierGroup().index
, 1u);
134 controller()->SwitchToNotifierGroup(0);
135 EXPECT_EQ(controller()->GetActiveNotifierGroup().name
,
136 base::UTF8ToUTF16("Profile-1"));
139 TEST_F(MessageCenterSettingsControllerChromeOSTest
, NotifierGroups
) {
140 CreateProfile("Profile-1");
141 CreateProfile("Profile-2");
144 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
146 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
147 base::UTF8ToUTF16("Profile-1"));
148 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 0u);
150 SwitchActiveUser("Profile-2");
151 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
152 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
153 base::UTF8ToUTF16("Profile-2"));
154 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 1u);
156 SwitchActiveUser("Profile-1");
157 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
158 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
159 base::UTF8ToUTF16("Profile-1"));
160 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 0u);
164 // TODO(mukai): write a test case to reproduce the actual guest session scenario
165 // in ChromeOS -- no profiles in the profile_info_cache.