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.
5 #include "chrome/browser/status_icons/status_icon_menu_model.h"
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"
18 class StatusIconMenuModelTest : public testing::Test,
19 public StatusIconMenuModel::Observer {
21 virtual void SetUp() OVERRIDE {
22 menu_.reset(new StatusIconMenuModel(NULL));
23 menu_->AddObserver(this);
27 virtual void TearDown() OVERRIDE {
28 menu_->RemoveObserver(this);
31 virtual int changed_count() {
32 return changed_count_;
35 StatusIconMenuModel* menu_model() {
40 virtual void OnMenuStateChanged() OVERRIDE {
44 scoped_ptr<StatusIconMenuModel> menu_;
48 TEST_F(StatusIconMenuModelTest, ToggleBooleanProperties) {
49 menu_model()->AddItem(0, ASCIIToUTF16("foo"));
51 menu_model()->SetCommandIdChecked(0, true);
52 EXPECT_TRUE(menu_model()->IsCommandIdChecked(0));
53 menu_model()->SetCommandIdChecked(0, false);
54 EXPECT_FALSE(menu_model()->IsCommandIdChecked(0));
56 menu_model()->SetCommandIdEnabled(0, true);
57 EXPECT_TRUE(menu_model()->IsCommandIdEnabled(0));
58 menu_model()->SetCommandIdEnabled(0, false);
59 EXPECT_FALSE(menu_model()->IsCommandIdEnabled(0));
61 menu_model()->SetCommandIdVisible(0, true);
62 EXPECT_TRUE(menu_model()->IsCommandIdVisible(0));
63 menu_model()->SetCommandIdVisible(0, false);
64 EXPECT_FALSE(menu_model()->IsCommandIdVisible(0));
66 // Menu state should have changed 7 times in this test.
67 EXPECT_EQ(7, changed_count());
70 TEST_F(StatusIconMenuModelTest, SetProperties) {
71 menu_model()->AddItem(0, ASCIIToUTF16("foo1"));
72 menu_model()->AddItem(1, ASCIIToUTF16("foo2"));
74 ui::Accelerator test_accel(ui::VKEY_A, ui::EF_NONE);
75 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
76 gfx::Image test_image1(*rb.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON));
77 gfx::Image test_image2(*rb.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON_PRESSED));
78 ui::Accelerator accel_arg;
81 EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg));
82 EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg));
83 EXPECT_FALSE(menu_model()->IsItemForCommandIdDynamic(0));
85 // Set the accelerator and label for the first menu item.
86 menu_model()->SetAcceleratorForCommandId(0, &test_accel);
87 EXPECT_TRUE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg));
88 EXPECT_EQ(test_accel, accel_arg);
90 // Try setting label and changing it. Also ensure that menu item is marked
91 // dynamic since the label has changed.
92 menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label1"));
93 EXPECT_TRUE(menu_model()->IsItemForCommandIdDynamic(0));
94 EXPECT_EQ(ASCIIToUTF16("label1"), menu_model()->GetLabelForCommandId(0));
95 menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label2"));
96 EXPECT_EQ(ASCIIToUTF16("label2"), menu_model()->GetLabelForCommandId(0));
98 // Set the sublabel and icon image for the second menu item.
99 menu_model()->ChangeSublabelForCommandId(1, ASCIIToUTF16("sublabel"));
100 EXPECT_EQ(ASCIIToUTF16("sublabel"), menu_model()->GetSublabelForCommandId(1));
102 // Try setting icon image and changing it.
103 menu_model()->ChangeIconForCommandId(1, test_image1);
104 EXPECT_TRUE(menu_model()->GetIconForCommandId(1, &image_arg));
105 EXPECT_EQ(image_arg.ToImageSkia(), test_image1.ToImageSkia());
106 menu_model()->ChangeIconForCommandId(1, test_image2);
107 EXPECT_TRUE(menu_model()->GetIconForCommandId(1, &image_arg));
108 EXPECT_EQ(image_arg.ToImageSkia(), test_image2.ToImageSkia());
110 // Ensure changes to one menu item does not affect the other menu item.
111 EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(1, &accel_arg));
112 EXPECT_EQ(string16(), menu_model()->GetLabelForCommandId(1));
113 EXPECT_EQ(string16(), menu_model()->GetSublabelForCommandId(0));
114 EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg));
116 // Menu state should have changed 8 times in this test.
117 EXPECT_EQ(8, changed_count());