• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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/ui/toolbar/wrench_menu_model.h"
6 
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/test/browser_with_test_window_test.h"
10 #include "chrome/test/menu_model_test.h"
11 #include "chrome/test/testing_profile.h"
12 #include "grit/generated_resources.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 class WrenchMenuModelTest : public BrowserWithTestWindowTest,
16                             public ui::AcceleratorProvider {
17  public:
18   // Don't handle accelerators.
GetAcceleratorForCommandId(int command_id,ui::Accelerator * accelerator)19   virtual bool GetAcceleratorForCommandId(
20       int command_id,
21       ui::Accelerator* accelerator) { return false; }
22 };
23 
24 // Copies parts of MenuModelTest::Delegate and combines them with the
25 // WrenchMenuModel since WrenchMenuModel is now a SimpleMenuModel::Delegate and
26 // not derived from SimpleMenuModel.
27 class TestWrenchMenuModel : public WrenchMenuModel {
28  public:
TestWrenchMenuModel(ui::AcceleratorProvider * provider,Browser * browser)29   TestWrenchMenuModel(ui::AcceleratorProvider* provider,
30                       Browser* browser)
31       : WrenchMenuModel(provider, browser),
32         execute_count_(0),
33         checked_count_(0),
34         enable_count_(0) {
35   }
36 
37   // Testing overrides to ui::SimpleMenuModel::Delegate:
IsCommandIdChecked(int command_id) const38   virtual bool IsCommandIdChecked(int command_id) const {
39     bool val = WrenchMenuModel::IsCommandIdChecked(command_id);
40     if (val)
41       checked_count_++;
42     return val;
43   }
44 
IsCommandIdEnabled(int command_id) const45   virtual bool IsCommandIdEnabled(int command_id) const {
46     ++enable_count_;
47     return true;
48   }
49 
ExecuteCommand(int command_id)50   virtual void ExecuteCommand(int command_id) { ++execute_count_; }
51 
52   int execute_count_;
53   mutable int checked_count_;
54   mutable int enable_count_;
55 };
56 
TEST_F(WrenchMenuModelTest,Basics)57 TEST_F(WrenchMenuModelTest, Basics) {
58   TestWrenchMenuModel model(this, browser());
59   int itemCount = model.GetItemCount();
60 
61   // Verify it has items. The number varies by platform, so we don't check
62   // the exact number.
63   EXPECT_GT(itemCount, 10);
64 
65   // Execute a couple of the items and make sure it gets back to our delegate.
66   // We can't use CountEnabledExecutable() here because the encoding menu's
67   // delegate is internal, it doesn't use the one we pass in.
68   model.ActivatedAt(0);
69   EXPECT_TRUE(model.IsEnabledAt(0));
70   // Make sure to use the index that is not separator in all configurations.
71   model.ActivatedAt(2);
72   EXPECT_TRUE(model.IsEnabledAt(2));
73   EXPECT_EQ(model.execute_count_, 2);
74   EXPECT_EQ(model.enable_count_, 2);
75 
76   model.execute_count_ = 0;
77   model.enable_count_ = 0;
78 
79   // Choose something from the tools submenu and make sure it makes it back to
80   // the delegate as well. Use the first submenu as the tools one.
81   int toolsModelIndex = -1;
82   for (int i = 0; i < itemCount; ++i) {
83     if (model.GetTypeAt(i) == ui::MenuModel::TYPE_SUBMENU) {
84       toolsModelIndex = i;
85       break;
86     }
87   }
88   EXPECT_GT(toolsModelIndex, -1);
89   ui::MenuModel* toolsModel = model.GetSubmenuModelAt(toolsModelIndex);
90   EXPECT_TRUE(toolsModel);
91   EXPECT_GT(toolsModel->GetItemCount(), 2);
92   toolsModel->ActivatedAt(2);
93   EXPECT_TRUE(toolsModel->IsEnabledAt(2));
94   EXPECT_EQ(model.execute_count_, 1);
95   EXPECT_EQ(model.enable_count_, 1);
96 }
97 
98 class EncodingMenuModelTest : public BrowserWithTestWindowTest,
99                               public MenuModelTest {
100 };
101 
TEST_F(EncodingMenuModelTest,IsCommandIdCheckedWithNoTabs)102 TEST_F(EncodingMenuModelTest, IsCommandIdCheckedWithNoTabs) {
103   EncodingMenuModel model(browser());
104   ASSERT_EQ(NULL, browser()->GetSelectedTabContents());
105   EXPECT_FALSE(model.IsCommandIdChecked(IDC_ENCODING_ISO88591));
106 }
107