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)
64 class MessageCenterSettingsControllerChromeOSTest
65 : public MessageCenterSettingsControllerTest
{
67 MessageCenterSettingsControllerChromeOSTest() {}
68 virtual ~MessageCenterSettingsControllerChromeOSTest() {}
70 virtual void SetUp() override
{
71 MessageCenterSettingsControllerTest::SetUp();
73 // Initialize the UserManager singleton to a fresh FakeUserManager instance.
74 user_manager_enabler_
.reset(
75 new chromeos::ScopedUserManagerEnabler(new chromeos::FakeUserManager
));
78 virtual void TearDown() override
{
80 MessageCenterSettingsControllerTest::TearDown();
83 virtual void CreateProfile(const std::string
& name
) override
{
84 MessageCenterSettingsControllerTest::CreateProfile(name
);
86 GetFakeUserManager()->AddUser(name
);
87 GetFakeUserManager()->LoginUser(name
);
90 void SwitchActiveUser(const std::string
& name
) {
91 GetFakeUserManager()->SwitchActiveUser(name
);
92 controller()->ActiveUserChanged(GetFakeUserManager()->GetActiveUser());
96 chromeos::FakeUserManager
* GetFakeUserManager() {
97 return static_cast<chromeos::FakeUserManager
*>(
98 user_manager::UserManager::Get());
101 scoped_ptr
<chromeos::ScopedUserManagerEnabler
> user_manager_enabler_
;
103 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerChromeOSTest
);
105 #endif // OS_CHROMEOS
107 #if !defined(OS_CHROMEOS)
108 TEST_F(MessageCenterSettingsControllerTest
, NotifierGroups
) {
109 CreateProfile("Profile-1");
110 CreateProfile("Profile-2");
113 EXPECT_EQ(controller()->GetNotifierGroupCount(), 2u);
115 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
116 base::UTF8ToUTF16("Profile-1"));
117 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 0u);
119 EXPECT_EQ(controller()->GetNotifierGroupAt(1).name
,
120 base::UTF8ToUTF16("Profile-2"));
121 EXPECT_EQ(controller()->GetNotifierGroupAt(1).index
, 1u);
123 EXPECT_EQ(controller()->GetActiveNotifierGroup().name
,
124 base::UTF8ToUTF16("Profile-1"));
125 EXPECT_EQ(controller()->GetActiveNotifierGroup().index
, 0u);
127 controller()->SwitchToNotifierGroup(1);
128 EXPECT_EQ(controller()->GetActiveNotifierGroup().name
,
129 base::UTF8ToUTF16("Profile-2"));
130 EXPECT_EQ(controller()->GetActiveNotifierGroup().index
, 1u);
132 controller()->SwitchToNotifierGroup(0);
133 EXPECT_EQ(controller()->GetActiveNotifierGroup().name
,
134 base::UTF8ToUTF16("Profile-1"));
137 TEST_F(MessageCenterSettingsControllerChromeOSTest
, NotifierGroups
) {
138 CreateProfile("Profile-1");
139 CreateProfile("Profile-2");
142 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
144 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
145 base::UTF8ToUTF16("Profile-1"));
146 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 0u);
148 SwitchActiveUser("Profile-2");
149 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
150 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
151 base::UTF8ToUTF16("Profile-2"));
152 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 1u);
154 SwitchActiveUser("Profile-1");
155 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
156 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
157 base::UTF8ToUTF16("Profile-1"));
158 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 0u);
162 // TODO(mukai): write a test case to reproduce the actual guest session scenario
163 // in ChromeOS -- no profiles in the profile_info_cache.