summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/app_menu_model_unittest.cc52
-rw-r--r--chrome/browser/page_menu_model_unittest.cc42
-rw-r--r--chrome/browser/tab_menu_model_unittest.cc50
-rwxr-xr-xchrome/chrome_tests.gypi2
-rw-r--r--chrome/test/menu_model_test.cc32
-rw-r--r--chrome/test/menu_model_test.h47
6 files changed, 104 insertions, 121 deletions
diff --git a/chrome/browser/app_menu_model_unittest.cc b/chrome/browser/app_menu_model_unittest.cc
index 1200c63..f4d46d1 100644
--- a/chrome/browser/app_menu_model_unittest.cc
+++ b/chrome/browser/app_menu_model_unittest.cc
@@ -6,55 +6,15 @@
#include "base/logging.h"
#include "chrome/test/browser_with_test_window_test.h"
+#include "chrome/test/menu_model_test.h"
#include "testing/gtest/include/gtest/gtest.h"
-// A menu delegate that counts the number of times certain things are called
-// to make sure things are hooked up properly.
-class Delegate : public menus::SimpleMenuModel::Delegate {
- public:
- Delegate() : execute_count_(0), enable_count_(0) { }
-
- virtual bool IsCommandIdChecked(int command_id) const { return false; }
- virtual bool IsCommandIdEnabled(int command_id) const {
- ++enable_count_;
- return true;
- }
- virtual bool GetAcceleratorForCommandId(
- int command_id,
- menus::Accelerator* accelerator) { return false; }
- virtual void ExecuteCommand(int command_id) { ++execute_count_; }
-
- int execute_count_;
- mutable int enable_count_;
-};
-
-class AppMenuModelTest : public BrowserWithTestWindowTest {
+class AppMenuModelTest : public BrowserWithTestWindowTest,
+ public MenuModelTest {
};
-// Recursively checks the enabled state and executes a command on every item
-// that's not a separator or a submenu parent item. The returned count should
-// match the number of times the delegate is called to ensure every item works.
-static void CountEnabledExecutable(menus::MenuModel* model, int* count) {
- for (int i = 0; i < model->GetItemCount(); ++i) {
- menus::MenuModel::ItemType type = model->GetTypeAt(i);
- switch (type) {
- case menus::MenuModel::TYPE_SEPARATOR:
- continue;
- case menus::MenuModel::TYPE_SUBMENU:
- CountEnabledExecutable(model->GetSubmenuModelAt(i), count);
- break;
- default:
- model->IsEnabledAt(i); // Check if it's enabled (ignore answer).
- model->ActivatedAt(i); // Execute it.
- (*count)++; // Increment the count of executable items seen.
- break;
- }
- }
-}
-
TEST_F(AppMenuModelTest, Basics) {
- Delegate delegate;
- AppMenuModel model(&delegate, browser());
+ AppMenuModel model(&delegate_, browser());
// Verify it has items. The number varies by platform, so we don't check
// the exact number.
@@ -63,6 +23,6 @@ TEST_F(AppMenuModelTest, Basics) {
int item_count = 0;
CountEnabledExecutable(&model, &item_count);
EXPECT_GT(item_count, 0);
- EXPECT_EQ(item_count, delegate.execute_count_);
- EXPECT_EQ(item_count, delegate.enable_count_);
+ EXPECT_EQ(item_count, delegate_.execute_count_);
+ EXPECT_EQ(item_count, delegate_.enable_count_);
}
diff --git a/chrome/browser/page_menu_model_unittest.cc b/chrome/browser/page_menu_model_unittest.cc
index e4a08ee..54eb017 100644
--- a/chrome/browser/page_menu_model_unittest.cc
+++ b/chrome/browser/page_menu_model_unittest.cc
@@ -6,50 +6,32 @@
#include "base/logging.h"
#include "chrome/test/browser_with_test_window_test.h"
+#include "chrome/test/menu_model_test.h"
#include "testing/gtest/include/gtest/gtest.h"
-// A menu delegate that counts the number of times certain things are called
-// to make sure things are hooked up properly.
-class Delegate : public menus::SimpleMenuModel::Delegate {
- public:
- Delegate() : execute_count_(0), enable_count_(0) { }
-
- virtual bool IsCommandIdChecked(int command_id) const { return false; }
- virtual bool IsCommandIdEnabled(int command_id) const {
- ++enable_count_;
- return true;
- }
- virtual bool GetAcceleratorForCommandId(
- int command_id,
- menus::Accelerator* accelerator) { return false; }
- virtual void ExecuteCommand(int command_id) { ++execute_count_; }
-
- int execute_count_;
- mutable int enable_count_;
-};
-
-class PageMenuModelTest : public BrowserWithTestWindowTest {
- public:
+class PageMenuModelTest : public BrowserWithTestWindowTest,
+ public MenuModelTest {
};
TEST_F(PageMenuModelTest, Basics) {
- Delegate delegate;
- PageMenuModel model(&delegate, browser());
+ PageMenuModel model(&delegate_, browser());
// Verify it has items. The number varies by platform, so we don't check
// the exact number.
EXPECT_GT(model.GetItemCount(), 10);
// Execute a couple of the items and make sure it gets back to our delegate.
+ // We can't use CountEnabledExecutable() here because the encoding menu's
+ // delegate is internal, it doesn't use the one we pass in.
model.ActivatedAt(0);
EXPECT_TRUE(model.IsEnabledAt(0));
model.ActivatedAt(3);
EXPECT_TRUE(model.IsEnabledAt(3));
- EXPECT_EQ(delegate.execute_count_, 2);
- EXPECT_EQ(delegate.enable_count_, 2);
+ EXPECT_EQ(delegate_.execute_count_, 2);
+ EXPECT_EQ(delegate_.enable_count_, 2);
- delegate.execute_count_ = 0;
- delegate.enable_count_ = 0;
+ delegate_.execute_count_ = 0;
+ delegate_.enable_count_ = 0;
// Choose something from the zoom submenu and make sure it makes it back to
// the delegate as well.
@@ -58,6 +40,6 @@ TEST_F(PageMenuModelTest, Basics) {
EXPECT_GT(zoomModel->GetItemCount(), 1);
zoomModel->ActivatedAt(1);
EXPECT_TRUE(zoomModel->IsEnabledAt(1));
- EXPECT_EQ(delegate.execute_count_, 1);
- EXPECT_EQ(delegate.enable_count_, 1);
+ EXPECT_EQ(delegate_.execute_count_, 1);
+ EXPECT_EQ(delegate_.enable_count_, 1);
}
diff --git a/chrome/browser/tab_menu_model_unittest.cc b/chrome/browser/tab_menu_model_unittest.cc
index eb9aa65..3e0fdd3 100644
--- a/chrome/browser/tab_menu_model_unittest.cc
+++ b/chrome/browser/tab_menu_model_unittest.cc
@@ -5,56 +5,16 @@
#include "chrome/browser/tab_menu_model.h"
#include "base/logging.h"
+#include "chrome/test/menu_model_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
-// A menu delegate that counts the number of times certain things are called
-// to make sure things are hooked up properly.
-class Delegate : public menus::SimpleMenuModel::Delegate {
- public:
- Delegate() : execute_count_(0), enable_count_(0) { }
- virtual bool IsCommandIdChecked(int command_id) const { return false; }
- virtual bool IsCommandIdEnabled(int command_id) const {
- ++enable_count_;
- return true;
- }
- virtual bool GetAcceleratorForCommandId(
- int command_id,
- menus::Accelerator* accelerator) { return false; }
- virtual void ExecuteCommand(int command_id) { ++execute_count_; }
-
- int execute_count_;
- mutable int enable_count_;
-};
-
-class TabMenuModelTest : public PlatformTest {
+class TabMenuModelTest : public PlatformTest, public MenuModelTest {
};
-// Recursively checks the enabled state and executes a command on every item
-// that's not a separator or a submenu parent item. The returned count should
-// match the number of times the delegate is called to ensure every item works.
-static void CountEnabledExecutable(menus::MenuModel* model, int* count) {
- for (int i = 0; i < model->GetItemCount(); ++i) {
- menus::MenuModel::ItemType type = model->GetTypeAt(i);
- switch (type) {
- case menus::MenuModel::TYPE_SEPARATOR:
- continue;
- case menus::MenuModel::TYPE_SUBMENU:
- CountEnabledExecutable(model->GetSubmenuModelAt(i), count);
- break;
- default:
- model->IsEnabledAt(i); // Check if it's enabled (ignore answer).
- model->ActivatedAt(i); // Execute it.
- (*count)++; // Increment the count of executable items seen.
- break;
- }
- }
-}
-
TEST_F(TabMenuModelTest, Basics) {
- Delegate delegate;
- TabMenuModel model(&delegate);
+ TabMenuModel model(&delegate_);
// Verify it has items. The number varies by platform, so we don't check
// the exact number.
@@ -63,6 +23,6 @@ TEST_F(TabMenuModelTest, Basics) {
int item_count = 0;
CountEnabledExecutable(&model, &item_count);
EXPECT_GT(item_count, 0);
- EXPECT_EQ(item_count, delegate.execute_count_);
- EXPECT_EQ(item_count, delegate.enable_count_);
+ EXPECT_EQ(item_count, delegate_.execute_count_);
+ EXPECT_EQ(item_count, delegate_.enable_count_);
}
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi
index c25bf49..e51bb02 100755
--- a/chrome/chrome_tests.gypi
+++ b/chrome/chrome_tests.gypi
@@ -834,6 +834,8 @@
'test/browser_with_test_window_test.h',
'test/file_test_utils.cc',
'test/file_test_utils.h',
+ 'test/menu_model_test.cc',
+ 'test/menu_model_test.h',
'test/render_view_test.cc',
'test/render_view_test.h',
'test/sync/test_http_bridge_factory.h',
diff --git a/chrome/test/menu_model_test.cc b/chrome/test/menu_model_test.cc
new file mode 100644
index 0000000..2ea2d0d
--- /dev/null
+++ b/chrome/test/menu_model_test.cc
@@ -0,0 +1,32 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
+// source code is governed by a BSD-style license that can be found in the
+// LICENSE file.
+
+#include "chrome/test/menu_model_test.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+// Recursively checks the enabled state and executes a command on every item
+// that's not a separator or a submenu parent item. The returned count should
+// match the number of times the delegate is called to ensure every item works.
+void MenuModelTest::CountEnabledExecutable(menus::MenuModel* model,
+ int* count) {
+ for (int i = 0; i < model->GetItemCount(); ++i) {
+ menus::MenuModel::ItemType type = model->GetTypeAt(i);
+ switch (type) {
+ case menus::MenuModel::TYPE_SEPARATOR:
+ continue;
+ case menus::MenuModel::TYPE_SUBMENU:
+ CountEnabledExecutable(model->GetSubmenuModelAt(i), count);
+ break;
+ case menus::MenuModel::TYPE_COMMAND:
+ case menus::MenuModel::TYPE_CHECK:
+ case menus::MenuModel::TYPE_RADIO:
+ model->IsEnabledAt(i); // Check if it's enabled (ignore answer).
+ model->ActivatedAt(i); // Execute it.
+ (*count)++; // Increment the count of executable items seen.
+ break;
+ default:
+ FAIL(); // Ensure every case is tested.
+ }
+ }
+}
diff --git a/chrome/test/menu_model_test.h b/chrome/test/menu_model_test.h
new file mode 100644
index 0000000..06eaf24
--- /dev/null
+++ b/chrome/test/menu_model_test.h
@@ -0,0 +1,47 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
+// source code is governed by a BSD-style license that can be found in the
+// LICENSE file.
+
+#ifndef CHROME_TEST_MENU_MODEL_TEST_H_
+#define CHROME_TEST_MENU_MODEL_TEST_H_
+
+#include "app/menus/simple_menu_model.h"
+
+// A mix-in class to be used in addition to something that derrives from
+// testing::Test to provide some extra functionality for testing menu models.
+class MenuModelTest {
+ public:
+ MenuModelTest() { }
+ virtual ~MenuModelTest() { }
+
+ protected:
+ // A menu delegate that counts the number of times certain things are called
+ // to make sure things are hooked up properly.
+ class Delegate : public menus::SimpleMenuModel::Delegate {
+ public:
+ Delegate() : execute_count_(0), enable_count_(0) { }
+
+ virtual bool IsCommandIdChecked(int command_id) const { return false; }
+ virtual bool IsCommandIdEnabled(int command_id) const {
+ ++enable_count_;
+ return true;
+ }
+ virtual bool GetAcceleratorForCommandId(
+ int command_id,
+ menus::Accelerator* accelerator) { return false; }
+ virtual void ExecuteCommand(int command_id) { ++execute_count_; }
+
+ int execute_count_;
+ mutable int enable_count_;
+ };
+
+ // Recursively checks the enabled state and executes a command on every item
+ // that's not a separator or a submenu parent item. The returned count should
+ // match the number of times the delegate is called to ensure every item
+ // works.
+ void CountEnabledExecutable(menus::MenuModel* model, int* count);
+
+ Delegate delegate_;
+};
+
+#endif // CHROME_TEST_MENU_MODEL_TEST_H_