| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/common/shelf/shelf_application_menu_model.h" | 5 #include "ash/common/shelf/shelf_application_menu_model.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "ash/common/test/test_shelf_item_delegate.h" |
| 9 #include "ash/public/cpp/shelf_application_menu_item.h" | 10 #include "ash/public/cpp/shelf_application_menu_item.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace ash { | 17 namespace ash { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 42 private: | 43 private: |
| 43 // The ShelfApplicationMenuModel to provide internal access to. Not owned. | 44 // The ShelfApplicationMenuModel to provide internal access to. Not owned. |
| 44 ShelfApplicationMenuModel* menu_; | 45 ShelfApplicationMenuModel* menu_; |
| 45 | 46 |
| 46 DISALLOW_COPY_AND_ASSIGN(ShelfApplicationMenuModelTestAPI); | 47 DISALLOW_COPY_AND_ASSIGN(ShelfApplicationMenuModelTestAPI); |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 // Verifies the menu contents given an empty item list. | 50 // Verifies the menu contents given an empty item list. |
| 50 TEST(ShelfApplicationMenuModelTest, VerifyContentsWithNoMenuItems) { | 51 TEST(ShelfApplicationMenuModelTest, VerifyContentsWithNoMenuItems) { |
| 51 base::string16 title = base::ASCIIToUTF16("title"); | 52 base::string16 title = base::ASCIIToUTF16("title"); |
| 52 ShelfApplicationMenuModel menu(title, ShelfAppMenuItemList()); | 53 ShelfApplicationMenuModel menu(title, ShelfAppMenuItemList(), nullptr); |
| 53 // Expect the title with separators. | 54 // Expect the title with separators. |
| 54 ASSERT_EQ(static_cast<int>(3), menu.GetItemCount()); | 55 ASSERT_EQ(static_cast<int>(3), menu.GetItemCount()); |
| 55 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(0)); | 56 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(0)); |
| 56 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(1)); | 57 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(1)); |
| 57 EXPECT_EQ(title, menu.GetLabelAt(1)); | 58 EXPECT_EQ(title, menu.GetLabelAt(1)); |
| 58 EXPECT_FALSE(menu.IsEnabledAt(1)); | 59 EXPECT_FALSE(menu.IsEnabledAt(1)); |
| 59 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(2)); | 60 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(2)); |
| 60 } | 61 } |
| 61 | 62 |
| 62 // Verifies the menu contents given a non-empty item list. | 63 // Verifies the menu contents given a non-empty item list. |
| 63 TEST(ShelfApplicationMenuModelTest, VerifyContentsWithMenuItems) { | 64 TEST(ShelfApplicationMenuModelTest, VerifyContentsWithMenuItems) { |
| 64 ShelfAppMenuItemList items; | 65 ShelfAppMenuItemList items; |
| 65 base::string16 title1 = base::ASCIIToUTF16("title1"); | 66 base::string16 title1 = base::ASCIIToUTF16("title1"); |
| 66 base::string16 title2 = base::ASCIIToUTF16("title2"); | 67 base::string16 title2 = base::ASCIIToUTF16("title2"); |
| 67 base::string16 title3 = base::ASCIIToUTF16("title3"); | 68 base::string16 title3 = base::ASCIIToUTF16("title3"); |
| 68 items.push_back(base::MakeUnique<ShelfApplicationMenuItem>(title1)); | 69 items.push_back(base::MakeUnique<ShelfApplicationMenuItem>(0, title1)); |
| 69 items.push_back(base::MakeUnique<ShelfApplicationMenuItem>(title2)); | 70 items.push_back(base::MakeUnique<ShelfApplicationMenuItem>(1, title2)); |
| 70 items.push_back(base::MakeUnique<ShelfApplicationMenuItem>(title3)); | 71 items.push_back(base::MakeUnique<ShelfApplicationMenuItem>(2, title3)); |
| 71 | 72 |
| 72 base::string16 title = base::ASCIIToUTF16("title"); | 73 base::string16 title = base::ASCIIToUTF16("title"); |
| 73 ShelfApplicationMenuModel menu(title, std::move(items)); | 74 ShelfApplicationMenuModel menu(title, std::move(items), nullptr); |
| 74 ShelfApplicationMenuModelTestAPI menu_test_api(&menu); | 75 ShelfApplicationMenuModelTestAPI menu_test_api(&menu); |
| 75 | 76 |
| 76 // Expect the title with separators, the enabled items, and another separator. | 77 // Expect the title with separators, the enabled items, and another separator. |
| 77 ASSERT_EQ(static_cast<int>(7), menu.GetItemCount()); | 78 ASSERT_EQ(static_cast<int>(7), menu.GetItemCount()); |
| 78 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(0)); | 79 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(0)); |
| 79 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(1)); | 80 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(1)); |
| 80 EXPECT_EQ(title, menu.GetLabelAt(1)); | 81 EXPECT_EQ(title, menu.GetLabelAt(1)); |
| 81 EXPECT_FALSE(menu.IsEnabledAt(1)); | 82 EXPECT_FALSE(menu.IsEnabledAt(1)); |
| 82 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(2)); | 83 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(2)); |
| 83 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(3)); | 84 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(3)); |
| 84 EXPECT_EQ(title1, menu.GetLabelAt(3)); | 85 EXPECT_EQ(title1, menu.GetLabelAt(3)); |
| 85 EXPECT_TRUE(menu.IsEnabledAt(3)); | 86 EXPECT_TRUE(menu.IsEnabledAt(3)); |
| 86 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(4)); | 87 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(4)); |
| 87 EXPECT_EQ(title2, menu.GetLabelAt(4)); | 88 EXPECT_EQ(title2, menu.GetLabelAt(4)); |
| 88 EXPECT_TRUE(menu.IsEnabledAt(4)); | 89 EXPECT_TRUE(menu.IsEnabledAt(4)); |
| 89 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(5)); | 90 EXPECT_EQ(ui::MenuModel::TYPE_COMMAND, menu.GetTypeAt(5)); |
| 90 EXPECT_EQ(title3, menu.GetLabelAt(5)); | 91 EXPECT_EQ(title3, menu.GetLabelAt(5)); |
| 91 EXPECT_TRUE(menu.IsEnabledAt(5)); | 92 EXPECT_TRUE(menu.IsEnabledAt(5)); |
| 92 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(6)); | 93 EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR, menu.GetTypeAt(6)); |
| 93 } | 94 } |
| 94 | 95 |
| 95 // Verifies RecordMenuItemSelectedMetrics uses the correct histogram buckets. | 96 // Verifies RecordMenuItemSelectedMetrics uses the correct histogram buckets. |
| 96 TEST(ShelfApplicationMenuModelTest, VerifyHistogramBuckets) { | 97 TEST(ShelfApplicationMenuModelTest, VerifyHistogramBuckets) { |
| 97 const int kCommandId = 3; | 98 const int kCommandId = 3; |
| 98 const int kNumMenuItemsEnabled = 7; | 99 const int kNumMenuItemsEnabled = 7; |
| 99 | 100 |
| 100 base::HistogramTester histogram_tester; | 101 base::HistogramTester histogram_tester; |
| 101 | 102 |
| 102 ShelfAppMenuItemList items; | 103 ShelfAppMenuItemList items; |
| 103 ShelfApplicationMenuModel menu(base::ASCIIToUTF16("title"), std::move(items)); | 104 ShelfApplicationMenuModel menu(base::ASCIIToUTF16("title"), std::move(items), |
| 105 nullptr); |
| 104 ShelfApplicationMenuModelTestAPI menu_test_api(&menu); | 106 ShelfApplicationMenuModelTestAPI menu_test_api(&menu); |
| 105 menu_test_api.RecordMenuItemSelectedMetrics(kCommandId, kNumMenuItemsEnabled); | 107 menu_test_api.RecordMenuItemSelectedMetrics(kCommandId, kNumMenuItemsEnabled); |
| 106 | 108 |
| 107 histogram_tester.ExpectTotalCount(kNumItemsEnabledHistogramName, 1); | 109 histogram_tester.ExpectTotalCount(kNumItemsEnabledHistogramName, 1); |
| 108 histogram_tester.ExpectBucketCount(kNumItemsEnabledHistogramName, | 110 histogram_tester.ExpectBucketCount(kNumItemsEnabledHistogramName, |
| 109 kNumMenuItemsEnabled, 1); | 111 kNumMenuItemsEnabled, 1); |
| 110 | 112 |
| 111 histogram_tester.ExpectTotalCount(kSelectedMenuItemIndexHistogramName, 1); | 113 histogram_tester.ExpectTotalCount(kSelectedMenuItemIndexHistogramName, 1); |
| 112 histogram_tester.ExpectBucketCount(kSelectedMenuItemIndexHistogramName, | 114 histogram_tester.ExpectBucketCount(kSelectedMenuItemIndexHistogramName, |
| 113 kCommandId, 1); | 115 kCommandId, 1); |
| 114 } | 116 } |
| 115 | 117 |
| 116 // Verify histogram data is recorded when ExecuteCommand is called. | 118 // Verify histogram data is recorded when ExecuteCommand is called. |
| 117 TEST(ShelfApplicationMenuModelTest, VerifyHistogramOnExecute) { | 119 TEST(ShelfApplicationMenuModelTest, VerifyHistogramOnExecute) { |
| 118 base::HistogramTester histogram_tester; | 120 base::HistogramTester histogram_tester; |
| 119 | 121 |
| 120 ShelfAppMenuItemList items; | 122 ShelfAppMenuItemList items; |
| 121 base::string16 title = base::ASCIIToUTF16("title"); | 123 base::string16 title = base::ASCIIToUTF16("title"); |
| 122 items.push_back(base::MakeUnique<ShelfApplicationMenuItem>(title)); | 124 items.push_back(base::MakeUnique<ShelfApplicationMenuItem>(0, title)); |
| 123 ShelfApplicationMenuModel menu(title, std::move(items)); | 125 test::TestShelfItemDelegate test_delegate(nullptr); |
| 126 ShelfApplicationMenuModel menu(title, std::move(items), &test_delegate); |
| 124 menu.ExecuteCommand(0, 0); | 127 menu.ExecuteCommand(0, 0); |
| 125 | 128 |
| 126 histogram_tester.ExpectTotalCount(kNumItemsEnabledHistogramName, 1); | 129 histogram_tester.ExpectTotalCount(kNumItemsEnabledHistogramName, 1); |
| 127 histogram_tester.ExpectTotalCount(kSelectedMenuItemIndexHistogramName, 1); | 130 histogram_tester.ExpectTotalCount(kSelectedMenuItemIndexHistogramName, 1); |
| 128 } | 131 } |
| 129 | 132 |
| 130 } // namespace ash | 133 } // namespace ash |
| OLD | NEW |