NaCl: Update revision in DEPS, e8fbd4b -> 9e3dac8
[chromium-blink-merge.git] / ui / message_center / cocoa / settings_controller_unittest.mm
blobc686fcfe6b25ae91b45e3534c646691996748af9
1 // Copyright (c) 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.
5 #import "ui/message_center/cocoa/settings_controller.h"
7 #include "base/strings/utf_string_conversions.h"
8 #import "ui/gfx/test/ui_cocoa_test_helper.h"
9 #include "ui/message_center/fake_notifier_settings_provider.h"
11 @implementation MCSettingsController (TestingInterface)
12 - (NSInteger)profileSwitcherListCount {
13   // Subtract the dummy item.
14   return [self groupDropDownButton]
15                  ? [[self groupDropDownButton] numberOfItems] - 1
16                  : 0;
19 - (NSUInteger)scrollViewItemCount {
20   return [[[[self scrollView] documentView] subviews] count];
23 - (MCSettingsEntryView*)bottomMostButton {
24   // The checkboxes are created bottom-to-top, so the first object is the
25   // bottom-most.
26   return [[[[self scrollView] documentView] subviews] objectAtIndex:0];
28 @end
30 namespace message_center {
32 using ui::CocoaTest;
34 namespace {
36 NotifierGroup* NewGroup(const std::string& name,
37                         const std::string& login_info) {
38   return new NotifierGroup(gfx::Image(),
39                            base::UTF8ToUTF16(name),
40                            base::UTF8ToUTF16(login_info),
41                            true);
44 Notifier* NewNotifier(const std::string& id,
45                       const std::string& title,
46                       bool enabled) {
47   NotifierId notifier_id(NotifierId::APPLICATION, id);
48   return new Notifier(notifier_id, base::UTF8ToUTF16(title), enabled);
51 }  // namespace
53 TEST_F(CocoaTest, Basic) {
54   // Notifiers are owned by settings controller.
55   std::vector<Notifier*> notifiers;
56   notifiers.push_back(NewNotifier("id", "title", /*enabled=*/ true));
57   notifiers.push_back(NewNotifier("id2", "other title", /*enabled=*/ false));
59   FakeNotifierSettingsProvider provider(notifiers);
61   base::scoped_nsobject<MCSettingsController> controller(
62       [[MCSettingsController alloc] initWithProvider:&provider
63                                   trayViewController:nil]);
64   [controller view];
66   EXPECT_EQ(notifiers.size(), [controller scrollViewItemCount]);
69 TEST_F(CocoaTest, Toggle) {
70   // Notifiers are owned by settings controller.
71   std::vector<Notifier*> notifiers;
72   notifiers.push_back(NewNotifier("id", "title", /*enabled=*/ true));
73   notifiers.push_back(NewNotifier("id2", "other title", /*enabled=*/ false));
75   FakeNotifierSettingsProvider provider(notifiers);
77   base::scoped_nsobject<MCSettingsController> controller(
78       [[MCSettingsController alloc] initWithProvider:&provider
79                                   trayViewController:nil]);
80   [controller view];
82   MCSettingsEntryView* toggleView = [controller bottomMostButton];
83   NSButton* toggleSecond = [toggleView checkbox];
85   [toggleSecond performClick:nil];
86   EXPECT_TRUE(provider.WasEnabled(*notifiers.back()));
88   [toggleSecond performClick:nil];
89   EXPECT_FALSE(provider.WasEnabled(*notifiers.back()));
91   EXPECT_EQ(0, provider.closed_called_count());
92   controller.reset();
93   EXPECT_EQ(1, provider.closed_called_count());
96 TEST_F(CocoaTest, SingleProfile) {
97   // Notifiers are owned by settings controller.
98   std::vector<Notifier*> notifiers;
99   notifiers.push_back(NewNotifier("id", "title", /*enabled=*/ true));
100   notifiers.push_back(NewNotifier("id2", "other title", /*enabled=*/ false));
102   FakeNotifierSettingsProvider provider(notifiers);
104   base::scoped_nsobject<MCSettingsController> controller(
105       [[MCSettingsController alloc] initWithProvider:&provider
106                                   trayViewController:nil]);
107   [controller view];
109   EXPECT_EQ(0, [controller profileSwitcherListCount]);
112 TEST_F(CocoaTest, MultiProfile) {
113   FakeNotifierSettingsProvider provider;
114   std::vector<Notifier*> group1_notifiers;
115   group1_notifiers.push_back(NewNotifier("id", "title", /*enabled=*/ true));
116   group1_notifiers.push_back(NewNotifier("id2", "title2", /*enabled=*/ false));
117   provider.AddGroup(NewGroup("Group1", "GroupId1"), group1_notifiers);
118   std::vector<Notifier*> group2_notifiers;
119   group2_notifiers.push_back(NewNotifier("id3", "title3", /*enabled=*/ true));
120   group2_notifiers.push_back(NewNotifier("id4", "title4", /*enabled=*/ false));
121   group2_notifiers.push_back(NewNotifier("id5", "title5", /*enabled=*/ false));
122   provider.AddGroup(NewGroup("Group2", "GroupId2"), group2_notifiers);
124   base::scoped_nsobject<MCSettingsController> controller(
125       [[MCSettingsController alloc] initWithProvider:&provider
126                                   trayViewController:nil]);
127   [controller view];
129   EXPECT_EQ(2, [controller profileSwitcherListCount]);
132 TEST_F(CocoaTest, LearnMoreButton) {
133   std::vector<Notifier*> notifiers;
134   notifiers.push_back(NewNotifier("id", "title", /*enabled=*/ true));
135   notifiers.push_back(NewNotifier("id2", "title2", /*enabled=*/ false));
137   FakeNotifierSettingsProvider provider(notifiers);
138   EXPECT_EQ(0u, provider.settings_requested_count());
139   NotifierId has_settings_handler_notifier =
140       NotifierId(NotifierId::APPLICATION, "id2");
141   provider.SetNotifierHasAdvancedSettings(has_settings_handler_notifier);
143   base::scoped_nsobject<MCSettingsController> controller(
144       [[MCSettingsController alloc] initWithProvider:&provider
145                                   trayViewController:nil]);
146   [controller view];
148   [[controller bottomMostButton] clickLearnMore];
150   EXPECT_EQ(1u, provider.settings_requested_count());
153 }  // namespace message_center