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