shelf_application_menu_model_unittest.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2015 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. #include "ash/shelf/shelf_application_menu_model.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "base/test/metrics/histogram_tester.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace ash {
  11. namespace {
  12. const char kNumItemsEnabledHistogramName[] =
  13. "Ash.Shelf.Menu.NumItemsEnabledUponSelection";
  14. const char kSelectedMenuItemIndexHistogramName[] =
  15. "Ash.Shelf.Menu.SelectedMenuItemIndex";
  16. } // namespace
  17. // Test API to provide internal access to a ShelfApplicationMenuModel.
  18. class ShelfApplicationMenuModelTestAPI {
  19. public:
  20. // Creates a test api to access the internals of the |menu|.
  21. explicit ShelfApplicationMenuModelTestAPI(ShelfApplicationMenuModel* menu)
  22. : menu_(menu) {}
  23. ShelfApplicationMenuModelTestAPI(const ShelfApplicationMenuModelTestAPI&) =
  24. delete;
  25. ShelfApplicationMenuModelTestAPI& operator=(
  26. const ShelfApplicationMenuModelTestAPI&) = delete;
  27. ~ShelfApplicationMenuModelTestAPI() = default;
  28. // Give public access to this metrics recording functions.
  29. void RecordMenuItemSelectedMetrics(int command_id,
  30. int num_menu_items_enabled) {
  31. menu_->RecordMenuItemSelectedMetrics(command_id, num_menu_items_enabled);
  32. }
  33. private:
  34. // The ShelfApplicationMenuModel to provide internal access to. Not owned.
  35. ShelfApplicationMenuModel* menu_;
  36. };
  37. // Verifies the menu contents given an empty item list.
  38. TEST(ShelfApplicationMenuModelTest, VerifyContentsWithNoMenuItems) {
  39. std::u16string title = u"title";
  40. ShelfApplicationMenuModel menu(title, {}, nullptr);
  41. // Expect the title and a separator.
  42. ASSERT_EQ(2u, menu.GetItemCount());
  43. EXPECT_EQ(ui::MenuModel::TYPE_TITLE, menu.GetTypeAt(0));
  44. EXPECT_EQ(title, menu.GetLabelAt(0));
  45. EXPECT_FALSE(menu.IsEnabledAt(0));
  46. EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(1));
  47. }
  48. // Verifies the menu contents given a non-empty item list.
  49. TEST(ShelfApplicationMenuModelTest, VerifyContentsWithMenuItems) {
  50. ShelfApplicationMenuModel::Items items;
  51. std::u16string title1 = u"title1";
  52. std::u16string title2 = u"title2";
  53. std::u16string title3 = u"title3";
  54. items.push_back({static_cast<int>(items.size()), title1, gfx::ImageSkia()});
  55. items.push_back({static_cast<int>(items.size()), title2, gfx::ImageSkia()});
  56. items.push_back({static_cast<int>(items.size()), title3, gfx::ImageSkia()});
  57. std::u16string title = u"title";
  58. ShelfApplicationMenuModel menu(title, std::move(items), nullptr);
  59. ShelfApplicationMenuModelTestAPI menu_test_api(&menu);
  60. // Expect the title and the enabled items.
  61. ASSERT_EQ(5u, menu.GetItemCount());
  62. // The label title should not be enabled.
  63. EXPECT_EQ(ui::MenuModel::TYPE_TITLE, menu.GetTypeAt(0));
  64. EXPECT_EQ(title, menu.GetLabelAt(0));
  65. EXPECT_FALSE(menu.IsEnabledAt(0));
  66. EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(1));
  67. EXPECT_EQ(title1, menu.GetLabelAt(1));
  68. EXPECT_TRUE(menu.IsEnabledAt(1));
  69. EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(2));
  70. EXPECT_EQ(title2, menu.GetLabelAt(2));
  71. EXPECT_TRUE(menu.IsEnabledAt(2));
  72. EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(3));
  73. EXPECT_EQ(title3, menu.GetLabelAt(3));
  74. EXPECT_TRUE(menu.IsEnabledAt(3));
  75. }
  76. // Verifies RecordMenuItemSelectedMetrics uses the correct histogram buckets.
  77. TEST(ShelfApplicationMenuModelTest, VerifyHistogramBuckets) {
  78. const int kCommandId = 3;
  79. const int kNumMenuItemsEnabled = 7;
  80. base::HistogramTester histogram_tester;
  81. ShelfApplicationMenuModel menu(u"title", {}, nullptr);
  82. ShelfApplicationMenuModelTestAPI menu_test_api(&menu);
  83. menu_test_api.RecordMenuItemSelectedMetrics(kCommandId, kNumMenuItemsEnabled);
  84. histogram_tester.ExpectTotalCount(kNumItemsEnabledHistogramName, 1);
  85. histogram_tester.ExpectBucketCount(kNumItemsEnabledHistogramName,
  86. kNumMenuItemsEnabled, 1);
  87. histogram_tester.ExpectTotalCount(kSelectedMenuItemIndexHistogramName, 1);
  88. histogram_tester.ExpectBucketCount(kSelectedMenuItemIndexHistogramName,
  89. kCommandId, 1);
  90. }
  91. // Verify histogram data is recorded when ExecuteCommand is called.
  92. TEST(ShelfApplicationMenuModelTest, VerifyHistogramOnExecute) {
  93. base::HistogramTester histogram_tester;
  94. ShelfApplicationMenuModel::Items items(1);
  95. std::u16string title = u"title";
  96. ShelfApplicationMenuModel menu(title, std::move(items), nullptr);
  97. menu.ExecuteCommand(0, 0);
  98. histogram_tester.ExpectTotalCount(kNumItemsEnabledHistogramName, 1);
  99. histogram_tester.ExpectTotalCount(kSelectedMenuItemIndexHistogramName, 1);
  100. }
  101. } // namespace ash