| 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/navigation_manager_impl.h" | 38 #import "ios/web/navigation/navigation_manager_impl.h" |
| 43 #include "ios/web/public/navigation_item.h" | 39 #include "ios/web/public/navigation_item.h" |
| 44 #import "ios/web/public/navigation_manager.h" | 40 #import "ios/web/public/navigation_manager.h" |
| 45 #include "ios/web/public/referrer.h" | 41 #include "ios/web/public/referrer.h" |
| 46 #include "ios/web/public/test/test_web_thread_bundle.h" | 42 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 47 #import "ios/web/web_state/navigation_context_impl.h" | 43 #import "ios/web/web_state/navigation_context_impl.h" |
| 48 #import "ios/web/web_state/ui/crw_web_controller.h" | 44 #import "ios/web/web_state/ui/crw_web_controller.h" |
| 49 #import "ios/web/web_state/web_state_impl.h" | 45 #import "ios/web/web_state/web_state_impl.h" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 friend base::RefCountedThreadSafe<HistoryQueryResultsObserver>; | 131 friend base::RefCountedThreadSafe<HistoryQueryResultsObserver>; |
| 136 virtual ~HistoryQueryResultsObserver(); | 132 virtual ~HistoryQueryResultsObserver(); |
| 137 | 133 |
| 138 private: | 134 private: |
| 139 history::QueryResults results_; | 135 history::QueryResults results_; |
| 140 base::RunLoop* run_loop_; | 136 base::RunLoop* run_loop_; |
| 141 }; | 137 }; |
| 142 | 138 |
| 143 HistoryQueryResultsObserver::~HistoryQueryResultsObserver() {} | 139 HistoryQueryResultsObserver::~HistoryQueryResultsObserver() {} |
| 144 | 140 |
| 145 class FakeChromeBrowserProvider : public ios::TestChromeBrowserProvider { | |
| 146 public: | |
| 147 FakeChromeBrowserProvider(id<NativeAppMetadata> metadata) { | |
| 148 FakeNativeAppWhitelistManager* fakeManager = | |
| 149 [[FakeNativeAppWhitelistManager alloc] init]; | |
| 150 fakeManager.metadata = metadata; | |
| 151 manager_.reset(fakeManager); | |
| 152 } | |
| 153 ~FakeChromeBrowserProvider() override {} | |
| 154 | |
| 155 id<NativeAppWhitelistManager> GetNativeAppWhitelistManager() const override { | |
| 156 return manager_; | |
| 157 } | |
| 158 | |
| 159 private: | |
| 160 base::scoped_nsprotocol<id<NativeAppWhitelistManager>> manager_; | |
| 161 }; | |
| 162 | |
| 163 // TODO(crbug.com/620465): can a TestWebState be used instead of a WebStateImpl | 141 // TODO(crbug.com/620465): can a TestWebState be used instead of a WebStateImpl |
| 164 // for those tests? This will require changing Tab to use a WebState instead of | 142 // for those tests? This will require changing Tab to use a WebState instead of |
| 165 // a WebStateImpl first though. | 143 // a WebStateImpl first though. |
| 166 class TabTest : public BlockCleanupTest { | 144 class TabTest : public BlockCleanupTest { |
| 167 public: | 145 public: |
| 168 TabTest() | 146 TabTest() |
| 169 : thread_bundle_(web::TestWebThreadBundle::REAL_FILE_THREAD), | 147 : thread_bundle_(web::TestWebThreadBundle::REAL_FILE_THREAD), |
| 170 scoped_browser_state_manager_( | 148 scoped_browser_state_manager_( |
| 171 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())), | 149 base::MakeUnique<TestChromeBrowserStateManager>(base::FilePath())), |
| 172 web_state_impl_(nullptr) {} | 150 web_state_impl_(nullptr) {} |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 // the url, the default filename "Document.pdf" should be used. | 393 // the url, the default filename "Document.pdf" should be used. |
| 416 GURL url(kInvalidFilenameUrl); | 394 GURL url(kInvalidFilenameUrl); |
| 417 scoped_refptr<net::HttpResponseHeaders> headers = | 395 scoped_refptr<net::HttpResponseHeaders> headers = |
| 418 new net::HttpResponseHeaders("HTTP 1.1 200 OK"); | 396 new net::HttpResponseHeaders("HTTP 1.1 200 OK"); |
| 419 headers->AddHeader(base::StringPrintf("Content-Type: application/pdf")); | 397 headers->AddHeader(base::StringPrintf("Content-Type: application/pdf")); |
| 420 web_state_impl_->OnHttpResponseHeadersReceived(headers.get(), url); | 398 web_state_impl_->OnHttpResponseHeadersReceived(headers.get(), url); |
| 421 BrowseTo(url, url, [NSString string]); | 399 BrowseTo(url, url, [NSString string]); |
| 422 EXPECT_NSEQ(@"Document.pdf", [[tab_ openInController] suggestedFilename]); | 400 EXPECT_NSEQ(@"Document.pdf", [[tab_ openInController] suggestedFilename]); |
| 423 } | 401 } |
| 424 | 402 |
| 425 // A separate test fixture is used to test opening external URLs using Google | |
| 426 // App Launcher. In any of the tests for this feature, scenarios have to be | |
| 427 // tested with the regular ChromeBrowserState AND the incognito | |
| 428 // ChromeBrowserState. | |
| 429 // In Incognito, -urlTriggersNativeAppLaunch:sourceURL should always return NO. | |
| 430 class TabOpenAppTest : public TabTest { | |
| 431 protected: | |
| 432 // Tests that calling |urlTriggersNativeAppLaunch:sourceURL:linkClicked| calls | |
| 433 // |openURL:| the expected number of times. |return_value| is the value to be | |
| 434 // returned from |openURL:|. |expected_result| is the value that is checked | |
| 435 // for from |urlTriggersNativeAppLaunch:sourceURL:linkClicked|. | |
| 436 void TestOpenNativeAppURL(const GURL& url, | |
| 437 BOOL return_value, | |
| 438 NSUInteger expected_tab_call_count, | |
| 439 BOOL expected_result) { | |
| 440 ExpectWithMockedExternalAppLauncherOpenURL( | |
| 441 return_value, expected_tab_call_count, ^{ | |
| 442 EXPECT_EQ(expected_result, | |
| 443 [tab_ urlTriggersNativeAppLaunch:url | |
| 444 sourceURL:GURL("http://google.com") | |
| 445 linkClicked:YES]); | |
| 446 }); | |
| 447 } | |
| 448 | |
| 449 // Stubs out |openURL:| and checks how many times it was called. | |
| 450 void ExpectWithMockedExternalAppLauncherOpenURL( | |
| 451 BOOL return_value, | |
| 452 NSUInteger expected_tab_call_count, | |
| 453 ProceduralBlock expectation_block) { | |
| 454 __block NSUInteger counter = 0; | |
| 455 [mock_external_app_launcher_ | |
| 456 onSelector:@selector(openURL:linkClicked:) | |
| 457 callBlockExpectation:(id) ^ (const GURL& url, BOOL linkClicked) { | |
| 458 ++counter; | |
| 459 return return_value; | |
| 460 }]; | |
| 461 expectation_block(); | |
| 462 EXPECT_EQ(expected_tab_call_count, counter); | |
| 463 [mock_external_app_launcher_ | |
| 464 removeBlockExpectationOnSelector:@selector(openURL:linkClicked:)]; | |
| 465 } | |
| 466 }; | |
| 467 | |
| 468 // A version of TabOpenAppTests customized to use the off-the-record browser | |
| 469 // state (instead of the non-incognito one). | |
| 470 class TabOpenAppOffTheRecordTest : public TabOpenAppTest { | |
| 471 private: | |
| 472 bool UseOffTheRecordBrowserState() const override { return true; } | |
| 473 }; | |
| 474 | |
| 475 // Tests the opening of matching native apps. | |
| 476 TEST_F(TabOpenAppTest, testDummyURL) { | |
| 477 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 478 // fully deprecated. | |
| 479 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 480 return; | |
| 481 EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); | |
| 482 | |
| 483 GURL no_native_app_url("dummy string"); | |
| 484 TestOpenNativeAppURL(no_native_app_url, NO, 0, NO); | |
| 485 } | |
| 486 | |
| 487 TEST_F(TabOpenAppTest, testURL) { | |
| 488 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 489 // fully deprecated. | |
| 490 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 491 return; | |
| 492 | |
| 493 EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); | |
| 494 | |
| 495 GURL testURL("http://www.youtube.com/"); | |
| 496 // Fake metadata object to enable and disable autoopenlinks for testURL. | |
| 497 FakeNativeAppMetadata* metadata = [[FakeNativeAppMetadata alloc] init]; | |
| 498 IOSChromeScopedTestingChromeBrowserProvider provider( | |
| 499 base::MakeUnique<FakeChromeBrowserProvider>(metadata)); | |
| 500 // Turn auto open on. | |
| 501 int expectedCallCount = 1; | |
| 502 [metadata setShouldAutoOpenLinks:YES]; | |
| 503 TestOpenNativeAppURL(testURL, YES, expectedCallCount, YES); | |
| 504 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
| 505 | |
| 506 // Turn auto open off. | |
| 507 expectedCallCount = 0; | |
| 508 [metadata setShouldAutoOpenLinks:NO]; | |
| 509 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
| 510 } | |
| 511 | |
| 512 // TODO(crbug.com/330189): This test fails if Google Maps is installed (usually | |
| 513 // on device). | |
| 514 TEST_F(TabOpenAppTest, DISABLED_testResetShouldAutoOpenOnFailure) { | |
| 515 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 516 // fully deprecated. | |
| 517 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 518 return; | |
| 519 | |
| 520 EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); | |
| 521 | |
| 522 // With a regular profile. | |
| 523 GURL testURL("http://maps.google.com/"); | |
| 524 // Fake metadata object | |
| 525 FakeNativeAppMetadata* metadata = [[FakeNativeAppMetadata alloc] init]; | |
| 526 | |
| 527 // Turn auto open on. | |
| 528 [metadata setShouldAutoOpenLinks:YES]; | |
| 529 int expectedCallCount = 2; | |
| 530 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
| 531 EXPECT_FALSE([metadata shouldAutoOpenLinks]); | |
| 532 } | |
| 533 | |
| 534 // Tests the opening of matching native apps with off-the-record browser state. | |
| 535 TEST_F(TabOpenAppOffTheRecordTest, testDummyURL) { | |
| 536 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 537 // fully deprecated. | |
| 538 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 539 return; | |
| 540 | |
| 541 EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); | |
| 542 | |
| 543 GURL no_native_app_url("dummy string"); | |
| 544 TestOpenNativeAppURL(no_native_app_url, NO, 0, NO); | |
| 545 } | |
| 546 | |
| 547 TEST_F(TabOpenAppOffTheRecordTest, testURL) { | |
| 548 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 549 // fully deprecated. | |
| 550 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 551 return; | |
| 552 | |
| 553 EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); | |
| 554 | |
| 555 // With a regular chrome browser state. | |
| 556 GURL testURL("http://www.youtube.com/"); | |
| 557 // Mock metadata object to enable and disable autoopenlinks for testURL. | |
| 558 FakeNativeAppMetadata* metadata = [[FakeNativeAppMetadata alloc] init]; | |
| 559 IOSChromeScopedTestingChromeBrowserProvider provider( | |
| 560 base::MakeUnique<FakeChromeBrowserProvider>(metadata)); | |
| 561 | |
| 562 // Turn auto open on. | |
| 563 [metadata setShouldAutoOpenLinks:YES]; | |
| 564 TestOpenNativeAppURL(testURL, NO, 0, NO); | |
| 565 | |
| 566 // Turn auto open off. | |
| 567 [metadata setShouldAutoOpenLinks:NO]; | |
| 568 TestOpenNativeAppURL(testURL, NO, 0, NO); | |
| 569 } | |
| 570 | |
| 571 // TODO(crbug.com/330189): This test fails if Google Maps is installed (usually | |
| 572 // on device). | |
| 573 TEST_F(TabOpenAppOffTheRecordTest, DISABLED_testResetShouldAutoOpenOnFailure) { | |
| 574 // TODO(crbug/711511): Remove this test when Native App Launcher has been | |
| 575 // fully deprecated. | |
| 576 if (!experimental_flags::IsNativeAppLauncherEnabled()) | |
| 577 return; | |
| 578 | |
| 579 EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); | |
| 580 | |
| 581 // With a regular profile. | |
| 582 GURL testURL("http://maps.google.com/"); | |
| 583 // Fake metadata object. | |
| 584 FakeNativeAppMetadata* metadata = [[FakeNativeAppMetadata alloc] init]; | |
| 585 | |
| 586 // Turn auto open on. | |
| 587 [metadata setShouldAutoOpenLinks:YES]; | |
| 588 int expectedCallCount = 2; | |
| 589 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
| 590 EXPECT_FALSE([metadata shouldAutoOpenLinks]); | |
| 591 } | |
| 592 | |
| 593 } // namespace | 403 } // namespace |
| OLD | NEW |