diff options
author | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 19:27:09 +0000 |
---|---|---|
committer | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 19:27:09 +0000 |
commit | b882d76636d5380e88904446c0c5d81a40cef9a1 (patch) | |
tree | c807aced8f268feac0ec1c86806095385209e28a /chrome/test | |
parent | 03e12a4defe3868fc1ed769925d887355b47309d (diff) | |
download | chromium_src-b882d76636d5380e88904446c0c5d81a40cef9a1.zip chromium_src-b882d76636d5380e88904446c0c5d81a40cef9a1.tar.gz chromium_src-b882d76636d5380e88904446c0c5d81a40cef9a1.tar.bz2 |
Refactor shared menu model testing code into a common baseclass
BUG=28977
TEST=unit tests still work
Review URL: http://codereview.chromium.org/504027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34735 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
-rw-r--r-- | chrome/test/menu_model_test.cc | 32 | ||||
-rw-r--r-- | chrome/test/menu_model_test.h | 47 |
2 files changed, 79 insertions, 0 deletions
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_ |