Home | History | Annotate | Download | only in status_icons
      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 "chrome/browser/status_icons/status_icon_menu_model.h"
      6 
      7 #include "base/compiler_specific.h"
      8 #include "base/strings/string_util.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "chrome/browser/status_icons/status_icon.h"
     11 #include "chrome/browser/status_icons/status_tray.h"
     12 #include "grit/chrome_unscaled_resources.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "ui/base/accelerators/accelerator.h"
     15 #include "ui/base/resource/resource_bundle.h"
     16 #include "ui/gfx/image/image.h"
     17 
     18 using base::ASCIIToUTF16;
     19 
     20 class StatusIconMenuModelTest : public testing::Test,
     21                                 public StatusIconMenuModel::Observer {
     22  public:
     23   virtual void SetUp() OVERRIDE {
     24     menu_.reset(new StatusIconMenuModel(NULL));
     25     menu_->AddObserver(this);
     26     changed_count_ = 0;
     27   }
     28 
     29   virtual void TearDown() OVERRIDE {
     30     menu_->RemoveObserver(this);
     31   }
     32 
     33   virtual int changed_count() {
     34     return changed_count_;
     35   }
     36 
     37   StatusIconMenuModel* menu_model() {
     38     return menu_.get();
     39   }
     40 
     41  private:
     42   virtual void OnMenuStateChanged() OVERRIDE {
     43     ++changed_count_;
     44   }
     45 
     46   scoped_ptr<StatusIconMenuModel> menu_;
     47   int changed_count_;
     48 };
     49 
     50 TEST_F(StatusIconMenuModelTest, ToggleBooleanProperties) {
     51   menu_model()->AddItem(0, ASCIIToUTF16("foo"));
     52 
     53   menu_model()->SetCommandIdChecked(0, true);
     54   EXPECT_TRUE(menu_model()->IsCommandIdChecked(0));
     55   menu_model()->SetCommandIdChecked(0, false);
     56   EXPECT_FALSE(menu_model()->IsCommandIdChecked(0));
     57 
     58   menu_model()->SetCommandIdEnabled(0, true);
     59   EXPECT_TRUE(menu_model()->IsCommandIdEnabled(0));
     60   menu_model()->SetCommandIdEnabled(0, false);
     61   EXPECT_FALSE(menu_model()->IsCommandIdEnabled(0));
     62 
     63   menu_model()->SetCommandIdVisible(0, true);
     64   EXPECT_TRUE(menu_model()->IsCommandIdVisible(0));
     65   menu_model()->SetCommandIdVisible(0, false);
     66   EXPECT_FALSE(menu_model()->IsCommandIdVisible(0));
     67 
     68   // Menu state should have changed 7 times in this test.
     69   EXPECT_EQ(7, changed_count());
     70 }
     71 
     72 TEST_F(StatusIconMenuModelTest, SetProperties) {
     73   menu_model()->AddItem(0, ASCIIToUTF16("foo1"));
     74   menu_model()->AddItem(1, ASCIIToUTF16("foo2"));
     75 
     76   ui::Accelerator test_accel(ui::VKEY_A, ui::EF_NONE);
     77   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
     78   gfx::Image test_image1(*rb.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON));
     79   ui::Accelerator accel_arg;
     80   gfx::Image image_arg;
     81 
     82   EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg));
     83   EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg));
     84   EXPECT_FALSE(menu_model()->IsItemForCommandIdDynamic(0));
     85 
     86   // Set the accelerator and label for the first menu item.
     87   menu_model()->SetAcceleratorForCommandId(0, &test_accel);
     88   EXPECT_TRUE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg));
     89   EXPECT_EQ(test_accel, accel_arg);
     90 
     91   // Try setting label and changing it. Also ensure that menu item is marked
     92   // dynamic since the label has changed.
     93   menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label1"));
     94   EXPECT_TRUE(menu_model()->IsItemForCommandIdDynamic(0));
     95   EXPECT_EQ(ASCIIToUTF16("label1"), menu_model()->GetLabelForCommandId(0));
     96   menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label2"));
     97   EXPECT_EQ(ASCIIToUTF16("label2"), menu_model()->GetLabelForCommandId(0));
     98 
     99   // Set the sublabel and icon image for the second menu item.
    100   menu_model()->ChangeSublabelForCommandId(1, ASCIIToUTF16("sublabel"));
    101   EXPECT_EQ(ASCIIToUTF16("sublabel"), menu_model()->GetSublabelForCommandId(1));
    102 
    103   // Try setting icon image and changing it.
    104   menu_model()->ChangeIconForCommandId(1, test_image1);
    105   EXPECT_TRUE(menu_model()->GetIconForCommandId(1, &image_arg));
    106   EXPECT_EQ(image_arg.ToImageSkia(), test_image1.ToImageSkia());
    107 
    108   // Ensure changes to one menu item does not affect the other menu item.
    109   EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(1, &accel_arg));
    110   EXPECT_EQ(base::string16(), menu_model()->GetLabelForCommandId(1));
    111   EXPECT_EQ(base::string16(), menu_model()->GetSublabelForCommandId(0));
    112   EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg));
    113 
    114   // Menu state should have changed 7 times in this test.
    115   EXPECT_EQ(7, changed_count());
    116 }
    117