1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// found in the LICENSE file. 4424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 5424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/status_icons/status_icon_menu_model.h" 6424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/compiler_specific.h" 8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/strings/string_util.h" 9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/status_icons/status_icon.h" 11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/status_icons/status_tray.h" 12424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "grit/chrome_unscaled_resources.h" 13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/base/accelerators/accelerator.h" 15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h" 16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/gfx/image/image.h" 17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::ASCIIToUTF16; 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class StatusIconMenuModelTest : public testing::Test, 21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) public StatusIconMenuModel::Observer { 22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) public: 23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual void SetUp() OVERRIDE { 24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_.reset(new StatusIconMenuModel(NULL)); 25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_->AddObserver(this); 26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) changed_count_ = 0; 27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual void TearDown() OVERRIDE { 30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_->RemoveObserver(this); 31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual int changed_count() { 34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return changed_count_; 35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) StatusIconMenuModel* menu_model() { 38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return menu_.get(); 39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) private: 42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual void OnMenuStateChanged() OVERRIDE { 43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ++changed_count_; 44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) scoped_ptr<StatusIconMenuModel> menu_; 47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int changed_count_; 48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}; 49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(StatusIconMenuModelTest, ToggleBooleanProperties) { 51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->AddItem(0, ASCIIToUTF16("foo")); 52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->SetCommandIdChecked(0, true); 54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_TRUE(menu_model()->IsCommandIdChecked(0)); 55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->SetCommandIdChecked(0, false); 56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_FALSE(menu_model()->IsCommandIdChecked(0)); 57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->SetCommandIdEnabled(0, true); 59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_TRUE(menu_model()->IsCommandIdEnabled(0)); 60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->SetCommandIdEnabled(0, false); 61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_FALSE(menu_model()->IsCommandIdEnabled(0)); 62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->SetCommandIdVisible(0, true); 64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_TRUE(menu_model()->IsCommandIdVisible(0)); 65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->SetCommandIdVisible(0, false); 66424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_FALSE(menu_model()->IsCommandIdVisible(0)); 67424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 68424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Menu state should have changed 7 times in this test. 69424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(7, changed_count()); 70424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 71424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 72424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TEST_F(StatusIconMenuModelTest, SetProperties) { 73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->AddItem(0, ASCIIToUTF16("foo1")); 74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->AddItem(1, ASCIIToUTF16("foo2")); 75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ui::Accelerator test_accel(ui::VKEY_A, ui::EF_NONE); 77424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 78424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Image test_image1(*rb.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON)); 79424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ui::Accelerator accel_arg; 80424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) gfx::Image image_arg; 81424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 82424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg)); 83424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg)); 84424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_FALSE(menu_model()->IsItemForCommandIdDynamic(0)); 85424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 86424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Set the accelerator and label for the first menu item. 87424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->SetAcceleratorForCommandId(0, &test_accel); 88424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_TRUE(menu_model()->GetAcceleratorForCommandId(0, &accel_arg)); 89424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(test_accel, accel_arg); 90424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Try setting label and changing it. Also ensure that menu item is marked 92424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // dynamic since the label has changed. 93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label1")); 94424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_TRUE(menu_model()->IsItemForCommandIdDynamic(0)); 95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(ASCIIToUTF16("label1"), menu_model()->GetLabelForCommandId(0)); 96424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->ChangeLabelForCommandId(0, ASCIIToUTF16("label2")); 97424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(ASCIIToUTF16("label2"), menu_model()->GetLabelForCommandId(0)); 98424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 99424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Set the sublabel and icon image for the second menu item. 100424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->ChangeSublabelForCommandId(1, ASCIIToUTF16("sublabel")); 101424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(ASCIIToUTF16("sublabel"), menu_model()->GetSublabelForCommandId(1)); 102424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 103424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Try setting icon image and changing it. 104424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) menu_model()->ChangeIconForCommandId(1, test_image1); 105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_TRUE(menu_model()->GetIconForCommandId(1, &image_arg)); 106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_EQ(image_arg.ToImageSkia(), test_image1.ToImageSkia()); 107424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 108424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Ensure changes to one menu item does not affect the other menu item. 109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_FALSE(menu_model()->GetAcceleratorForCommandId(1, &accel_arg)); 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(base::string16(), menu_model()->GetLabelForCommandId(1)); 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(base::string16(), menu_model()->GetSublabelForCommandId(0)); 112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) EXPECT_FALSE(menu_model()->GetIconForCommandId(0, &image_arg)); 113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Menu state should have changed 7 times in this test. 1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci EXPECT_EQ(7, changed_count()); 116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 117