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/metrics/field_trial.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/notifications/message_center_settings_controller.h"
11 #include "chrome/browser/prefs/pref_service_syncable.h"
12 #include "chrome/browser/profiles/profile_info_cache.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_profile_manager.h"
17 #include "components/variations/entropy_provider.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/message_center/notifier_settings.h"
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/chromeos/login/fake_user_manager.h"
26 class MessageCenterSettingsControllerTest
: public testing::Test
{
28 MessageCenterSettingsControllerTest()
29 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {};
30 virtual ~MessageCenterSettingsControllerTest() {};
32 base::FilePath
GetProfilePath(const std::string
& base_name
) {
33 return testing_profile_manager_
.profile_manager()->user_data_dir()
34 .AppendASCII(base_name
);
37 virtual void SetUp() OVERRIDE
{
38 ASSERT_TRUE(testing_profile_manager_
.SetUp());
41 virtual void CreateProfile(const std::string
& name
) {
42 testing_profile_manager_
.CreateTestingProfile(name
);
45 void CreateController() {
46 controller_
.reset(new MessageCenterSettingsController(
47 testing_profile_manager_
.profile_info_cache()));
50 void ResetController() {
54 MessageCenterSettingsController
* controller() { return controller_
.get(); }
57 TestingProfileManager testing_profile_manager_
;
58 scoped_ptr
<MessageCenterSettingsController
> controller_
;
60 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerTest
);
63 #if defined(OS_CHROMEOS)
64 // This value should be same as the one in fake_user_manager.cc
65 static const char kUserIdHashSuffix
[] = "-hash";
67 class MessageCenterSettingsControllerChromeOSTest
68 : public MessageCenterSettingsControllerTest
{
70 MessageCenterSettingsControllerChromeOSTest() {}
71 virtual ~MessageCenterSettingsControllerChromeOSTest() {}
73 virtual void SetUp() OVERRIDE
{
74 MessageCenterSettingsControllerTest::SetUp();
76 // Enabling multi profile requires several flags to be set.
77 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kMultiProfiles
);
78 field_trial_list_
.reset(new base::FieldTrialList(
79 new metrics::SHA1EntropyProvider("42")));
80 base::FieldTrialList::CreateTrialsFromString(
81 "ChromeOSUseMultiProfiles/Enable/",
82 base::FieldTrialList::ACTIVATE_TRIALS
);
84 // Initialize the UserManager singleton to a fresh FakeUserManager instance.
85 user_manager_enabler_
.reset(
86 new chromeos::ScopedUserManagerEnabler(new chromeos::FakeUserManager
));
89 virtual void TearDown() OVERRIDE
{
91 MessageCenterSettingsControllerTest::TearDown();
94 virtual void CreateProfile(const std::string
& name
) OVERRIDE
{
95 MessageCenterSettingsControllerTest::CreateProfile(name
);
97 GetFakeUserManager()->AddUser(name
);
98 GetFakeUserManager()->UserLoggedIn(name
, name
+ kUserIdHashSuffix
, false);
101 void SwitchActiveUser(const std::string
& name
) {
102 GetFakeUserManager()->SwitchActiveUser(name
);
103 controller()->ActiveUserChanged(GetFakeUserManager()->GetActiveUser());
107 chromeos::FakeUserManager
* GetFakeUserManager() {
108 return static_cast<chromeos::FakeUserManager
*>(
109 chromeos::UserManager::Get());
112 scoped_ptr
<chromeos::ScopedUserManagerEnabler
> user_manager_enabler_
;
113 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
115 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerChromeOSTest
);
117 #endif // OS_CHROMEOS
119 #if !defined(OS_CHROMEOS)
120 TEST_F(MessageCenterSettingsControllerTest
, NotifierGroups
) {
121 CreateProfile("Profile-1");
122 CreateProfile("Profile-2");
125 EXPECT_EQ(controller()->GetNotifierGroupCount(), 2u);
127 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
128 base::UTF8ToUTF16("Profile-1"));
129 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 0u);
131 EXPECT_EQ(controller()->GetNotifierGroupAt(1).name
,
132 base::UTF8ToUTF16("Profile-2"));
133 EXPECT_EQ(controller()->GetNotifierGroupAt(1).index
, 1u);
135 EXPECT_EQ(controller()->GetActiveNotifierGroup().name
,
136 base::UTF8ToUTF16("Profile-1"));
137 EXPECT_EQ(controller()->GetActiveNotifierGroup().index
, 0u);
139 controller()->SwitchToNotifierGroup(1);
140 EXPECT_EQ(controller()->GetActiveNotifierGroup().name
,
141 base::UTF8ToUTF16("Profile-2"));
142 EXPECT_EQ(controller()->GetActiveNotifierGroup().index
, 1u);
144 controller()->SwitchToNotifierGroup(0);
145 EXPECT_EQ(controller()->GetActiveNotifierGroup().name
,
146 base::UTF8ToUTF16("Profile-1"));
149 TEST_F(MessageCenterSettingsControllerChromeOSTest
, NotifierGroups
) {
150 CreateProfile("Profile-1");
151 CreateProfile("Profile-2");
154 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);
160 SwitchActiveUser("Profile-2");
161 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
162 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
163 base::UTF8ToUTF16("Profile-2"));
164 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 1u);
166 SwitchActiveUser("Profile-1");
167 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
168 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name
,
169 base::UTF8ToUTF16("Profile-1"));
170 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index
, 0u);
174 // TODO(mukai): write a test case to reproduce the actual guest session scenario
175 // in ChromeOS -- no profiles in the profile_info_cache.