OLD | NEW |
(Empty) | |
| 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 #include <map> |
| 6 |
| 7 #import <EarlGrey/EarlGrey.h> |
| 8 #import <WebKit/WebKit.h> |
| 9 #import <XCTest/XCTest.h> |
| 10 |
| 11 #include "base/ios/ios_util.h" |
| 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "components/strings/grit/components_strings.h" |
| 14 #include "ios/chrome/browser/ui/ui_util.h" |
| 15 #import "ios/chrome/test/app/chrome_test_util.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 19 #import "ios/web/public/test/http_server.h" |
| 20 #include "ios/web/public/test/http_server_util.h" |
| 21 #include "ios/web/public/test/response_providers/html_response_provider.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 |
| 24 // This test suite only tests javascript in the omnibox. Nothing to do with BVC |
| 25 // really, the name is a bit misleading. |
| 26 @interface BrowserViewControllerTestCase : ChromeTestCase |
| 27 @end |
| 28 |
| 29 @implementation BrowserViewControllerTestCase |
| 30 |
| 31 // Tests that evaluating JavaScript in the omnibox (e.g, a bookmarklet) works. |
| 32 - (void)testJavaScriptInOmnibox { |
| 33 // TODO(crbug.com/640220): Keyboard entry inside the omnibox fails only on |
| 34 // iPad |
| 35 // running iOS X. |
| 36 if (IsIPadIdiom() && base::ios::IsRunningOnIOS10OrLater()) |
| 37 return; |
| 38 |
| 39 // Preps the http server with two URLs serving content. |
| 40 std::map<GURL, std::string> responses; |
| 41 const GURL startURL = web::test::HttpServer::MakeUrl("http://origin"); |
| 42 const GURL destinationURL = |
| 43 web::test::HttpServer::MakeUrl("http://destination"); |
| 44 responses[startURL] = "Start"; |
| 45 responses[destinationURL] = "You've arrived!"; |
| 46 web::test::SetUpSimpleHttpServer(responses); |
| 47 |
| 48 // Just load the first URL. |
| 49 [ChromeEarlGrey loadURL:startURL]; |
| 50 |
| 51 // Waits for the page to load and check it is the expected content. |
| 52 id<GREYMatcher> responseMatcher = |
| 53 chrome_test_util::webViewContainingText(responses[startURL]); |
| 54 [[EarlGrey selectElementWithMatcher:responseMatcher] |
| 55 assertWithMatcher:grey_notNil()]; |
| 56 |
| 57 // In the omnibox, the URL should be present, without the http:// prefix. |
| 58 [[EarlGrey selectElementWithMatcher:chrome_test_util::omnibox()] |
| 59 assertWithMatcher:chrome_test_util::omniboxText(startURL.GetContent())]; |
| 60 |
| 61 // Types some javascript in the omnibox to trigger a navigation. |
| 62 NSString* script = |
| 63 [NSString stringWithFormat:@"javascript:location.href='%s'\n", |
| 64 destinationURL.spec().c_str()]; |
| 65 |
| 66 [[EarlGrey selectElementWithMatcher:chrome_test_util::omnibox()] |
| 67 performAction:grey_typeText(script)]; |
| 68 |
| 69 // In the omnibox, the new URL should be present, without the http:// prefix. |
| 70 [[EarlGrey selectElementWithMatcher:chrome_test_util::omnibox()] |
| 71 assertWithMatcher:chrome_test_util::omniboxText( |
| 72 destinationURL.GetContent())]; |
| 73 |
| 74 // Verifies that the navigation to the destination page happened. |
| 75 GREYAssertEqual(destinationURL, |
| 76 chrome_test_util::GetCurrentWebState()->GetVisibleURL(), |
| 77 @"Did not navigate to the destination url."); |
| 78 |
| 79 // Verifies that the destination page is shown. |
| 80 id<GREYMatcher> navigationMatcher = |
| 81 chrome_test_util::webViewContainingText(responses[destinationURL]); |
| 82 [[EarlGrey selectElementWithMatcher:grey_kindOfClass([WKWebView class])] |
| 83 assertWithMatcher:navigationMatcher]; |
| 84 } |
| 85 |
| 86 @end |
OLD | NEW |