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