| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 <UIKit/UIKit.h> | 5 #import <UIKit/UIKit.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/ios/block_types.h" | 11 #include "base/ios/block_types.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
| 17 #include "components/bookmarks/test/bookmark_test_helpers.h" | 17 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 18 #include "components/history/core/browser/history_service.h" | 18 #include "components/history/core/browser/history_service.h" |
| 19 #include "components/keyed_service/core/service_access_type.h" | 19 #include "components/keyed_service/core/service_access_type.h" |
| 20 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" | 20 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 21 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 21 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 22 #include "ios/chrome/browser/browser_state/test_chrome_browser_state_manager.h" | 22 #include "ios/chrome/browser/browser_state/test_chrome_browser_state_manager.h" |
| 23 #import "ios/chrome/browser/chrome_url_util.h" | 23 #import "ios/chrome/browser/chrome_url_util.h" |
| 24 #include "ios/chrome/browser/experimental_flags.h" | |
| 25 #include "ios/chrome/browser/history/history_service_factory.h" | 24 #include "ios/chrome/browser/history/history_service_factory.h" |
| 26 #import "ios/chrome/browser/tabs/legacy_tab_helper.h" | 25 #import "ios/chrome/browser/tabs/legacy_tab_helper.h" |
| 27 #import "ios/chrome/browser/tabs/tab.h" | 26 #import "ios/chrome/browser/tabs/tab.h" |
| 28 #import "ios/chrome/browser/tabs/tab_helper_util.h" | 27 #import "ios/chrome/browser/tabs/tab_helper_util.h" |
| 29 #import "ios/chrome/browser/tabs/tab_model.h" | 28 #import "ios/chrome/browser/tabs/tab_model.h" |
| 30 #import "ios/chrome/browser/tabs/tab_private.h" | 29 #import "ios/chrome/browser/tabs/tab_private.h" |
| 31 #import "ios/chrome/browser/ui/open_in_controller.h" | 30 #import "ios/chrome/browser/ui/open_in_controller.h" |
| 32 #import "ios/chrome/browser/ui/open_in_controller_testing.h" | 31 #import "ios/chrome/browser/ui/open_in_controller_testing.h" |
| 33 #import "ios/chrome/browser/web/external_app_launcher.h" | 32 #import "ios/chrome/browser/web/external_app_launcher.h" |
| 34 #include "ios/chrome/test/block_cleanup_test.h" | 33 #include "ios/chrome/test/block_cleanup_test.h" |
| 35 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_provider.h" | |
| 36 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_state_manager
.h" | 34 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_state_manager
.h" |
| 37 #include "ios/chrome/test/ios_chrome_scoped_testing_local_state.h" | 35 #include "ios/chrome/test/ios_chrome_scoped_testing_local_state.h" |
| 38 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_
metadata.h" | |
| 39 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_
whitelist_manager.h" | |
| 40 #include "ios/public/provider/chrome/browser/test_chrome_browser_provider.h" | 36 #include "ios/public/provider/chrome/browser/test_chrome_browser_provider.h" |
| 41 #import "ios/testing/ocmock_complex_type_helper.h" | 37 #import "ios/testing/ocmock_complex_type_helper.h" |
| 42 #import "ios/web/navigation/crw_session_controller.h" | 38 #import "ios/web/navigation/crw_session_controller.h" |
| 43 #import "ios/web/navigation/navigation_manager_impl.h" | 39 #import "ios/web/navigation/navigation_manager_impl.h" |
| 44 #include "ios/web/public/navigation_item.h" | 40 #include "ios/web/public/navigation_item.h" |
| 45 #import "ios/web/public/navigation_manager.h" | 41 #import "ios/web/public/navigation_manager.h" |
| 46 #include "ios/web/public/referrer.h" | 42 #include "ios/web/public/referrer.h" |
| 47 #include "ios/web/public/test/test_web_thread_bundle.h" | 43 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 48 #import "ios/web/web_state/navigation_context_impl.h" | 44 #import "ios/web/web_state/navigation_context_impl.h" |
| 49 #import "ios/web/web_state/ui/crw_web_controller.h" | 45 #import "ios/web/web_state/ui/crw_web_controller.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 friend base::RefCountedThreadSafe<HistoryQueryResultsObserver>; | 135 friend base::RefCountedThreadSafe<HistoryQueryResultsObserver>; |
| 140 virtual ~HistoryQueryResultsObserver(); | 136 virtual ~HistoryQueryResultsObserver(); |
| 141 | 137 |
| 142 private: | 138 private: |
| 143 history::QueryResults results_; | 139 history::QueryResults results_; |
| 144 base::RunLoop* run_loop_; | 140 base::RunLoop* run_loop_; |
| 145 }; | 141 }; |
| 146 | 142 |
| 147 HistoryQueryResultsObserver::~HistoryQueryResultsObserver() {} | 143 HistoryQueryResultsObserver::~HistoryQueryResultsObserver() {} |
| 148 | 144 |
| 149 class FakeChromeBrowserProvider : public ios::TestChromeBrowserProvider { | |
| 150 public: | |
| 151 FakeChromeBrowserProvider(id<NativeAppMetadata> metadata) { | |
| 152 FakeNativeAppWhitelistManager* fakeManager = | |
| 153 [[FakeNativeAppWhitelistManager alloc] init]; | |
| 154 fakeManager.metadata = metadata; | |
| 155 manager_.reset(fakeManager); | |
| 156 } | |
| 157 ~FakeChromeBrowserProvider() override {} | |
| 158 | |
| 159 id<NativeAppWhitelistManager> GetNativeAppWhitelistManager() const override { | |
| 160 return manager_; | |
| 161 } | |
| 162 | |
| 163 private: | |
| 164 base::scoped_nsprotocol<id<NativeAppWhitelistManager>> manager_; | |
| 165 }; | |
| 166 | |
| 167 // TODO(crbug.com/620465): can a TestWebState be used instead of a WebStateImpl | 145 // TODO(crbug.com/620465): can a TestWebState be used instead of a WebStateImpl |
| 168 // for those tests? This will require changing Tab to use a WebState instead of | 146 // for those tests? This will require changing Tab to use a WebState instead of |
| 169 // a WebStateImpl first though. | 147 // a WebStateImpl first though. |
| 170 class TabTest : public BlockCleanupTest { | 148 class TabTest : public BlockCleanupTest { |
| 171 public: | 149 public: |
| 172 TabTest() | 150 TabTest() |
| 173 : thread_bundle_(web::TestWebThreadBundle::REAL_FILE_THREAD), | 151 : thread_bundle_(web::TestWebThreadBundle::REAL_FILE_THREAD), |
| 174 scoped_browser_state_manager_( | 152 scoped_browser_state_manager_( |
| 175 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())), | 153 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())), |
| 176 web_state_impl_(nullptr) {} | 154 web_state_impl_(nullptr) {} |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 // the url, the default filename "Document.pdf" should be used. | 402 // the url, the default filename "Document.pdf" should be used. |
| 425 GURL url(kInvalidFilenameUrl); | 403 GURL url(kInvalidFilenameUrl); |
| 426 scoped_refptr<net::HttpResponseHeaders> headers = | 404 scoped_refptr<net::HttpResponseHeaders> headers = |
| 427 new net::HttpResponseHeaders("HTTP 1.1 200 OK"); | 405 new net::HttpResponseHeaders("HTTP 1.1 200 OK"); |
| 428 headers->AddHeader(base::StringPrintf("Content-Type: application/pdf")); | 406 headers->AddHeader(base::StringPrintf("Content-Type: application/pdf")); |
| 429 web_state_impl_->OnHttpResponseHeadersReceived(headers.get(), url); | 407 web_state_impl_->OnHttpResponseHeadersReceived(headers.get(), url); |
| 430 BrowseTo(url, url, [NSString string]); | 408 BrowseTo(url, url, [NSString string]); |
| 431 EXPECT_NSEQ(@"Document.pdf", [[tab_ openInController] suggestedFilename]); | 409 EXPECT_NSEQ(@"Document.pdf", [[tab_ openInController] suggestedFilename]); |
| 432 } | 410 } |
| 433 | 411 |
| 434 // A separate test fixture is used to test opening external URLs using Google | |
| 435 // App Launcher. In any of the tests for this feature, scenarios have to be | |
| 436 // tested with the regular ChromeBrowserState AND the incognito | |
| 437 // ChromeBrowserState. | |
| 438 // In Incognito, -urlTriggersNativeAppLaunch:sourceURL should always return NO. | |
| 439 class TabOpenAppTest : public TabTest { | |
| 440 protected: | |
| 441 // Tests that calling |urlTriggersNativeAppLaunch:sourceURL:linkClicked| calls | |
| 442 // |openURL:| the expected number of times. |return_value| is the value to be | |
| 443 // returned from |openURL:|. |expected_result| is the value that is checked | |
| 444 // for from |urlTriggersNativeAppLaunch:sourceURL:linkClicked|. | |
| 445 void TestOpenNativeAppURL(const GURL& url, | |
| 446 BOOL return_value, | |
| 447 NSUInteger expected_tab_call_count, | |
| 448 BOOL expected_result) { | |
| 449 ExpectWithMockedExternalAppLauncherOpenURL( | |
| 450 return_value, expected_tab_call_count, ^{ | |
| 451 EXPECT_EQ(expected_result, | |
| 452 [tab_ urlTriggersNativeAppLaunch:url | |
| 453 sourceURL:GURL("http://google.com") | |
| 454 linkClicked:YES]); | |
| 455 }); | |
| 456 } | |
| 457 | |
| 458 // Stubs out |openURL:| and checks how many times it was called. | |
| 459 void ExpectWithMockedExternalAppLauncherOpenURL( | |
| 460 BOOL return_value, | |
| 461 NSUInteger expected_tab_call_count, | |
| 462 ProceduralBlock expectation_block) { | |
| 463 __block NSUInteger counter = 0; | |
| 464 [mock_external_app_launcher_ | |
| 465 onSelector:@selector(openURL:linkClicked:) | |
| 466 callBlockExpectation:(id) ^ (const GURL& url, BOOL linkClicked) { | |
| 467 ++counter; | |
| 468 return return_value; | |
| 469 }]; | |
| 470 expectation_block(); | |
| 471 EXPECT_EQ(expected_tab_call_count, counter); | |
| 472 [mock_external_app_launcher_ | |
| 473 removeBlockExpectationOnSelector:@selector(openURL:linkClicked:)]; | |
| 474 } | |
| 475 }; | |
| 476 | |
| 477 // A version of TabOpenAppTests customized to use the off-the-record browser | |
| 478 // state (instead of the non-incognito one). | |
| 479 class TabOpenAppOffTheRecordTest : public TabOpenAppTest { | |
| 480 private: | |
| 481 bool UseOffTheRecordBrowserState() const override { return true; } | |
| 482 }; | |
| 483 | |
| 484 // Tests the opening of matching native apps. | |
| 485 TEST_F(TabOpenAppTest, testDummyURL) { | |
| 486 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 487 // fully deprecated. | |
| 488 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 489 return; | |
| 490 EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); | |
| 491 | |
| 492 GURL no_native_app_url("dummy string"); | |
| 493 TestOpenNativeAppURL(no_native_app_url, NO, 0, NO); | |
| 494 } | |
| 495 | |
| 496 TEST_F(TabOpenAppTest, testURL) { | |
| 497 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 498 // fully deprecated. | |
| 499 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 500 return; | |
| 501 | |
| 502 EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); | |
| 503 | |
| 504 GURL testURL("http://www.youtube.com/"); | |
| 505 // Fake metadata object to enable and disable autoopenlinks for testURL. | |
| 506 FakeNativeAppMetadata* metadata = [[FakeNativeAppMetadata alloc] init]; | |
| 507 IOSChromeScopedTestingChromeBrowserProvider provider( | |
| 508 base::MakeUnique<FakeChromeBrowserProvider>(metadata)); | |
| 509 // Turn auto open on. | |
| 510 int expectedCallCount = 1; | |
| 511 [metadata setShouldAutoOpenLinks:YES]; | |
| 512 TestOpenNativeAppURL(testURL, YES, expectedCallCount, YES); | |
| 513 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
| 514 | |
| 515 // Turn auto open off. | |
| 516 expectedCallCount = 0; | |
| 517 [metadata setShouldAutoOpenLinks:NO]; | |
| 518 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
| 519 } | |
| 520 | |
| 521 // TODO(crbug.com/330189): This test fails if Google Maps is installed (usually | |
| 522 // on device). | |
| 523 TEST_F(TabOpenAppTest, DISABLED_testResetShouldAutoOpenOnFailure) { | |
| 524 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 525 // fully deprecated. | |
| 526 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 527 return; | |
| 528 | |
| 529 EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); | |
| 530 | |
| 531 // With a regular profile. | |
| 532 GURL testURL("http://maps.google.com/"); | |
| 533 // Fake metadata object | |
| 534 FakeNativeAppMetadata* metadata = [[FakeNativeAppMetadata alloc] init]; | |
| 535 | |
| 536 // Turn auto open on. | |
| 537 [metadata setShouldAutoOpenLinks:YES]; | |
| 538 int expectedCallCount = 2; | |
| 539 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
| 540 EXPECT_FALSE([metadata shouldAutoOpenLinks]); | |
| 541 } | |
| 542 | |
| 543 // Tests the opening of matching native apps with off-the-record browser state. | |
| 544 TEST_F(TabOpenAppOffTheRecordTest, testDummyURL) { | |
| 545 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 546 // fully deprecated. | |
| 547 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 548 return; | |
| 549 | |
| 550 EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); | |
| 551 | |
| 552 GURL no_native_app_url("dummy string"); | |
| 553 TestOpenNativeAppURL(no_native_app_url, NO, 0, NO); | |
| 554 } | |
| 555 | |
| 556 TEST_F(TabOpenAppOffTheRecordTest, testURL) { | |
| 557 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 558 // fully deprecated. | |
| 559 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 560 return; | |
| 561 | |
| 562 EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); | |
| 563 | |
| 564 // With a regular chrome browser state. | |
| 565 GURL testURL("http://www.youtube.com/"); | |
| 566 // Mock metadata object to enable and disable autoopenlinks for testURL. | |
| 567 FakeNativeAppMetadata* metadata = [[FakeNativeAppMetadata alloc] init]; | |
| 568 IOSChromeScopedTestingChromeBrowserProvider provider( | |
| 569 base::MakeUnique<FakeChromeBrowserProvider>(metadata)); | |
| 570 | |
| 571 // Turn auto open on. | |
| 572 [metadata setShouldAutoOpenLinks:YES]; | |
| 573 TestOpenNativeAppURL(testURL, NO, 0, NO); | |
| 574 | |
| 575 // Turn auto open off. | |
| 576 [metadata setShouldAutoOpenLinks:NO]; | |
| 577 TestOpenNativeAppURL(testURL, NO, 0, NO); | |
| 578 } | |
| 579 | |
| 580 // TODO(crbug.com/330189): This test fails if Google Maps is installed (usually | |
| 581 // on device). | |
| 582 TEST_F(TabOpenAppOffTheRecordTest, DISABLED_testResetShouldAutoOpenOnFailure) { | |
| 583 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 584 // fully deprecated. | |
| 585 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 586 return; | |
| 587 | |
| 588 EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); | |
| 589 | |
| 590 // With a regular profile. | |
| 591 GURL testURL("http://maps.google.com/"); | |
| 592 // Fake metadata object. | |
| 593 FakeNativeAppMetadata* metadata = [[FakeNativeAppMetadata alloc] init]; | |
| 594 | |
| 595 // Turn auto open on. | |
| 596 [metadata setShouldAutoOpenLinks:YES]; | |
| 597 int expectedCallCount = 2; | |
| 598 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
| 599 EXPECT_FALSE([metadata shouldAutoOpenLinks]); | |
| 600 } | |
| 601 | |
| 602 } // namespace | 412 } // namespace |
| OLD | NEW |