750f29f47c5fbfcc37485bd2d16b7ceee19a460a
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / message_center_settings_controller_unittest.cc
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.
4
5 #include <string>
6
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"
18
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"
22 #endif
23
24 class MessageCenterSettingsControllerTest : public testing::Test {
25  protected:
26   MessageCenterSettingsControllerTest()
27       : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {};
28   virtual ~MessageCenterSettingsControllerTest() {};
29
30   base::FilePath GetProfilePath(const std::string& base_name) {
31     return testing_profile_manager_.profile_manager()->user_data_dir()
32         .AppendASCII(base_name);
33   }
34
35   virtual void SetUp() OVERRIDE {
36     ASSERT_TRUE(testing_profile_manager_.SetUp());
37   }
38
39   virtual void CreateProfile(const std::string& name) {
40     testing_profile_manager_.CreateTestingProfile(name);
41   }
42
43   void CreateController() {
44     controller_.reset(new MessageCenterSettingsController(
45         testing_profile_manager_.profile_info_cache()));
46   }
47
48   void ResetController() {
49     controller_.reset();
50   }
51
52   MessageCenterSettingsController* controller() { return controller_.get(); }
53
54  private:
55   TestingProfileManager testing_profile_manager_;
56   scoped_ptr<MessageCenterSettingsController> controller_;
57   content::TestBrowserThreadBundle thread_bundle_;
58
59   DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerTest);
60 };
61
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";
65
66 class MessageCenterSettingsControllerChromeOSTest
67     : public MessageCenterSettingsControllerTest {
68  protected:
69   MessageCenterSettingsControllerChromeOSTest() {}
70   virtual ~MessageCenterSettingsControllerChromeOSTest() {}
71
72   virtual void SetUp() OVERRIDE {
73     MessageCenterSettingsControllerTest::SetUp();
74
75     // Initialize the UserManager singleton to a fresh FakeUserManager instance.
76     user_manager_enabler_.reset(
77         new chromeos::ScopedUserManagerEnabler(new chromeos::FakeUserManager));
78   }
79
80   virtual void TearDown() OVERRIDE {
81     ResetController();
82     MessageCenterSettingsControllerTest::TearDown();
83   }
84
85   virtual void CreateProfile(const std::string& name) OVERRIDE {
86     MessageCenterSettingsControllerTest::CreateProfile(name);
87
88     GetFakeUserManager()->AddUser(name);
89     GetFakeUserManager()->UserLoggedIn(name, name + kUserIdHashSuffix, false);
90   }
91
92   void SwitchActiveUser(const std::string& name) {
93     GetFakeUserManager()->SwitchActiveUser(name);
94     controller()->ActiveUserChanged(GetFakeUserManager()->GetActiveUser());
95   }
96
97  private:
98   chromeos::FakeUserManager* GetFakeUserManager() {
99     return static_cast<chromeos::FakeUserManager*>(
100         user_manager::UserManager::Get());
101   }
102
103   scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
104
105   DISALLOW_COPY_AND_ASSIGN(MessageCenterSettingsControllerChromeOSTest);
106 };
107 #endif  // OS_CHROMEOS
108
109 #if !defined(OS_CHROMEOS)
110 TEST_F(MessageCenterSettingsControllerTest, NotifierGroups) {
111   CreateProfile("Profile-1");
112   CreateProfile("Profile-2");
113   CreateController();
114
115   EXPECT_EQ(controller()->GetNotifierGroupCount(), 2u);
116
117   EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
118             base::UTF8ToUTF16("Profile-1"));
119   EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
120
121   EXPECT_EQ(controller()->GetNotifierGroupAt(1).name,
122             base::UTF8ToUTF16("Profile-2"));
123   EXPECT_EQ(controller()->GetNotifierGroupAt(1).index, 1u);
124
125   EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
126             base::UTF8ToUTF16("Profile-1"));
127   EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 0u);
128
129   controller()->SwitchToNotifierGroup(1);
130   EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
131             base::UTF8ToUTF16("Profile-2"));
132   EXPECT_EQ(controller()->GetActiveNotifierGroup().index, 1u);
133
134   controller()->SwitchToNotifierGroup(0);
135   EXPECT_EQ(controller()->GetActiveNotifierGroup().name,
136             base::UTF8ToUTF16("Profile-1"));
137 }
138 #else
139 TEST_F(MessageCenterSettingsControllerChromeOSTest, NotifierGroups) {
140   CreateProfile("Profile-1");
141   CreateProfile("Profile-2");
142   CreateController();
143
144   EXPECT_EQ(controller()->GetNotifierGroupCount(), 1u);
145
146   EXPECT_EQ(controller()->GetNotifierGroupAt(0).name,
147             base::UTF8ToUTF16("Profile-1"));
148   EXPECT_EQ(controller()->GetNotifierGroupAt(0).index, 0u);
149
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);
155
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);
161 }
162 #endif
163
164 // TODO(mukai): write a test case to reproduce the actual guest session scenario
165 // in ChromeOS -- no profiles in the profile_info_cache.