Index: components/arc/intent_helper/open_with_menu_observer_unittest.cc |
diff --git a/components/arc/intent_helper/open_with_menu_observer_unittest.cc b/components/arc/intent_helper/open_with_menu_observer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..664b76e7a6d4740b177737b44b1b13e171fb636a |
--- /dev/null |
+++ b/components/arc/intent_helper/open_with_menu_observer_unittest.cc |
@@ -0,0 +1,130 @@ |
+// Copyright 2016 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 "base/strings/stringprintf.h" |
+#include "components/arc/intent_helper/open_with_menu_observer.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace arc { |
+ |
+namespace { |
+ |
+// All tests in this file assume that 4 and 10 IDC command IDs are reserved |
+// for the main and sub menus, respectively. |
+const int kFirstMainMenuId = 0x12345; |
+const int kFirstSubMenuId = 0x23456; |
+ |
+std::pair<OpenWithMenuObserver::HandlerMap, int> BuildHandlersMap( |
+ size_t num_apps) { |
+ mojo::Array<UrlHandlerInfoPtr> handlers; |
+ for (size_t i = 0; i < num_apps; ++i) { |
+ UrlHandlerInfoPtr p = UrlHandlerInfo::New(); |
+ p->name = base::StringPrintf("App %zu", i); |
+ handlers.push_back(std::move(p)); |
+ } |
+ return OpenWithMenuObserver::BuildHandlersMapForTesting( |
+ kFirstMainMenuId, 4, kFirstSubMenuId, 10, std::move(handlers)); |
+} |
+ |
+} // namespace |
+ |
+TEST(OpenWithMenuObserverTest, TestGetCommandIdsToEnable) { |
+ auto result = BuildHandlersMap(0); |
+ EXPECT_EQ(0U, result.first.size()); |
+ EXPECT_EQ(-1, result.second); |
+ |
+ result = BuildHandlersMap(1); |
+ ASSERT_EQ(1U, result.first.size()); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId)); |
+ EXPECT_EQ(-1, result.second); |
+ EXPECT_EQ("App 0", result.first[kFirstMainMenuId]->name); |
+ |
+ result = BuildHandlersMap(2); |
+ EXPECT_EQ(2U, result.first.size()); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId)); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId + 1)); |
+ EXPECT_EQ(-1, result.second); |
+ EXPECT_EQ("App 0", result.first[kFirstMainMenuId]->name); |
+ EXPECT_EQ("App 1", result.first[kFirstMainMenuId + 1]->name); |
+ |
+ result = BuildHandlersMap(3); |
+ EXPECT_EQ(3U, result.first.size()); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId)); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId + 1)); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId + 2)); |
+ EXPECT_EQ(-1, result.second); |
+ EXPECT_EQ("App 0", result.first[kFirstMainMenuId]->name); |
+ EXPECT_EQ("App 1", result.first[kFirstMainMenuId + 1]->name); |
+ EXPECT_EQ("App 2", result.first[kFirstMainMenuId + 2]->name); |
+ |
+ // Test if app names will overflow to the sub menu. |
+ result = BuildHandlersMap(4); |
+ EXPECT_EQ(4U, result.first.size()); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId)); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId + 1)); |
+ // In this case, kFirstMainMenuId + 2 should be hidden. |
+ EXPECT_EQ(kFirstMainMenuId + 3, result.second); |
+ ASSERT_EQ(1U, result.first.count(kFirstSubMenuId)); |
+ ASSERT_EQ(1U, result.first.count(kFirstSubMenuId + 1)); |
+ EXPECT_EQ("App 0", result.first[kFirstMainMenuId]->name); |
+ EXPECT_EQ("App 1", result.first[kFirstMainMenuId + 1]->name); |
+ EXPECT_EQ("App 2", result.first[kFirstSubMenuId]->name); |
+ EXPECT_EQ("App 3", result.first[kFirstSubMenuId + 1]->name); |
+ |
+ result = BuildHandlersMap(11); |
+ EXPECT_EQ(11U, result.first.size()); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId)); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId + 1)); |
+ EXPECT_EQ(kFirstMainMenuId + 3, result.second); |
+ EXPECT_EQ("App 0", result.first[kFirstMainMenuId]->name); |
+ EXPECT_EQ("App 1", result.first[kFirstMainMenuId + 1]->name); |
+ for (size_t i = 0; i < 9; ++i) { |
+ ASSERT_EQ(1U, result.first.count(kFirstSubMenuId + i)) << i; |
+ } |
+ |
+ // The main and sub menus can show up to 12 (=3+10-1) app names. |
+ result = BuildHandlersMap(12); |
+ EXPECT_EQ(12U, result.first.size()); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId)); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId + 1)); |
+ EXPECT_EQ(kFirstMainMenuId + 3, result.second); |
+ EXPECT_EQ("App 0", result.first[kFirstMainMenuId]->name); |
+ EXPECT_EQ("App 1", result.first[kFirstMainMenuId + 1]->name); |
+ for (size_t i = 0; i < 10; ++i) { |
+ const int id = kFirstSubMenuId + i; |
+ ASSERT_EQ(1U, result.first.count(id)) << i; |
+ EXPECT_EQ(base::StringPrintf("App %zu", i + 2), result.first[id]->name) |
+ << i; |
+ } |
+ |
+ result = BuildHandlersMap(13); |
+ EXPECT_EQ(12U, result.first.size()); // still 12 |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId)); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId + 1)); |
+ EXPECT_EQ(kFirstMainMenuId + 3, result.second); |
+ EXPECT_EQ("App 0", result.first[kFirstMainMenuId]->name); |
+ EXPECT_EQ("App 1", result.first[kFirstMainMenuId + 1]->name); |
+ for (size_t i = 0; i < 10; ++i) { // still 10 |
+ const int id = kFirstSubMenuId + i; |
+ ASSERT_EQ(1U, result.first.count(id)) << i; |
+ EXPECT_EQ(base::StringPrintf("App %zu", i + 2), result.first[id]->name) |
+ << i; |
+ } |
+ |
+ result = BuildHandlersMap(1000); |
+ EXPECT_EQ(12U, result.first.size()); // still 12 |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId)); |
+ ASSERT_EQ(1U, result.first.count(kFirstMainMenuId + 1)); |
+ EXPECT_EQ(kFirstMainMenuId + 3, result.second); |
+ EXPECT_EQ("App 0", result.first[kFirstMainMenuId]->name); |
+ EXPECT_EQ("App 1", result.first[kFirstMainMenuId + 1]->name); |
+ for (size_t i = 0; i < 10; ++i) { // still 10 |
+ const int id = kFirstSubMenuId + i; |
+ ASSERT_EQ(1U, result.first.count(id)) << i; |
+ EXPECT_EQ(base::StringPrintf("App %zu", i + 2), result.first[id]->name) |
+ << i; |
+ } |
+} |
+ |
+} // namespace arc |