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 gfx::Image test_image2(*rb.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON_PRESSED)); 80 ui::Accelerator accel_arg; 81 gfx::Image image_arg; 82 83 EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg)); 84 EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg)); 85 EXPECT_FALSE(menu_model()->IsItemForCommandIdDynamic(0)); 86 87 // Set the accelerator and label for the first menu item. 88 menu_model()->SetAcceleratorForCommandId(0, &test_accel); 89 EXPECT_TRUE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg)); 90 EXPECT_EQ(test_accel, accel_arg); 91 92 // Try setting label and changing it. Also ensure that menu item is marked 93 // dynamic since the label has changed. 94 menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label1")); 95 EXPECT_TRUE(menu_model()->IsItemForCommandIdDynamic(0)); 96 EXPECT_EQ(ASCIIToUTF16("label1"), menu_model()->GetLabelForCommandId(0)); 97 menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label2")); 98 EXPECT_EQ(ASCIIToUTF16("label2"), menu_model()->GetLabelForCommandId(0)); 99 100 // Set the sublabel and icon image for the second menu item. 101 menu_model()->ChangeSublabelForCommandId(1, ASCIIToUTF16("sublabel")); 102 EXPECT_EQ(ASCIIToUTF16("sublabel"), menu_model()->GetSublabelForCommandId(1)); 103 104 // Try setting icon image and changing it. 105 menu_model()->ChangeIconForCommandId(1, test_image1); 106 EXPECT_TRUE(menu_model()->GetIconForCommandId(1, &image_arg)); 107 EXPECT_EQ(image_arg.ToImageSkia(), test_image1.ToImageSkia()); 108 menu_model()->ChangeIconForCommandId(1, test_image2); 109 EXPECT_TRUE(menu_model()->GetIconForCommandId(1, &image_arg)); 110 EXPECT_EQ(image_arg.ToImageSkia(), test_image2.ToImageSkia()); 111 112 // Ensure changes to one menu item does not affect the other menu item. 113 EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(1, &accel_arg)); 114 EXPECT_EQ(base::string16(), menu_model()->GetLabelForCommandId(1)); 115 EXPECT_EQ(base::string16(), menu_model()->GetSublabelForCommandId(0)); 116 EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg)); 117 118 // Menu state should have changed 8 times in this test. 119 EXPECT_EQ(8, changed_count()); 120 } 121