| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 | |
| 5 #include <algorithm> | |
| 6 | |
| 7 #include "chrome/browser/chromeos/arc/arc_navigation_throttle.h" | |
| 8 #include "components/arc/intent_helper/arc_intent_helper_bridge.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "url/gurl.h" | |
| 11 | |
| 12 namespace arc { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Creates an array with |num_elements| handlers and makes |chrome_index|-th | |
| 17 // handler "Chrome". If Chrome is not necessary, set |chrome_index| to | |
| 18 // |num_elements|. | |
| 19 mojo::Array<mojom::IntentHandlerInfoPtr> CreateArray(size_t num_elements, | |
| 20 size_t chrome_index) { | |
| 21 mojo::Array<mojom::IntentHandlerInfoPtr> handlers; | |
| 22 for (size_t i = 0; i < num_elements; ++i) { | |
| 23 mojom::IntentHandlerInfoPtr handler = mojom::IntentHandlerInfo::New(); | |
| 24 handler->name = "Name"; | |
| 25 if (i == chrome_index) { | |
| 26 handler->package_name = | |
| 27 ArcIntentHelperBridge::kArcIntentHelperPackageName; | |
| 28 } else { | |
| 29 handler->package_name = "com.package"; | |
| 30 } | |
| 31 handlers.push_back(std::move(handler)); | |
| 32 } | |
| 33 return handlers; | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 TEST(ArcNavigationThrottleTest, TestShouldOverrideUrlLoading) { | |
| 39 // A navigation within the same domain shouldn't be overridden. | |
| 40 EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 41 GURL("http://google.com"), GURL("http://google.com/"))); | |
| 42 EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 43 GURL("http://google.com"), GURL("http://a.google.com/"))); | |
| 44 EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 45 GURL("http://a.google.com"), GURL("http://google.com/"))); | |
| 46 EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 47 GURL("http://a.google.com"), GURL("http://b.google.com/"))); | |
| 48 EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 49 GURL("http://a.google.com"), GURL("http://b.c.google.com/"))); | |
| 50 EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 51 GURL("http://a.b.google.com"), GURL("http://c.google.com/"))); | |
| 52 | |
| 53 // If either of two paramters is empty, the function should return false. | |
| 54 EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 55 GURL(), GURL("http://a.google.com/"))); | |
| 56 EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 57 GURL("http://a.google.com/"), GURL())); | |
| 58 EXPECT_FALSE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 59 GURL(), GURL())); | |
| 60 | |
| 61 // A navigation not within the same domain can be overridden. | |
| 62 EXPECT_TRUE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 63 GURL("http://www.google.com"), GURL("http://www.not-google.com/"))); | |
| 64 EXPECT_TRUE(ArcNavigationThrottle::ShouldOverrideUrlLoadingForTesting( | |
| 65 GURL("http://www.not-google.com"), GURL("http://www.google.com/"))); | |
| 66 } | |
| 67 | |
| 68 TEST(ArcNavigationThrottleTest, TestIsAppAvailable) { | |
| 69 // Test an empty array. | |
| 70 EXPECT_FALSE( | |
| 71 ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(0, 0))); | |
| 72 // Chrome only. | |
| 73 EXPECT_FALSE( | |
| 74 ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(1, 0))); | |
| 75 // Chrome and another app. | |
| 76 EXPECT_TRUE( | |
| 77 ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(2, 0))); | |
| 78 EXPECT_TRUE( | |
| 79 ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(2, 1))); | |
| 80 // App(s) only. This doesn't happen on production though. | |
| 81 EXPECT_TRUE( | |
| 82 ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(1, 1))); | |
| 83 EXPECT_TRUE( | |
| 84 ArcNavigationThrottle::IsAppAvailableForTesting(CreateArray(2, 2))); | |
| 85 } | |
| 86 | |
| 87 TEST(ArcNavigationThrottleTest, TestFindPreferredApp) { | |
| 88 // Test an empty array. | |
| 89 EXPECT_EQ( | |
| 90 0u, ArcNavigationThrottle::FindPreferredAppForTesting(CreateArray(0, 0))); | |
| 91 // Test no-preferred-app cases. | |
| 92 EXPECT_EQ( | |
| 93 1u, ArcNavigationThrottle::FindPreferredAppForTesting(CreateArray(1, 0))); | |
| 94 EXPECT_EQ( | |
| 95 2u, ArcNavigationThrottle::FindPreferredAppForTesting(CreateArray(2, 1))); | |
| 96 EXPECT_EQ( | |
| 97 3u, ArcNavigationThrottle::FindPreferredAppForTesting(CreateArray(3, 2))); | |
| 98 // Add a preferred app and call the function. | |
| 99 for (size_t i = 0; i < 3; ++i) { | |
| 100 mojo::Array<mojom::IntentHandlerInfoPtr> handlers = CreateArray(3, 0); | |
| 101 handlers[i]->is_preferred = true; | |
| 102 EXPECT_EQ(i, ArcNavigationThrottle::FindPreferredAppForTesting(handlers)) | |
| 103 << i; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 TEST(ArcNavigationThrottleTest, TestGetAppIndex) { | |
| 108 const std::string package_name = | |
| 109 ArcIntentHelperBridge::kArcIntentHelperPackageName; | |
| 110 // Test an empty array. | |
| 111 EXPECT_EQ( | |
| 112 0u, ArcNavigationThrottle::GetAppIndex(CreateArray(0, 0), package_name)); | |
| 113 // Test Chrome-only case. | |
| 114 EXPECT_EQ( | |
| 115 0u, ArcNavigationThrottle::GetAppIndex(CreateArray(1, 0), package_name)); | |
| 116 // Test not-found cases. | |
| 117 EXPECT_EQ( | |
| 118 1u, ArcNavigationThrottle::GetAppIndex(CreateArray(1, 1), package_name)); | |
| 119 EXPECT_EQ( | |
| 120 2u, ArcNavigationThrottle::GetAppIndex(CreateArray(2, 2), package_name)); | |
| 121 // Test other cases. | |
| 122 EXPECT_EQ( | |
| 123 0u, ArcNavigationThrottle::GetAppIndex(CreateArray(2, 0), package_name)); | |
| 124 EXPECT_EQ( | |
| 125 1u, ArcNavigationThrottle::GetAppIndex(CreateArray(2, 1), package_name)); | |
| 126 EXPECT_EQ( | |
| 127 0u, ArcNavigationThrottle::GetAppIndex(CreateArray(3, 0), package_name)); | |
| 128 EXPECT_EQ( | |
| 129 1u, ArcNavigationThrottle::GetAppIndex(CreateArray(3, 1), package_name)); | |
| 130 EXPECT_EQ( | |
| 131 2u, ArcNavigationThrottle::GetAppIndex(CreateArray(3, 2), package_name)); | |
| 132 } | |
| 133 | |
| 134 TEST(ArcNavigationThrottleTest, TestIsSwapElementsNeeded) { | |
| 135 std::pair<size_t, size_t> indices; | |
| 136 for (size_t i = 1; i <= ArcNavigationThrottle::kMaxAppResults; ++i) { | |
| 137 // When Chrome is the first element, swap is unnecessary. | |
| 138 mojo::Array<mojom::IntentHandlerInfoPtr> handlers = CreateArray(i, 0); | |
| 139 EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( | |
| 140 handlers, &indices)) | |
| 141 << i; | |
| 142 | |
| 143 // When Chrome is within the first |kMaxAppResults| elements, swap is | |
| 144 // unnecessary. | |
| 145 handlers = CreateArray(i, i - 1); | |
| 146 EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( | |
| 147 handlers, &indices)) | |
| 148 << i; | |
| 149 } | |
| 150 | |
| 151 for (size_t i = ArcNavigationThrottle::kMaxAppResults + 1; | |
| 152 i < ArcNavigationThrottle::kMaxAppResults * 2; ++i) { | |
| 153 // When Chrome is within the first |kMaxAppResults| elements, swap is | |
| 154 // unnecessary. | |
| 155 mojo::Array<mojom::IntentHandlerInfoPtr> handlers = CreateArray(i, 0); | |
| 156 EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( | |
| 157 handlers, &indices)) | |
| 158 << i; | |
| 159 | |
| 160 // When Chrome is the |kMaxAppResults|-th element, swap is unnecessary. | |
| 161 handlers = CreateArray(i, ArcNavigationThrottle::kMaxAppResults - 1); | |
| 162 EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( | |
| 163 handlers, &indices)) | |
| 164 << i; | |
| 165 | |
| 166 // When Chrome is not within the first |kMaxAppResults| elements, swap is | |
| 167 // necessary. | |
| 168 handlers = CreateArray(i, i - 1); | |
| 169 indices.first = indices.second = 0; | |
| 170 EXPECT_TRUE(ArcNavigationThrottle::IsSwapElementsNeededForTesting(handlers, | |
| 171 &indices)) | |
| 172 << i; | |
| 173 EXPECT_EQ(ArcNavigationThrottle::kMaxAppResults - 1u, indices.first) << i; | |
| 174 EXPECT_EQ(i - 1, indices.second) << i; | |
| 175 } | |
| 176 | |
| 177 for (size_t i = 0; i <= ArcNavigationThrottle::kMaxAppResults * 2; ++i) { | |
| 178 // When Chrome does not exist in |handlers|, swap is unnecessary. | |
| 179 mojo::Array<mojom::IntentHandlerInfoPtr> handlers = CreateArray(i, i); | |
| 180 EXPECT_FALSE(ArcNavigationThrottle::IsSwapElementsNeededForTesting( | |
| 181 handlers, &indices)) | |
| 182 << i; | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 } // namespace arc | |
| OLD | NEW |