Index: ios/chrome/browser/tabs/tab_unittest.mm |
diff --git a/ios/chrome/browser/tabs/tab_unittest.mm b/ios/chrome/browser/tabs/tab_unittest.mm |
index 75e89edce72453f7e2d7daf2ed7cd2e967f5644f..dae23a9d52fe66a81e9521daa6149543544cae26 100644 |
--- a/ios/chrome/browser/tabs/tab_unittest.mm |
+++ b/ios/chrome/browser/tabs/tab_unittest.mm |
@@ -411,4 +411,147 @@ |
EXPECT_NSEQ(@"Document.pdf", [[tab_ openInController] suggestedFilename]); |
} |
+// A separate test fixture is used to test opening external URLs using Google |
+// App Launcher. In any of the tests for this feature, scenarios have to be |
+// tested with the regular ChromeBrowserState AND the incognito |
+// ChromeBrowserState. |
+// In Incognito, -urlTriggersNativeAppLaunch:sourceURL should always return NO. |
+class TabOpenAppTest : public TabTest { |
+ protected: |
+ // Tests that calling |urlTriggersNativeAppLaunch:sourceURL:linkClicked| calls |
+ // |openURL:| the expected number of times. |return_value| is the value to be |
+ // returned from |openURL:|. |expected_result| is the value that is checked |
+ // for from |urlTriggersNativeAppLaunch:sourceURL:linkClicked|. |
+ void TestOpenNativeAppURL(const GURL& url, |
+ BOOL return_value, |
+ NSUInteger expected_tab_call_count, |
+ BOOL expected_result) { |
+ ExpectWithMockedExternalAppLauncherOpenURL( |
+ return_value, expected_tab_call_count, ^{ |
+ EXPECT_EQ(expected_result, |
+ [tab_ urlTriggersNativeAppLaunch:url |
+ sourceURL:GURL("http://google.com") |
+ linkClicked:YES]); |
+ }); |
+ } |
+ |
+ // Stubs out |openURL:| and checks how many times it was called. |
+ void ExpectWithMockedExternalAppLauncherOpenURL( |
+ BOOL return_value, |
+ NSUInteger expected_tab_call_count, |
+ ProceduralBlock expectation_block) { |
+ __block NSUInteger counter = 0; |
+ [mock_external_app_launcher_ |
+ onSelector:@selector(openURL:linkClicked:) |
+ callBlockExpectation:(id) ^ (const GURL& url, BOOL linkClicked) { |
+ ++counter; |
+ return return_value; |
+ }]; |
+ expectation_block(); |
+ EXPECT_EQ(expected_tab_call_count, counter); |
+ [mock_external_app_launcher_ |
+ removeBlockExpectationOnSelector:@selector(openURL:linkClicked:)]; |
+ } |
+}; |
+ |
+// A version of TabOpenAppTests customized to use the off-the-record browser |
+// state (instead of the non-incognito one). |
+class TabOpenAppOffTheRecordTest : public TabOpenAppTest { |
+ private: |
+ bool UseOffTheRecordBrowserState() const override { return true; } |
+}; |
+ |
+// Tests the opening of matching native apps. |
+TEST_F(TabOpenAppTest, testDummyURL) { |
+ EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); |
+ |
+ GURL no_native_app_url("dummy string"); |
+ TestOpenNativeAppURL(no_native_app_url, NO, 0, NO); |
+} |
+ |
+TEST_F(TabOpenAppTest, testURL) { |
+ EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); |
+ |
+ GURL testURL("http://www.youtube.com/"); |
+ // Fake metadata object to enable and disable autoopenlinks for testURL. |
+ base::scoped_nsobject<FakeNativeAppMetadata> metadata( |
+ [[FakeNativeAppMetadata alloc] init]); |
+ IOSChromeScopedTestingChromeBrowserProvider provider( |
+ base::MakeUnique<FakeChromeBrowserProvider>(metadata)); |
+ // Turn auto open on. |
+ int expectedCallCount = 1; |
+ [metadata setShouldAutoOpenLinks:YES]; |
+ TestOpenNativeAppURL(testURL, YES, expectedCallCount, YES); |
+ TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); |
+ |
+ // Turn auto open off. |
+ expectedCallCount = 0; |
+ [metadata setShouldAutoOpenLinks:NO]; |
+ TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); |
+} |
+ |
+// TODO(crbug.com/330189): This test fails if Google Maps is installed (usually |
+// on device). |
+TEST_F(TabOpenAppTest, DISABLED_testResetShouldAutoOpenOnFailure) { |
+ EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); |
+ |
+ // With a regular profile. |
+ GURL testURL("http://maps.google.com/"); |
+ // Fake metadata object |
+ base::scoped_nsobject<FakeNativeAppMetadata> metadata( |
+ [[FakeNativeAppMetadata alloc] init]); |
+ |
+ // Turn auto open on. |
+ [metadata setShouldAutoOpenLinks:YES]; |
+ int expectedCallCount = 2; |
+ TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); |
+ EXPECT_FALSE([metadata shouldAutoOpenLinks]); |
+} |
+ |
+// Tests the opening of matching native apps with off-the-record browser state. |
+TEST_F(TabOpenAppOffTheRecordTest, testDummyURL) { |
+ EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); |
+ |
+ GURL no_native_app_url("dummy string"); |
+ TestOpenNativeAppURL(no_native_app_url, NO, 0, NO); |
+} |
+ |
+TEST_F(TabOpenAppOffTheRecordTest, testURL) { |
+ EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); |
+ |
+ // With a regular chrome browser state. |
+ GURL testURL("http://www.youtube.com/"); |
+ // Mock metadata object to enable and disable autoopenlinks for testURL. |
+ base::scoped_nsobject<FakeNativeAppMetadata> metadata( |
+ [[FakeNativeAppMetadata alloc] init]); |
+ IOSChromeScopedTestingChromeBrowserProvider provider( |
+ base::MakeUnique<FakeChromeBrowserProvider>(metadata)); |
+ |
+ // Turn auto open on. |
+ [metadata setShouldAutoOpenLinks:YES]; |
+ TestOpenNativeAppURL(testURL, NO, 0, NO); |
+ |
+ // Turn auto open off. |
+ [metadata setShouldAutoOpenLinks:NO]; |
+ TestOpenNativeAppURL(testURL, NO, 0, NO); |
+} |
+ |
+// TODO(crbug.com/330189): This test fails if Google Maps is installed (usually |
+// on device). |
+TEST_F(TabOpenAppOffTheRecordTest, DISABLED_testResetShouldAutoOpenOnFailure) { |
+ EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); |
+ |
+ // With a regular profile. |
+ GURL testURL("http://maps.google.com/"); |
+ // Fake metadata object. |
+ base::scoped_nsobject<FakeNativeAppMetadata> metadata( |
+ [[FakeNativeAppMetadata alloc] init]); |
+ |
+ // Turn auto open on. |
+ [metadata setShouldAutoOpenLinks:YES]; |
+ int expectedCallCount = 2; |
+ TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); |
+ EXPECT_FALSE([metadata shouldAutoOpenLinks]); |
+} |
+ |
} // namespace |