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 using base::ASCIIToUTF16
;
20 class StatusIconMenuModelTest
: public testing::Test
,
21 public StatusIconMenuModel::Observer
{
23 virtual void SetUp() OVERRIDE
{
24 menu_
.reset(new StatusIconMenuModel(NULL
));
25 menu_
->AddObserver(this);
29 virtual void TearDown() OVERRIDE
{
30 menu_
->RemoveObserver(this);
33 virtual int changed_count() {
34 return changed_count_
;
37 StatusIconMenuModel
* menu_model() {
42 virtual void OnMenuStateChanged() OVERRIDE
{
46 scoped_ptr
<StatusIconMenuModel
> menu_
;
50 TEST_F(StatusIconMenuModelTest
, ToggleBooleanProperties
) {
51 menu_model()->AddItem(0, ASCIIToUTF16("foo"));
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));
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));
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));
68 // Menu state should have changed 7 times in this test.
69 EXPECT_EQ(7, changed_count());
72 TEST_F(StatusIconMenuModelTest
, SetProperties
) {
73 menu_model()->AddItem(0, ASCIIToUTF16("foo1"));
74 menu_model()->AddItem(1, ASCIIToUTF16("foo2"));
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
;
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));
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
);
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));
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));
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());
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
));
118 // Menu state should have changed 8 times in this test.
119 EXPECT_EQ(8, changed_count());