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 void SetUp() override
{
24 menu_
.reset(new StatusIconMenuModel(NULL
));
25 menu_
->AddObserver(this);
29 void TearDown() override
{ menu_
->RemoveObserver(this); }
31 virtual int changed_count() {
32 return changed_count_
;
35 StatusIconMenuModel
* menu_model() {
40 void OnMenuStateChanged() override
{ ++changed_count_
; }
42 scoped_ptr
<StatusIconMenuModel
> menu_
;
46 TEST_F(StatusIconMenuModelTest
, ToggleBooleanProperties
) {
47 menu_model()->AddItem(0, ASCIIToUTF16("foo"));
49 menu_model()->SetCommandIdChecked(0, true);
50 EXPECT_TRUE(menu_model()->IsCommandIdChecked(0));
51 menu_model()->SetCommandIdChecked(0, false);
52 EXPECT_FALSE(menu_model()->IsCommandIdChecked(0));
54 menu_model()->SetCommandIdEnabled(0, true);
55 EXPECT_TRUE(menu_model()->IsCommandIdEnabled(0));
56 menu_model()->SetCommandIdEnabled(0, false);
57 EXPECT_FALSE(menu_model()->IsCommandIdEnabled(0));
59 menu_model()->SetCommandIdVisible(0, true);
60 EXPECT_TRUE(menu_model()->IsCommandIdVisible(0));
61 menu_model()->SetCommandIdVisible(0, false);
62 EXPECT_FALSE(menu_model()->IsCommandIdVisible(0));
64 // Menu state should have changed 7 times in this test.
65 EXPECT_EQ(7, changed_count());
68 TEST_F(StatusIconMenuModelTest
, SetProperties
) {
69 menu_model()->AddItem(0, ASCIIToUTF16("foo1"));
70 menu_model()->AddItem(1, ASCIIToUTF16("foo2"));
72 ui::Accelerator
test_accel(ui::VKEY_A
, ui::EF_NONE
);
73 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
74 gfx::Image
test_image1(*rb
.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON
));
75 ui::Accelerator accel_arg
;
78 EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg
));
79 EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg
));
80 EXPECT_FALSE(menu_model()->IsItemForCommandIdDynamic(0));
82 // Set the accelerator and label for the first menu item.
83 menu_model()->SetAcceleratorForCommandId(0, &test_accel
);
84 EXPECT_TRUE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg
));
85 EXPECT_EQ(test_accel
, accel_arg
);
87 // Try setting label and changing it. Also ensure that menu item is marked
88 // dynamic since the label has changed.
89 menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label1"));
90 EXPECT_TRUE(menu_model()->IsItemForCommandIdDynamic(0));
91 EXPECT_EQ(ASCIIToUTF16("label1"), menu_model()->GetLabelForCommandId(0));
92 menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label2"));
93 EXPECT_EQ(ASCIIToUTF16("label2"), menu_model()->GetLabelForCommandId(0));
95 // Set the sublabel and icon image for the second menu item.
96 menu_model()->ChangeSublabelForCommandId(1, ASCIIToUTF16("sublabel"));
97 EXPECT_EQ(ASCIIToUTF16("sublabel"), menu_model()->GetSublabelForCommandId(1));
99 // Try setting icon image and changing it.
100 menu_model()->ChangeIconForCommandId(1, test_image1
);
101 EXPECT_TRUE(menu_model()->GetIconForCommandId(1, &image_arg
));
102 EXPECT_EQ(image_arg
.ToImageSkia(), test_image1
.ToImageSkia());
104 // Ensure changes to one menu item does not affect the other menu item.
105 EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(1, &accel_arg
));
106 EXPECT_EQ(base::string16(), menu_model()->GetLabelForCommandId(1));
107 EXPECT_EQ(base::string16(), menu_model()->GetSublabelForCommandId(0));
108 EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg
));
110 // Menu state should have changed 7 times in this test.
111 EXPECT_EQ(7, changed_count());