| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/app_menu_model.h" | 5 #include "chrome/browser/app_menu_model.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/test/browser_with_test_window_test.h" | 8 #include "chrome/test/browser_with_test_window_test.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 menus::Accelerator* accelerator) { return false; } | 24 menus::Accelerator* accelerator) { return false; } |
| 25 virtual void ExecuteCommand(int command_id) { ++execute_count_; } | 25 virtual void ExecuteCommand(int command_id) { ++execute_count_; } |
| 26 | 26 |
| 27 int execute_count_; | 27 int execute_count_; |
| 28 mutable int enable_count_; | 28 mutable int enable_count_; |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 class AppMenuModelTest : public BrowserWithTestWindowTest { | 31 class AppMenuModelTest : public BrowserWithTestWindowTest { |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 // Recursively checks the enabled state and executes a command on every item |
| 35 // that's not a separator or a submenu parent item. The returned count should |
| 36 // match the number of times the delegate is called to ensure every item works. |
| 37 static void CountEnabledExecutable(menus::MenuModel* model, int* count) { |
| 38 for (int i = 0; i < model->GetItemCount(); ++i) { |
| 39 menus::MenuModel::ItemType type = model->GetTypeAt(i); |
| 40 switch (type) { |
| 41 case menus::MenuModel::TYPE_SEPARATOR: |
| 42 continue; |
| 43 case menus::MenuModel::TYPE_SUBMENU: |
| 44 CountEnabledExecutable(model->GetSubmenuModelAt(i), count); |
| 45 break; |
| 46 default: |
| 47 model->IsEnabledAt(i); // Check if it's enabled (ignore answer). |
| 48 model->ActivatedAt(i); // Execute it. |
| 49 (*count)++; // Increment the count of executable items seen. |
| 50 break; |
| 51 } |
| 52 } |
| 53 } |
| 54 |
| 34 TEST_F(AppMenuModelTest, Basics) { | 55 TEST_F(AppMenuModelTest, Basics) { |
| 35 Delegate delegate; | 56 Delegate delegate; |
| 36 AppMenuModel model(&delegate, browser()); | 57 AppMenuModel model(&delegate, browser()); |
| 37 | 58 |
| 38 // Verify it has items. The number varies by platform, so we don't check | 59 // Verify it has items. The number varies by platform, so we don't check |
| 39 // the exact number. | 60 // the exact number. |
| 40 EXPECT_GT(model.GetItemCount(), 5); | 61 EXPECT_GT(model.GetItemCount(), 5); |
| 41 | 62 |
| 42 // Execute a couple of the items and make sure it gets back to our delegate. | 63 int item_count = 0; |
| 43 model.ActivatedAt(0); | 64 CountEnabledExecutable(&model, &item_count); |
| 44 EXPECT_TRUE(model.IsEnabledAt(0)); | 65 EXPECT_GT(item_count, 0); |
| 45 model.ActivatedAt(3); | 66 EXPECT_EQ(item_count, delegate.execute_count_); |
| 46 EXPECT_TRUE(model.IsEnabledAt(4)); | 67 EXPECT_EQ(item_count, delegate.enable_count_); |
| 47 EXPECT_EQ(delegate.execute_count_, 2); | |
| 48 EXPECT_EQ(delegate.enable_count_, 2); | |
| 49 | |
| 50 delegate.execute_count_ = 0; | |
| 51 delegate.enable_count_ = 0; | |
| 52 } | 68 } |
| OLD | NEW |