Home | History | Annotate | Download | only in cocoa
      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.
      4 
      5 #import "ui/message_center/cocoa/settings_controller.h"
      6 
      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"
     10 
     11 @implementation MCSettingsController (TestingInterface)
     12 - (NSInteger)profileSwitcherListCount {
     13   // Subtract the dummy item.
     14   return [self groupDropDownButton]
     15                  ? [[self groupDropDownButton] numberOfItems] - 1
     16                  : 0;
     17 }
     18 
     19 - (NSUInteger)scrollViewItemCount {
     20   return [[[[self scrollView] documentView] subviews] count];
     21 }
     22 
     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];
     27 }
     28 @end
     29 
     30 namespace message_center {
     31 
     32 using ui::CocoaTest;
     33 
     34 namespace {
     35 
     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);
     42 }
     43 
     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);
     49 }
     50 
     51 }  // namespace
     52 
     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));
     58 
     59   FakeNotifierSettingsProvider provider(notifiers);
     60 
     61   base::scoped_nsobject<MCSettingsController> controller(
     62       [[MCSettingsController alloc] initWithProvider:&provider
     63                                   trayViewController:nil]);
     64   [controller view];
     65 
     66   EXPECT_EQ(notifiers.size(), [controller scrollViewItemCount]);
     67 }
     68 
     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));
     74 
     75   FakeNotifierSettingsProvider provider(notifiers);
     76 
     77   base::scoped_nsobject<MCSettingsController> controller(
     78       [[MCSettingsController alloc] initWithProvider:&provider
     79                                   trayViewController:nil]);
     80   [controller view];
     81 
     82   MCSettingsEntryView* toggleView = [controller bottomMostButton];
     83   NSButton* toggleSecond = [toggleView checkbox];
     84 
     85   [toggleSecond performClick:nil];
     86   EXPECT_TRUE(provider.WasEnabled(*notifiers.back()));
     87 
     88   [toggleSecond performClick:nil];
     89   EXPECT_FALSE(provider.WasEnabled(*notifiers.back()));
     90 
     91   EXPECT_EQ(0, provider.closed_called_count());
     92   controller.reset();
     93   EXPECT_EQ(1, provider.closed_called_count());
     94 }
     95 
     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));
    101 
    102   FakeNotifierSettingsProvider provider(notifiers);
    103 
    104   base::scoped_nsobject<MCSettingsController> controller(
    105       [[MCSettingsController alloc] initWithProvider:&provider
    106                                   trayViewController:nil]);
    107   [controller view];
    108 
    109   EXPECT_EQ(0, [controller profileSwitcherListCount]);
    110 }
    111 
    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);
    123 
    124   base::scoped_nsobject<MCSettingsController> controller(
    125       [[MCSettingsController alloc] initWithProvider:&provider
    126                                   trayViewController:nil]);
    127   [controller view];
    128 
    129   EXPECT_EQ(2, [controller profileSwitcherListCount]);
    130 }
    131 
    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));
    136 
    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);
    142 
    143   base::scoped_nsobject<MCSettingsController> controller(
    144       [[MCSettingsController alloc] initWithProvider:&provider
    145                                   trayViewController:nil]);
    146   [controller view];
    147 
    148   [[controller bottomMostButton] clickLearnMore];
    149 
    150   EXPECT_EQ(1u, provider.settings_requested_count());
    151 }
    152 
    153 }  // namespace message_center
    154