Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 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/test/base/menu_model_test.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 
      9 bool MenuModelTest::Delegate::IsCommandIdChecked(int command_id) const {
     10   return false;
     11 }
     12 
     13 bool MenuModelTest::Delegate::IsCommandIdEnabled(int command_id) const {
     14   ++enable_count_;
     15   return true;
     16 }
     17 
     18 bool MenuModelTest::Delegate::GetAcceleratorForCommandId(
     19     int command_id,
     20     ui::Accelerator* accelerator) {
     21   return false;
     22 }
     23 
     24 void MenuModelTest::Delegate::ExecuteCommand(int command_id, int event_flags) {
     25   ++execute_count_;
     26 }
     27 
     28 // Recursively checks the enabled state and executes a command on every item
     29 // that's not a separator or a submenu parent item. The returned count should
     30 // match the number of times the delegate is called to ensure every item works.
     31 void MenuModelTest::CountEnabledExecutable(ui::MenuModel* model,
     32                                            int* count) {
     33   for (int i = 0; i < model->GetItemCount(); ++i) {
     34     ui::MenuModel::ItemType type = model->GetTypeAt(i);
     35     switch (type) {
     36       case ui::MenuModel::TYPE_SEPARATOR:
     37         continue;
     38       case ui::MenuModel::TYPE_SUBMENU:
     39         CountEnabledExecutable(model->GetSubmenuModelAt(i), count);
     40         break;
     41       case ui::MenuModel::TYPE_COMMAND:
     42       case ui::MenuModel::TYPE_CHECK:
     43       case ui::MenuModel::TYPE_RADIO:
     44         model->IsEnabledAt(i);  // Check if it's enabled (ignore answer).
     45         model->ActivatedAt(i);  // Execute it.
     46         (*count)++;  // Increment the count of executable items seen.
     47         break;
     48       default:
     49         FAIL();  // Ensure every case is tested.
     50     }
     51   }
     52 }
     53