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" |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 // the url, the default filename "Document.pdf" should be used. | 404 // the url, the default filename "Document.pdf" should be used. |
405 GURL url(kInvalidFilenameUrl); | 405 GURL url(kInvalidFilenameUrl); |
406 scoped_refptr<net::HttpResponseHeaders> headers = | 406 scoped_refptr<net::HttpResponseHeaders> headers = |
407 new net::HttpResponseHeaders("HTTP 1.1 200 OK"); | 407 new net::HttpResponseHeaders("HTTP 1.1 200 OK"); |
408 headers->AddHeader(base::StringPrintf("Content-Type: application/pdf")); | 408 headers->AddHeader(base::StringPrintf("Content-Type: application/pdf")); |
409 web_state_impl_->OnHttpResponseHeadersReceived(headers.get(), url); | 409 web_state_impl_->OnHttpResponseHeadersReceived(headers.get(), url); |
410 BrowseTo(url, url, [NSString string]); | 410 BrowseTo(url, url, [NSString string]); |
411 EXPECT_NSEQ(@"Document.pdf", [[tab_ openInController] suggestedFilename]); | 411 EXPECT_NSEQ(@"Document.pdf", [[tab_ openInController] suggestedFilename]); |
412 } | 412 } |
413 | 413 |
414 // A separate test fixture is used to test opening external URLs using Google | |
415 // App Launcher. In any of the tests for this feature, scenarios have to be | |
416 // tested with the regular ChromeBrowserState AND the incognito | |
417 // ChromeBrowserState. | |
418 // In Incognito, -urlTriggersNativeAppLaunch:sourceURL should always return NO. | |
419 class TabOpenAppTest : public TabTest { | |
420 protected: | |
421 // Tests that calling |urlTriggersNativeAppLaunch:sourceURL:linkClicked| calls | |
422 // |openURL:| the expected number of times. |return_value| is the value to be | |
423 // returned from |openURL:|. |expected_result| is the value that is checked | |
424 // for from |urlTriggersNativeAppLaunch:sourceURL:linkClicked|. | |
425 void TestOpenNativeAppURL(const GURL& url, | |
426 BOOL return_value, | |
427 NSUInteger expected_tab_call_count, | |
428 BOOL expected_result) { | |
429 ExpectWithMockedExternalAppLauncherOpenURL( | |
430 return_value, expected_tab_call_count, ^{ | |
431 EXPECT_EQ(expected_result, | |
432 [tab_ urlTriggersNativeAppLaunch:url | |
433 sourceURL:GURL("http://google.com") | |
434 linkClicked:YES]); | |
435 }); | |
436 } | |
437 | |
438 // Stubs out |openURL:| and checks how many times it was called. | |
439 void ExpectWithMockedExternalAppLauncherOpenURL( | |
440 BOOL return_value, | |
441 NSUInteger expected_tab_call_count, | |
442 ProceduralBlock expectation_block) { | |
443 __block NSUInteger counter = 0; | |
444 [mock_external_app_launcher_ | |
445 onSelector:@selector(openURL:linkClicked:) | |
446 callBlockExpectation:(id) ^ (const GURL& url, BOOL linkClicked) { | |
447 ++counter; | |
448 return return_value; | |
449 }]; | |
450 expectation_block(); | |
451 EXPECT_EQ(expected_tab_call_count, counter); | |
452 [mock_external_app_launcher_ | |
453 removeBlockExpectationOnSelector:@selector(openURL:linkClicked:)]; | |
454 } | |
455 }; | |
456 | |
457 // A version of TabOpenAppTests customized to use the off-the-record browser | |
458 // state (instead of the non-incognito one). | |
459 class TabOpenAppOffTheRecordTest : public TabOpenAppTest { | |
460 private: | |
461 bool UseOffTheRecordBrowserState() const override { return true; } | |
462 }; | |
463 | |
464 // Tests the opening of matching native apps. | |
465 TEST_F(TabOpenAppTest, testDummyURL) { | |
466 EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); | |
467 | |
468 GURL no_native_app_url("dummy string"); | |
469 TestOpenNativeAppURL(no_native_app_url, NO, 0, NO); | |
470 } | |
471 | |
472 TEST_F(TabOpenAppTest, testURL) { | |
473 EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); | |
474 | |
475 GURL testURL("http://www.youtube.com/"); | |
476 // Fake metadata object to enable and disable autoopenlinks for testURL. | |
477 base::scoped_nsobject<FakeNativeAppMetadata> metadata( | |
478 [[FakeNativeAppMetadata alloc] init]); | |
479 IOSChromeScopedTestingChromeBrowserProvider provider( | |
480 base::MakeUnique<FakeChromeBrowserProvider>(metadata)); | |
481 // Turn auto open on. | |
482 int expectedCallCount = 1; | |
483 [metadata setShouldAutoOpenLinks:YES]; | |
484 TestOpenNativeAppURL(testURL, YES, expectedCallCount, YES); | |
485 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
486 | |
487 // Turn auto open off. | |
488 expectedCallCount = 0; | |
489 [metadata setShouldAutoOpenLinks:NO]; | |
490 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
491 } | |
492 | |
493 // TODO(crbug.com/330189): This test fails if Google Maps is installed (usually | |
494 // on device). | |
495 TEST_F(TabOpenAppTest, DISABLED_testResetShouldAutoOpenOnFailure) { | |
496 EXPECT_FALSE([tab_ browserState]->IsOffTheRecord()); | |
497 | |
498 // With a regular profile. | |
499 GURL testURL("http://maps.google.com/"); | |
500 // Fake metadata object | |
501 base::scoped_nsobject<FakeNativeAppMetadata> metadata( | |
502 [[FakeNativeAppMetadata alloc] init]); | |
503 | |
504 // Turn auto open on. | |
505 [metadata setShouldAutoOpenLinks:YES]; | |
506 int expectedCallCount = 2; | |
507 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
508 EXPECT_FALSE([metadata shouldAutoOpenLinks]); | |
509 } | |
510 | |
511 // Tests the opening of matching native apps with off-the-record browser state. | |
512 TEST_F(TabOpenAppOffTheRecordTest, testDummyURL) { | |
513 EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); | |
514 | |
515 GURL no_native_app_url("dummy string"); | |
516 TestOpenNativeAppURL(no_native_app_url, NO, 0, NO); | |
517 } | |
518 | |
519 TEST_F(TabOpenAppOffTheRecordTest, testURL) { | |
520 EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); | |
521 | |
522 // With a regular chrome browser state. | |
523 GURL testURL("http://www.youtube.com/"); | |
524 // Mock metadata object to enable and disable autoopenlinks for testURL. | |
525 base::scoped_nsobject<FakeNativeAppMetadata> metadata( | |
526 [[FakeNativeAppMetadata alloc] init]); | |
527 IOSChromeScopedTestingChromeBrowserProvider provider( | |
528 base::MakeUnique<FakeChromeBrowserProvider>(metadata)); | |
529 | |
530 // Turn auto open on. | |
531 [metadata setShouldAutoOpenLinks:YES]; | |
532 TestOpenNativeAppURL(testURL, NO, 0, NO); | |
533 | |
534 // Turn auto open off. | |
535 [metadata setShouldAutoOpenLinks:NO]; | |
536 TestOpenNativeAppURL(testURL, NO, 0, NO); | |
537 } | |
538 | |
539 // TODO(crbug.com/330189): This test fails if Google Maps is installed (usually | |
540 // on device). | |
541 TEST_F(TabOpenAppOffTheRecordTest, DISABLED_testResetShouldAutoOpenOnFailure) { | |
542 EXPECT_TRUE([tab_ browserState]->IsOffTheRecord()); | |
543 | |
544 // With a regular profile. | |
545 GURL testURL("http://maps.google.com/"); | |
546 // Fake metadata object. | |
547 base::scoped_nsobject<FakeNativeAppMetadata> metadata( | |
548 [[FakeNativeAppMetadata alloc] init]); | |
549 | |
550 // Turn auto open on. | |
551 [metadata setShouldAutoOpenLinks:YES]; | |
552 int expectedCallCount = 2; | |
553 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); | |
554 EXPECT_FALSE([metadata shouldAutoOpenLinks]); | |
555 } | |
556 | |
557 } // namespace | 414 } // namespace |
OLD | NEW |