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/base/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 : 0;
     16 }
     17 
     18 - (NSUInteger)scrollViewItemCount {
     19   return [[[[self scrollView] documentView] subviews] count];
     20 }
     21 
     22 - (NSButton*)bottomMostButton {
     23   // The checkboxes are created bottom-to-top, so the first object is the
     24   // bottom-most.
     25   return [[[[self scrollView] documentView] subviews] objectAtIndex:0];
     26 }
     27 @end
     28 
     29 namespace message_center {
     30 
     31 using ui::CocoaTest;
     32 
     33 namespace {
     34 
     35 NotifierGroup* NewGroup(const std::string& name,
     36                         const std::string& login_info) {
     37   return new NotifierGroup(gfx::Image(),
     38                            base::UTF8ToUTF16(name),
     39                            base::UTF8ToUTF16(login_info),
     40                            true);
     41 }
     42 
     43 Notifier* NewNotifier(const std::string& id,
     44                       const std::string& title,
     45                       bool enabled) {
     46   NotifierId notifier_id(NotifierId::APPLICATION, id);
     47   return new Notifier(notifier_id, base::UTF8ToUTF16(title), enabled);
     48 }
     49 
     50 }  // namespace
     51 
     52 TEST_F(CocoaTest, Basic) {
     53   // Notifiers are owned by settings controller.
     54   std::vector<Notifier*> notifiers;
     55   notifiers.push_back(NewNotifier("id", "title", /*enabled=*/true));
     56   notifiers.push_back(NewNotifier("id2", "other title", /*enabled=*/false));
     57 
     58   FakeNotifierSettingsProvider provider(notifiers);
     59 
     60   base::scoped_nsobject<MCSettingsController> controller(
     61       [[MCSettingsController alloc] initWithProvider:&provider
     62                                   trayViewController:nil]);
     63   [controller view];
     64 
     65   EXPECT_EQ(notifiers.size(), [controller scrollViewItemCount]);
     66 }
     67 
     68 TEST_F(CocoaTest, Toggle) {
     69   // Notifiers are owned by settings controller.
     70   std::vector<Notifier*> notifiers;
     71   notifiers.push_back(NewNotifier("id", "title", /*enabled=*/true));
     72   notifiers.push_back(NewNotifier("id2", "other title", /*enabled=*/false));
     73 
     74   FakeNotifierSettingsProvider provider(notifiers);
     75 
     76   base::scoped_nsobject<MCSettingsController> controller(
     77       [[MCSettingsController alloc] initWithProvider:&provider
     78                                   trayViewController:nil]);
     79   [controller view];
     80 
     81   NSButton* toggleSecond = [controller bottomMostButton];
     82 
     83   [toggleSecond performClick:nil];
     84   EXPECT_TRUE(provider.WasEnabled(*notifiers.back()));
     85 
     86   [toggleSecond performClick:nil];
     87   EXPECT_FALSE(provider.WasEnabled(*notifiers.back()));
     88 
     89   EXPECT_EQ(0, provider.closed_called_count());
     90   controller.reset();
     91   EXPECT_EQ(1, provider.closed_called_count());
     92 }
     93 
     94 TEST_F(CocoaTest, SingleProfile) {
     95   // Notifiers are owned by settings controller.
     96   std::vector<Notifier*> notifiers;
     97   notifiers.push_back(NewNotifier("id", "title", /*enabled=*/true));
     98   notifiers.push_back(NewNotifier("id2", "other title", /*enabled=*/false));
     99 
    100   FakeNotifierSettingsProvider provider(notifiers);
    101 
    102   base::scoped_nsobject<MCSettingsController> controller(
    103       [[MCSettingsController alloc] initWithProvider:&provider
    104                                   trayViewController:nil]);
    105   [controller view];
    106 
    107   EXPECT_EQ(0, [controller profileSwitcherListCount]);
    108 }
    109 
    110 TEST_F(CocoaTest, MultiProfile) {
    111   FakeNotifierSettingsProvider provider;
    112   std::vector<Notifier*> group1_notifiers;
    113   group1_notifiers.push_back(NewNotifier("id", "title", /*enabled=*/true));
    114   group1_notifiers.push_back(NewNotifier("id2", "title2", /*enabled=*/false));
    115   provider.AddGroup(NewGroup("Group1", "GroupId1"), group1_notifiers);
    116   std::vector<Notifier*> group2_notifiers;
    117   group2_notifiers.push_back(NewNotifier("id3", "title3", /*enabled=*/true));
    118   group2_notifiers.push_back(NewNotifier("id4", "title4", /*enabled=*/false));
    119   group2_notifiers.push_back(NewNotifier("id5", "title5", /*enabled=*/false));
    120   provider.AddGroup(NewGroup("Group2", "GroupId2"), group2_notifiers);
    121 
    122   base::scoped_nsobject<MCSettingsController> controller(
    123       [[MCSettingsController alloc] initWithProvider:&provider
    124                                   trayViewController:nil]);
    125   [controller view];
    126 
    127   EXPECT_EQ(2, [controller profileSwitcherListCount]);
    128 }
    129 
    130 }  // namespace message_center
    131