| 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 #import "ios/chrome/app/chrome_app_startup_parameters.h" | 5 #import "ios/chrome/app/chrome_app_startup_parameters.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #include "base/mac/scoped_nsobject.h" | |
| 10 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 11 #include "ios/chrome/browser/app_startup_parameters.h" | 10 #include "ios/chrome/browser/app_startup_parameters.h" |
| 12 #include "ios/chrome/browser/chrome_url_constants.h" | 11 #include "ios/chrome/browser/chrome_url_constants.h" |
| 13 #import "ios/chrome/browser/xcallback_parameters.h" | 12 #import "ios/chrome/browser/xcallback_parameters.h" |
| 14 #include "ios/chrome/common/app_group/app_group_constants.h" | 13 #include "ios/chrome/common/app_group/app_group_constants.h" |
| 15 #include "testing/gtest_mac.h" | 14 #include "testing/gtest_mac.h" |
| 16 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 17 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 18 | 17 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." |
| 20 #endif |
| 21 |
| 19 namespace { | 22 namespace { |
| 20 void CheckLaunchSourceForURL(first_run::ExternalLaunch expectedSource, | 23 void CheckLaunchSourceForURL(first_run::ExternalLaunch expectedSource, |
| 21 NSString* urlString) { | 24 NSString* urlString) { |
| 22 NSURL* url = [NSURL URLWithString:urlString]; | 25 NSURL* url = [NSURL URLWithString:urlString]; |
| 23 base::scoped_nsobject<ChromeAppStartupParameters> params( | 26 ChromeAppStartupParameters* params = [ChromeAppStartupParameters |
| 24 [ChromeAppStartupParameters | 27 newChromeAppStartupParametersWithURL:url |
| 25 newChromeAppStartupParametersWithURL:url | 28 fromSourceApplication:@"com.apple.mobilesafari"]; |
| 26 fromSourceApplication:@"com.apple.mobilesafari"]); | |
| 27 EXPECT_EQ(expectedSource, [params launchSource]); | 29 EXPECT_EQ(expectedSource, [params launchSource]); |
| 28 } | 30 } |
| 29 | 31 |
| 30 typedef PlatformTest AppStartupParametersTest; | 32 typedef PlatformTest AppStartupParametersTest; |
| 31 TEST_F(PlatformTest, ParseURLWithEmptyURL) { | 33 TEST_F(PlatformTest, ParseURLWithEmptyURL) { |
| 32 NSURL* url = [NSURL URLWithString:@""]; | 34 NSURL* url = [NSURL URLWithString:@""]; |
| 33 base::scoped_nsobject<ChromeAppStartupParameters> params( | 35 ChromeAppStartupParameters* params = |
| 34 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 36 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
| 35 fromSourceApplication:nil]); | 37 fromSourceApplication:nil]; |
| 36 | 38 |
| 37 EXPECT_FALSE(params); | 39 EXPECT_FALSE(params); |
| 38 } | 40 } |
| 39 | 41 |
| 40 TEST_F(AppStartupParametersTest, ParseURLWithOneProtocol) { | 42 TEST_F(AppStartupParametersTest, ParseURLWithOneProtocol) { |
| 41 NSURL* url = [NSURL URLWithString:@"protocol://www.google.com"]; | 43 NSURL* url = [NSURL URLWithString:@"protocol://www.google.com"]; |
| 42 base::scoped_nsobject<ChromeAppStartupParameters> params( | 44 ChromeAppStartupParameters* params = |
| 43 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 45 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
| 44 fromSourceApplication:nil]); | 46 fromSourceApplication:nil]; |
| 45 // Here "protocol" opens the app and no protocol is given for the parsed URL, | 47 // Here "protocol" opens the app and no protocol is given for the parsed URL, |
| 46 // which defaults to be "http". | 48 // which defaults to be "http". |
| 47 EXPECT_EQ("http://www.google.com/", [params externalURL].spec()); | 49 EXPECT_EQ("http://www.google.com/", [params externalURL].spec()); |
| 48 } | 50 } |
| 49 | 51 |
| 50 TEST_F(AppStartupParametersTest, ParseURLWithEmptyParsedURL) { | 52 TEST_F(AppStartupParametersTest, ParseURLWithEmptyParsedURL) { |
| 51 // Test chromium:// | 53 // Test chromium:// |
| 52 NSURL* url = [NSURL URLWithString:@"chromium://"]; | 54 NSURL* url = [NSURL URLWithString:@"chromium://"]; |
| 53 base::scoped_nsobject<ChromeAppStartupParameters> params( | 55 ChromeAppStartupParameters* params = |
| 54 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 56 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
| 55 fromSourceApplication:nil]); | 57 fromSourceApplication:nil]; |
| 56 | 58 |
| 57 EXPECT_FALSE(params); | 59 EXPECT_FALSE(params); |
| 58 } | 60 } |
| 59 | 61 |
| 60 TEST_F(AppStartupParametersTest, ParseURLWithParsedURLDefaultToHttp) { | 62 TEST_F(AppStartupParametersTest, ParseURLWithParsedURLDefaultToHttp) { |
| 61 NSURL* url = [NSURL URLWithString:@"chromium://www.google.com"]; | 63 NSURL* url = [NSURL URLWithString:@"chromium://www.google.com"]; |
| 62 base::scoped_nsobject<ChromeAppStartupParameters> params( | 64 ChromeAppStartupParameters* params = |
| 63 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 65 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
| 64 fromSourceApplication:nil]); | 66 fromSourceApplication:nil]; |
| 65 | 67 |
| 66 EXPECT_EQ("http://www.google.com/", [params externalURL].spec()); | 68 EXPECT_EQ("http://www.google.com/", [params externalURL].spec()); |
| 67 } | 69 } |
| 68 | 70 |
| 69 TEST_F(AppStartupParametersTest, ParseURLWithInvalidParsedURL) { | 71 TEST_F(AppStartupParametersTest, ParseURLWithInvalidParsedURL) { |
| 70 NSURL* url = [NSURL URLWithString:@"http:google.com:foo"]; | 72 NSURL* url = [NSURL URLWithString:@"http:google.com:foo"]; |
| 71 base::scoped_nsobject<ChromeAppStartupParameters> params( | 73 ChromeAppStartupParameters* params = |
| 72 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 74 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
| 73 fromSourceApplication:nil]); | 75 fromSourceApplication:nil]; |
| 74 | 76 |
| 75 EXPECT_FALSE(params); | 77 EXPECT_FALSE(params); |
| 76 } | 78 } |
| 77 | 79 |
| 78 TEST_F(AppStartupParametersTest, ParseURLWithHttpsParsedURL) { | 80 TEST_F(AppStartupParametersTest, ParseURLWithHttpsParsedURL) { |
| 79 NSURL* url = [NSURL URLWithString:@"chromiums://www.google.com"]; | 81 NSURL* url = [NSURL URLWithString:@"chromiums://www.google.com"]; |
| 80 base::scoped_nsobject<ChromeAppStartupParameters> params( | 82 ChromeAppStartupParameters* params = |
| 81 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 83 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
| 82 fromSourceApplication:nil]); | 84 fromSourceApplication:nil]; |
| 83 | 85 |
| 84 EXPECT_EQ("https://www.google.com/", [params externalURL].spec()); | 86 EXPECT_EQ("https://www.google.com/", [params externalURL].spec()); |
| 85 } | 87 } |
| 86 | 88 |
| 87 TEST_F(AppStartupParametersTest, ParseURLWithXCallbackURL) { | 89 TEST_F(AppStartupParametersTest, ParseURLWithXCallbackURL) { |
| 88 NSURL* url = | 90 NSURL* url = |
| 89 [NSURL URLWithString:@"chromium-x-callback://x-callback-url/open?" | 91 [NSURL URLWithString:@"chromium-x-callback://x-callback-url/open?" |
| 90 "url=https://www.google.com"]; | 92 "url=https://www.google.com"]; |
| 91 base::scoped_nsobject<ChromeAppStartupParameters> params( | 93 ChromeAppStartupParameters* params = |
| 92 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 94 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
| 93 fromSourceApplication:nil]); | 95 fromSourceApplication:nil]; |
| 94 EXPECT_EQ("https://www.google.com/", [params externalURL].spec()); | 96 EXPECT_EQ("https://www.google.com/", [params externalURL].spec()); |
| 95 } | 97 } |
| 96 | 98 |
| 97 TEST_F(AppStartupParametersTest, ParseURLWithXCallbackURLAndExtraParams) { | 99 TEST_F(AppStartupParametersTest, ParseURLWithXCallbackURLAndExtraParams) { |
| 98 NSURL* url = | 100 NSURL* url = |
| 99 [NSURL URLWithString:@"chromium-x-callback://x-callback-url/open?" | 101 [NSURL URLWithString:@"chromium-x-callback://x-callback-url/open?" |
| 100 "url=https://www.google.com&" | 102 "url=https://www.google.com&" |
| 101 "x-success=http://success"]; | 103 "x-success=http://success"]; |
| 102 base::scoped_nsobject<ChromeAppStartupParameters> params( | 104 ChromeAppStartupParameters* params = |
| 103 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 105 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
| 104 fromSourceApplication:nil]); | 106 fromSourceApplication:nil]; |
| 105 EXPECT_EQ("https://www.google.com/", [params externalURL].spec()); | 107 EXPECT_EQ("https://www.google.com/", [params externalURL].spec()); |
| 106 } | 108 } |
| 107 | 109 |
| 108 TEST_F(AppStartupParametersTest, ParseURLWithMalformedXCallbackURL) { | 110 TEST_F(AppStartupParametersTest, ParseURLWithMalformedXCallbackURL) { |
| 109 NSURL* url = [NSURL | 111 NSURL* url = [NSURL |
| 110 URLWithString:@"chromium-x-callback://x-callback-url/open?url=foobar&" | 112 URLWithString:@"chromium-x-callback://x-callback-url/open?url=foobar&" |
| 111 "x-source=myapp&x-success=http://success"]; | 113 "x-source=myapp&x-success=http://success"]; |
| 112 base::scoped_nsobject<ChromeAppStartupParameters> params( | 114 ChromeAppStartupParameters* params = [ChromeAppStartupParameters |
| 113 [ChromeAppStartupParameters | 115 newChromeAppStartupParametersWithURL:url |
| 114 newChromeAppStartupParametersWithURL:url | 116 fromSourceApplication:@"com.myapp"]; |
| 115 fromSourceApplication:@"com.myapp"]); | |
| 116 EXPECT_FALSE(params); | 117 EXPECT_FALSE(params); |
| 117 } | 118 } |
| 118 | 119 |
| 119 TEST_F(AppStartupParametersTest, ParseURLWithJavascriptURLInXCallbackURL) { | 120 TEST_F(AppStartupParametersTest, ParseURLWithJavascriptURLInXCallbackURL) { |
| 120 NSURL* url = [NSURL | 121 NSURL* url = [NSURL |
| 121 URLWithString: | 122 URLWithString: |
| 122 @"chromium-x-callback://x-callback-url/open?url=" | 123 @"chromium-x-callback://x-callback-url/open?url=" |
| 123 "javascript:window.open()&x-source=myapp&x-success=http://success"]; | 124 "javascript:window.open()&x-source=myapp&x-success=http://success"]; |
| 124 base::scoped_nsobject<ChromeAppStartupParameters> params( | 125 ChromeAppStartupParameters* params = [ChromeAppStartupParameters |
| 125 [ChromeAppStartupParameters | 126 newChromeAppStartupParametersWithURL:url |
| 126 newChromeAppStartupParametersWithURL:url | 127 fromSourceApplication:@"com.myapp"]; |
| 127 fromSourceApplication:@"com.myapp"]); | |
| 128 EXPECT_FALSE(params); | 128 EXPECT_FALSE(params); |
| 129 } | 129 } |
| 130 | 130 |
| 131 TEST_F(AppStartupParametersTest, ParseURLWithChromeURLInXCallbackURL) { | 131 TEST_F(AppStartupParametersTest, ParseURLWithChromeURLInXCallbackURL) { |
| 132 NSURL* url = | 132 NSURL* url = |
| 133 [NSURL URLWithString:@"chromium-x-callback://x-callback-url/open?url=" | 133 [NSURL URLWithString:@"chromium-x-callback://x-callback-url/open?url=" |
| 134 "chrome:passwords"]; | 134 "chrome:passwords"]; |
| 135 base::scoped_nsobject<ChromeAppStartupParameters> params( | 135 ChromeAppStartupParameters* params = [ChromeAppStartupParameters |
| 136 [ChromeAppStartupParameters | 136 newChromeAppStartupParametersWithURL:url |
| 137 newChromeAppStartupParametersWithURL:url | 137 fromSourceApplication:@"com.myapp"]; |
| 138 fromSourceApplication:@"com.myapp"]); | |
| 139 EXPECT_FALSE(params); | 138 EXPECT_FALSE(params); |
| 140 } | 139 } |
| 141 | 140 |
| 142 TEST_F(AppStartupParametersTest, ParseURLWithFileParsedURL) { | 141 TEST_F(AppStartupParametersTest, ParseURLWithFileParsedURL) { |
| 143 NSURL* url = [NSURL URLWithString:@"file://localhost/path/to/file.pdf"]; | 142 NSURL* url = [NSURL URLWithString:@"file://localhost/path/to/file.pdf"]; |
| 144 base::scoped_nsobject<ChromeAppStartupParameters> params( | 143 ChromeAppStartupParameters* params = |
| 145 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 144 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
| 146 fromSourceApplication:nil]); | 145 fromSourceApplication:nil]; |
| 147 | 146 |
| 148 std::string expectedUrlString = base::StringPrintf( | 147 std::string expectedUrlString = base::StringPrintf( |
| 149 "%s://%s/file.pdf", kChromeUIScheme, kChromeUIExternalFileHost); | 148 "%s://%s/file.pdf", kChromeUIScheme, kChromeUIExternalFileHost); |
| 150 | 149 |
| 151 EXPECT_EQ(expectedUrlString, [params externalURL].spec()); | 150 EXPECT_EQ(expectedUrlString, [params externalURL].spec()); |
| 152 } | 151 } |
| 153 | 152 |
| 154 TEST_F(AppStartupParametersTest, ParseURLWithAppGroupVoiceSearch) { | 153 TEST_F(AppStartupParametersTest, ParseURLWithAppGroupVoiceSearch) { |
| 155 base::scoped_nsobject<ChromeAppStartupParameters> params( | 154 ChromeAppStartupParameters* params = [ChromeAppStartupParameters |
| 156 [ChromeAppStartupParameters | 155 newAppStartupParametersForCommand:@"voicesearch" |
| 157 newAppStartupParametersForCommand:@"voicesearch" | 156 withParameter:nil |
| 158 withParameter:nil | 157 withURL:nil |
| 159 withURL:nil | 158 fromSourceApplication:nil |
| 160 fromSourceApplication:nil | 159 fromSecureSourceApplication:nil]; |
| 161 fromSecureSourceApplication:nil]); | |
| 162 | 160 |
| 163 std::string expectedUrlString = | 161 std::string expectedUrlString = |
| 164 base::StringPrintf("%s://%s/", kChromeUIScheme, kChromeUINewTabHost); | 162 base::StringPrintf("%s://%s/", kChromeUIScheme, kChromeUINewTabHost); |
| 165 | 163 |
| 166 EXPECT_EQ(expectedUrlString, [params externalURL].spec()); | 164 EXPECT_EQ(expectedUrlString, [params externalURL].spec()); |
| 167 EXPECT_TRUE([params launchVoiceSearch]); | 165 EXPECT_TRUE([params launchVoiceSearch]); |
| 168 } | 166 } |
| 169 | 167 |
| 170 TEST_F(AppStartupParametersTest, ParseURLWithAppGroupNewTab) { | 168 TEST_F(AppStartupParametersTest, ParseURLWithAppGroupNewTab) { |
| 171 base::scoped_nsobject<ChromeAppStartupParameters> params( | 169 ChromeAppStartupParameters* params = |
| 172 [ChromeAppStartupParameters newAppStartupParametersForCommand:@"newtab" | 170 [ChromeAppStartupParameters newAppStartupParametersForCommand:@"newtab" |
| 173 withParameter:nil | 171 withParameter:nil |
| 174 withURL:nil | 172 withURL:nil |
| 175 fromSourceApplication:nil | 173 fromSourceApplication:nil |
| 176 fromSecureSourceApplication:nil]); | 174 fromSecureSourceApplication:nil]; |
| 177 std::string expectedUrlString = | 175 std::string expectedUrlString = |
| 178 base::StringPrintf("%s://%s/", kChromeUIScheme, kChromeUINewTabHost); | 176 base::StringPrintf("%s://%s/", kChromeUIScheme, kChromeUINewTabHost); |
| 179 | 177 |
| 180 EXPECT_EQ(expectedUrlString, [params externalURL].spec()); | 178 EXPECT_EQ(expectedUrlString, [params externalURL].spec()); |
| 181 EXPECT_FALSE([params launchVoiceSearch]); | 179 EXPECT_FALSE([params launchVoiceSearch]); |
| 182 } | 180 } |
| 183 | 181 |
| 184 TEST_F(AppStartupParametersTest, ParseURLWithAppGroupOpenURL) { | 182 TEST_F(AppStartupParametersTest, ParseURLWithAppGroupOpenURL) { |
| 185 base::scoped_nsobject<ChromeAppStartupParameters> params( | 183 ChromeAppStartupParameters* params = [ChromeAppStartupParameters |
| 186 [ChromeAppStartupParameters | 184 newAppStartupParametersForCommand:@"openurl" |
| 187 newAppStartupParametersForCommand:@"openurl" | 185 withParameter:@"http://foo/bar" |
| 188 withParameter:@"http://foo/bar" | 186 withURL:nil |
| 189 withURL:nil | 187 fromSourceApplication:nil |
| 190 fromSourceApplication:nil | 188 fromSecureSourceApplication:nil]; |
| 191 fromSecureSourceApplication:nil]); | |
| 192 | 189 |
| 193 EXPECT_EQ("http://foo/bar", [params externalURL].spec()); | 190 EXPECT_EQ("http://foo/bar", [params externalURL].spec()); |
| 194 } | 191 } |
| 195 | 192 |
| 196 TEST_F(AppStartupParametersTest, ParseURLWithAppGroupGarbage) { | 193 TEST_F(AppStartupParametersTest, ParseURLWithAppGroupGarbage) { |
| 197 base::scoped_nsobject<ChromeAppStartupParameters> params( | 194 ChromeAppStartupParameters* params = |
| 198 [ChromeAppStartupParameters newAppStartupParametersForCommand:@"garbage" | 195 [ChromeAppStartupParameters newAppStartupParametersForCommand:@"garbage" |
| 199 withParameter:nil | 196 withParameter:nil |
| 200 withURL:nil | 197 withURL:nil |
| 201 fromSourceApplication:nil | 198 fromSourceApplication:nil |
| 202 fromSecureSourceApplication:nil]); | 199 fromSecureSourceApplication:nil]; |
| 203 EXPECT_FALSE(params); | 200 EXPECT_FALSE(params); |
| 204 } | 201 } |
| 205 | 202 |
| 206 TEST_F(AppStartupParametersTest, FirstRunExternalLaunchSource) { | 203 TEST_F(AppStartupParametersTest, FirstRunExternalLaunchSource) { |
| 207 // Key at the beginning of query string. | 204 // Key at the beginning of query string. |
| 208 CheckLaunchSourceForURL( | 205 CheckLaunchSourceForURL( |
| 209 first_run::LAUNCH_BY_SMARTAPPBANNER, | 206 first_run::LAUNCH_BY_SMARTAPPBANNER, |
| 210 @"http://www.google.com/search?safarisab=1&query=pony"); | 207 @"http://www.google.com/search?safarisab=1&query=pony"); |
| 211 // Key at the end of query string. | 208 // Key at the end of query string. |
| 212 CheckLaunchSourceForURL( | 209 CheckLaunchSourceForURL( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 235 // Key is a substring of some other string. | 232 // Key is a substring of some other string. |
| 236 CheckLaunchSourceForURL( | 233 CheckLaunchSourceForURL( |
| 237 first_run::LAUNCH_BY_MOBILESAFARI, | 234 first_run::LAUNCH_BY_MOBILESAFARI, |
| 238 @"http://www.google.com/search?query=pony&safarisabcdefg=1"); | 235 @"http://www.google.com/search?query=pony&safarisabcdefg=1"); |
| 239 CheckLaunchSourceForURL( | 236 CheckLaunchSourceForURL( |
| 240 first_run::LAUNCH_BY_MOBILESAFARI, | 237 first_run::LAUNCH_BY_MOBILESAFARI, |
| 241 @"http://www.google.com/search?query=pony¬safarisab=1&abc=def"); | 238 @"http://www.google.com/search?query=pony¬safarisab=1&abc=def"); |
| 242 } | 239 } |
| 243 | 240 |
| 244 } // namespace | 241 } // namespace |
| OLD | NEW |