Index: chrome/browser/chromeos/arc/arc_navigation_throttle_unittest.cc |
diff --git a/chrome/browser/chromeos/arc/arc_navigation_throttle_unittest.cc b/chrome/browser/chromeos/arc/arc_navigation_throttle_unittest.cc |
index 759a2556d52aed6728419f39f72b657e2bf96e0e..0a945f885f0d556fcf682d12eac9fcbd0eaebc82 100644 |
--- a/chrome/browser/chromeos/arc/arc_navigation_throttle_unittest.cc |
+++ b/chrome/browser/chromeos/arc/arc_navigation_throttle_unittest.cc |
@@ -2,12 +2,39 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include <algorithm> |
+ |
#include "chrome/browser/chromeos/arc/arc_navigation_throttle.h" |
+#include "components/arc/intent_helper/arc_intent_helper_bridge.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "url/gurl.h" |
namespace arc { |
+namespace { |
+ |
+// Creates an array with |num_elements| handlers and makes |chrome_index|-th |
+// handler "Chrome". If Chrome is not necessary, set |chrome_index| to |
+// |num_elements|. |
+mojo::Array<mojom::IntentHandlerInfoPtr> CreateArray(size_t num_elements, |
+ size_t chrome_index) { |
+ mojo::Array<mojom::IntentHandlerInfoPtr> handlers; |
+ for (size_t i = 0; i < num_elements; ++i) { |
+ mojom::IntentHandlerInfoPtr handler = mojom::IntentHandlerInfo::New(); |
+ handler->name = "Name"; |
+ if (i == chrome_index) { |
+ handler->package_name = |
+ ArcIntentHelperBridge::kArcIntentHelperPackageName; |
+ } else { |
+ handler->package_name = "com.package"; |
+ } |
+ handlers.push_back(std::move(handler)); |
+ } |
+ return handlers; |
+} |
+ |
+} // namespace |
+ |
TEST(ArcNavigationThrottleTest, TestShouldOverrideUrlLoading) { |
// A navigation within the same domain shouldn't be overridden. |
EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( |
@@ -38,4 +65,122 @@ TEST(ArcNavigationThrottleTest, TestShouldOverrideUrlLoading) { |
GURL("http://www.not-google.com"), GURL("http://www.google.com/"))); |
} |
+TEST(ArcNavigationThrottleTest, TestIsAppAvailable) { |
+ // Test an empty array. |
+ EXPECT_FALSE( |
+ ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(0, 0))); |
+ // Chrome only. |
+ EXPECT_FALSE( |
+ ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(1, 0))); |
+ // Chrome and another app. |
+ EXPECT_TRUE( |
+ ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(2, 0))); |
+ EXPECT_TRUE( |
+ ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(2, 1))); |
+ // App(s) only. This doesn't happen on production though. |
+ EXPECT_TRUE( |
+ ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(1, 1))); |
+ EXPECT_TRUE( |
+ ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(2, 2))); |
+} |
+ |
+TEST(ArcNavigationThrottleTest, TestFindPreferredApp) { |
+ // Test an empty array. |
+ EXPECT_EQ( |
+ 0u, ArcNavigationThrottle::FindPreferredAppForTesting(CreateArray(0, 0))); |
+ // Test no-preferred-app cases. |
+ EXPECT_EQ( |
+ 1u, ArcNavigationThrottle::FindPreferredAppForTesting(CreateArray(1, 0))); |
+ EXPECT_EQ( |
+ 2u, ArcNavigationThrottle::FindPreferredAppForTesting(CreateArray(2, 1))); |
+ EXPECT_EQ( |
+ 3u, ArcNavigationThrottle::FindPreferredAppForTesting(CreateArray(3, 2))); |
+ // Add a preferred app and call the function. |
+ for (size_t i = 0; i < 3; ++i) { |
+ mojo::Array<mojom::IntentHandlerInfoPtr> handlers = CreateArray(3, 0); |
+ handlers[i]->is_preferred = true; |
+ EXPECT_EQ(i, ArcNavigationThrottle::FindPreferredAppForTesting(handlers)) |
+ << i; |
+ } |
+} |
+ |
+TEST(ArcNavigationThrottleTest, TestGetAppIndex) { |
+ const std::string package_name = |
+ ArcIntentHelperBridge::kArcIntentHelperPackageName; |
+ // Test an empty array. |
+ EXPECT_EQ( |
+ 0u, ArcNavigationThrottle::GetAppIndex(CreateArray(0, 0), package_name)); |
+ // Test Chrome-only case. |
+ EXPECT_EQ( |
+ 0u, ArcNavigationThrottle::GetAppIndex(CreateArray(1, 0), package_name)); |
+ // Test not-found cases. |
+ EXPECT_EQ( |
+ 1u, ArcNavigationThrottle::GetAppIndex(CreateArray(1, 1), package_name)); |
+ EXPECT_EQ( |
+ 2u, ArcNavigationThrottle::GetAppIndex(CreateArray(2, 2), package_name)); |
+ // Test other cases. |
+ EXPECT_EQ( |
+ 0u, ArcNavigationThrottle::GetAppIndex(CreateArray(2, 0), package_name)); |
+ EXPECT_EQ( |
+ 1u, ArcNavigationThrottle::GetAppIndex(CreateArray(2, 1), package_name)); |
+ EXPECT_EQ( |
+ 0u, ArcNavigationThrottle::GetAppIndex(CreateArray(3, 0), package_name)); |
+ EXPECT_EQ( |
+ 1u, ArcNavigationThrottle::GetAppIndex(CreateArray(3, 1), package_name)); |
+ EXPECT_EQ( |
+ 2u, ArcNavigationThrottle::GetAppIndex(CreateArray(3, 2), package_name)); |
+} |
+ |
+TEST(ArcNavigationThrottleTest, TestIsSwapElementsNeeded) { |
+ std::pair<size_t, size_t> indices; |
+ for (size_t i = 1; i <= ArcNavigationThrottle::kMaxAppResults; ++i) { |
+ // When Chrome is the first element, swap is unnecessary. |
+ mojo::Array<mojom::IntentHandlerInfoPtr> handlers = CreateArray(i, 0); |
+ EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( |
+ handlers, &indices)) |
+ << i; |
+ |
+ // When Chrome is within the first |kMaxAppResults| elements, swap is |
+ // unnecessary. |
+ handlers = CreateArray(i, i - 1); |
+ EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( |
+ handlers, &indices)) |
+ << i; |
+ } |
+ |
+ for (size_t i = ArcNavigationThrottle::kMaxAppResults + 1; |
+ i < ArcNavigationThrottle::kMaxAppResults * 2; ++i) { |
+ // When Chrome is within the first |kMaxAppResults| elements, swap is |
+ // unnecessary. |
+ mojo::Array<mojom::IntentHandlerInfoPtr> handlers = CreateArray(i, 0); |
+ EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( |
+ handlers, &indices)) |
+ << i; |
+ |
+ // When Chrome is the |kMaxAppResults|-th element, swap is unnecessary. |
+ handlers = CreateArray(i, ArcNavigationThrottle::kMaxAppResults - 1); |
+ EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( |
+ handlers, &indices)) |
+ << i; |
+ |
+ // When Chrome is not within the first |kMaxAppResults| elements, swap is |
+ // necessary. |
+ handlers = CreateArray(i, i - 1); |
+ indices.first = indices.second = 0; |
+ EXPECT_TRUE(ArcNavigationThrottle::IsSwapElementsNeededForTesting(handlers, |
+ &indices)) |
+ << i; |
+ EXPECT_EQ(ArcNavigationThrottle::kMaxAppResults - 1u, indices.first) << i; |
+ EXPECT_EQ(i - 1, indices.second) << i; |
+ } |
+ |
+ for (size_t i = 0; i <= ArcNavigationThrottle::kMaxAppResults * 2; ++i) { |
+ // When Chrome does not exist in |handlers|, swap is unnecessary. |
+ mojo::Array<mojom::IntentHandlerInfoPtr> handlers = CreateArray(i, i); |
+ EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( |
+ handlers, &indices)) |
+ << i; |
+ } |
+} |
+ |
} // namespace arc |