1// Copyright 2016 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#import <XCTest/XCTest.h> 6 7#import "ios/chrome/browser/device_sharing/handoff_manager_app_interface.h" 8#import "ios/chrome/test/earl_grey/chrome_earl_grey.h" 9#import "ios/chrome/test/earl_grey/chrome_test_case.h" 10#import "ios/testing/earl_grey/earl_grey_test.h" 11#import "net/base/mac/url_conversions.h" 12#include "net/test/embedded_test_server/embedded_test_server.h" 13#include "url/gurl.h" 14 15#if !defined(__has_feature) || !__has_feature(objc_arc) 16#error "This file requires ARC support." 17#endif 18 19#pragma clang diagnostic push 20#pragma clang diagnostic ignored "-Wc++98-compat-extra-semi" 21GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(HandoffManagerAppInterface); 22#pragma clang diagnostic pop 23 24namespace { 25 26// Checks that Handoff will report the specified |gurl|. 27void AssertHandoffURL(const GURL& gurl) { 28 NSURL* handoffURL = 29 [HandoffManagerAppInterface currentUserActivityWebPageURL]; 30 if (gurl.is_valid()) { 31 NSURL* URL = net::NSURLWithGURL(gurl); 32 GREYAssertTrue([handoffURL isEqual:URL], @"Incorrect Handoff URL."); 33 } else { 34 GREYAssertTrue(handoffURL == nil, @"Handoff URL is not nil."); 35 } 36} 37 38} // namespace 39 40// Tests that HandoffManager reports the correct active URL based on the 41// active tab. 42@interface HandoffManagerTestCase : ChromeTestCase 43@end 44 45@implementation HandoffManagerTestCase 46 47- (void)setUp { 48 [super setUp]; 49 GREYAssertTrue(self.testServer->Start(), @"Server did not start."); 50} 51 52#pragma mark - Tests 53 54// Tests that an empty new tab page should result in no Handoff URL. 55- (void)testNewTabPageEmptyURL { 56 AssertHandoffURL(GURL()); 57} 58 59// Tests that the simple case of Handoff URL for a single page. 60- (void)testTypicalURL { 61 const GURL destinationUrl = self.testServer->GetURL("/destination.html"); 62 [ChromeEarlGrey loadURL:destinationUrl]; 63 AssertHandoffURL(destinationUrl); 64} 65 66// Tests Handoff URL for a new tab. 67- (void)testTypicalURLInNewTab { 68 [ChromeEarlGrey openNewTab]; 69 const GURL destinationUrl = self.testServer->GetURL("/pony.html"); 70 [ChromeEarlGrey loadURL:destinationUrl]; 71 AssertHandoffURL(destinationUrl); 72} 73 74// Tests that Handoff URL should never be set for an incognito tab. 75- (void)testTypicalURLInNewIncognitoTab { 76 // Opens an incognito tab and loads a web page. Check that Handoff URL is nil. 77 [ChromeEarlGrey openNewIncognitoTab]; 78 const GURL destinationUrl = self.testServer->GetURL("/destination.html"); 79 [ChromeEarlGrey loadURL:destinationUrl]; 80 AssertHandoffURL(GURL()); 81 82 // Loads a second URL on the same incognito tab. Handoff URL should still be 83 // nil. 84 const GURL destinationUrl2 = self.testServer->GetURL("/pony.html"); 85 [ChromeEarlGrey loadURL:destinationUrl2]; 86 AssertHandoffURL(GURL()); 87} 88 89// Tests the state for Handoff URL when creating, closing tab, and switching 90// tab. 91- (void)testMultipleSwitchingTabs { 92 const GURL tab1URL = self.testServer->GetURL("/destination.html"); 93 const GURL tab2URL = self.testServer->GetURL("/pony.html"); 94 const GURL tab3URL = self.testServer->GetURL("/chromium_logo_page.html"); 95 96 // Sets up the state for 3 tabs. 97 [ChromeEarlGrey loadURL:tab1URL]; 98 [ChromeEarlGrey openNewTab]; 99 [ChromeEarlGrey loadURL:tab2URL]; 100 [ChromeEarlGrey openNewTab]; 101 [ChromeEarlGrey loadURL:tab3URL]; 102 103 // When tab 3 is closed, tab 2 is front and Handoff URL should be the URL for 104 // tab 2. 105 [ChromeEarlGrey closeCurrentTab]; 106 AssertHandoffURL(tab2URL); 107 108 // Switches back to the first tab. 109 [ChromeEarlGrey selectTabAtIndex:0]; 110 AssertHandoffURL(tab1URL); 111} 112 113// Tests the state for Handoff URL when switching between normal tabs and 114// incognito tabs. 115- (void)testSwitchBetweenNormalAndIncognitoTabs { 116 const GURL tab1URL = self.testServer->GetURL("/destination.html"); 117 const GURL tab2URL = self.testServer->GetURL("/pony.html"); 118 const GURL tab3URL = self.testServer->GetURL("/chromium_logo_page.html"); 119 120 // Loads one page. 121 [ChromeEarlGrey loadURL:tab1URL]; 122 // Loads page two in incognito and verifies that Handoff URL is nil. 123 [ChromeEarlGrey openNewIncognitoTab]; 124 [ChromeEarlGrey loadURL:tab2URL]; 125 AssertHandoffURL(GURL()); 126 127 // Loads page three in a new normal tab and verify that Handoff URL is not 128 // nil. 129 [ChromeEarlGrey openNewTab]; 130 [ChromeEarlGrey loadURL:tab3URL]; 131 AssertHandoffURL(tab3URL); 132} 133 134@end 135