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 ~MessageCenterSettingsControllerTest() override{};
30 base::FilePath GetProfilePath(const std::string& base_name) {
31 return testing_profile_manager_.profile_manager()->user_data_dir()
32 .AppendASCII(base_name);
35 void SetUp() override { ASSERT_TRUE(testing_profile_manager_.SetUp()); }
37 virtual void CreateProfile(const std::string& name) {
38 testing_profile_manager_.CreateTestingProfile(name);
41 void CreateController() {
42 controller_.reset(new MessageCenterSettingsController(
43 testing_profile_manager_.profile_info_cache()));
46 void ResetController() {
50 MessageCenterSettingsController* controller() { return controller_.get(); }
53 TestingProfileManager testing_profile_manager_;
54 scoped_ptr<MessageCenterSettingsController> controller_;
55 content::TestBrowserThreadBundle thread_bundle_;
57 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerTest);
60 #if defined(OS_CHROMEOS)
62 class MessageCenterSettingsControllerChromeOSTest
63 : public MessageCenterSettingsControllerTest {
65 MessageCenterSettingsControllerChromeOSTest() {}
66 virtual ~MessageCenterSettingsControllerChromeOSTest() {}
68 virtual void SetUp() override {
69 MessageCenterSettingsControllerTest::SetUp();
71 // Initialize the UserManager singleton to a fresh FakeUserManager instance.
72 user_manager_enabler_.reset(
73 new chromeos::ScopedUserManagerEnabler(new chromeos::FakeUserManager));
76 virtual void TearDown() override {
78 MessageCenterSettingsControllerTest::TearDown();
81 virtual void CreateProfile(const std::string& name) override {
82 MessageCenterSettingsControllerTest::CreateProfile(name);
84 GetFakeUserManager()->AddUser(name);
85 GetFakeUserManager()->LoginUser(name);
88 void SwitchActiveUser(const std::string& name) {
89 GetFakeUserManager()->SwitchActiveUser(name);
90 controller()->ActiveUserChanged(GetFakeUserManager()->GetActiveUser());
94 chromeos::FakeUserManager* GetFakeUserManager() {
95 return static_cast<chromeos::FakeUserManager*>(
96 user_manager::UserManager::Get());
99 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
101 DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerChromeOSTest);
103 #endif // OS_CHROMEOS
105 #if !defined(OS_CHROMEOS)
106 TEST_F(MessageCenterSettingsControllerTest, NotifierGroups) {
107 CreateProfile("Profile-1");
108 CreateProfile("Profile-2");
111 EXPECT_EQ(controller()->GetNotifierGroupCount(), 2u);
113 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
114 base::UTF8ToUTF16("Profile-1"));
115 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
117 EXPECT_EQ(controller()->GetNotifierGroupAt(1).name,
118 base::UTF8ToUTF16("Profile-2"));
119 EXPECT_EQ(controller()->GetNotifierGroupAt(1).index, 1u);
121 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
122 base::UTF8ToUTF16("Profile-1"));
123 EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 0u);
125 controller()->SwitchToNotifierGroup(1);
126 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
127 base::UTF8ToUTF16("Profile-2"));
128 EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 1u);
130 controller()->SwitchToNotifierGroup(0);
131 EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
132 base::UTF8ToUTF16("Profile-1"));
135 TEST_F(MessageCenterSettingsControllerChromeOSTest, NotifierGroups) {
136 CreateProfile("Profile-1");
137 CreateProfile("Profile-2");
140 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
142 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
143 base::UTF8ToUTF16("Profile-1"));
144 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
146 SwitchActiveUser("Profile-2");
147 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
148 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
149 base::UTF8ToUTF16("Profile-2"));
150 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 1u);
152 SwitchActiveUser("Profile-1");
153 EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
154 EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
155 base::UTF8ToUTF16("Profile-1"));
156 EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
160 // TODO(mukai): write a test case to reproduce the actual guest session scenario
161 // in ChromeOS -- no profiles in the profile_info_cache.