| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #import <WebKit/WebKit.h> | 6 #import <WebKit/WebKit.h> |
| 7 #import <XCTest/XCTest.h> | 7 #import <XCTest/XCTest.h> |
| 8 | 8 |
| 9 #import <EarlGrey/EarlGrey.h> | 9 #import <EarlGrey/EarlGrey.h> |
| 10 | 10 |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #import "ios/web/public/test/http_server.h" | 12 #import "ios/web/public/test/http_server.h" |
| 13 #include "ios/web/public/test/http_server_util.h" | 13 #include "ios/web/public/test/http_server_util.h" |
| 14 #include "ios/web/shell/test/app/navigation_test_util.h" | 14 #include "ios/web/shell/test/app/navigation_test_util.h" |
| 15 #include "ios/web/shell/test/app/web_view_interaction_test_util.h" | 15 #include "ios/web/shell/test/app/web_view_interaction_test_util.h" |
| 16 #import "ios/web/shell/test/earl_grey/shell_matchers.h" | 16 #import "ios/web/shell/test/earl_grey/shell_matchers.h" |
| 17 | 17 |
| 18 // Navigation test cases for the web shell. These are Earl Grey integration | 18 // Navigation test cases for the web shell. These are Earl Grey integration |
| 19 // tests, which are based on XCTest. | 19 // tests, which are based on XCTest. |
| 20 @interface CRWWebShellNavigationTest : XCTestCase | 20 @interface CRWWebShellNavigationTest : XCTestCase |
| 21 | 21 |
| 22 @end | 22 @end |
| 23 | 23 |
| 24 @implementation CRWWebShellNavigationTest | 24 @implementation CRWWebShellNavigationTest |
| 25 | 25 |
| 26 // Set up called once for the class. | 26 // Set up called once for the class. |
| 27 + (void)setUp { | 27 + (void)setUp { |
| 28 [super setUp]; | 28 [super setUp]; |
| 29 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(@"Chromium")] | 29 [[EarlGrey selectElementWithMatcher:web::webViewContainingText("Chromium")] |
| 30 assertWithMatcher:grey_notNil()]; | 30 assertWithMatcher:grey_notNil()]; |
| 31 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); | 31 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); |
| 32 server.StartOrDie(); | 32 server.StartOrDie(); |
| 33 DCHECK(server.IsRunning()); | 33 DCHECK(server.IsRunning()); |
| 34 } | 34 } |
| 35 | 35 |
| 36 // Tear down called once for the class. | 36 // Tear down called once for the class. |
| 37 + (void)tearDown { | 37 + (void)tearDown { |
| 38 [super tearDown]; | 38 [super tearDown]; |
| 39 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); | 39 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); |
| 40 server.Stop(); | 40 server.Stop(); |
| 41 DCHECK(!server.IsRunning()); | 41 DCHECK(!server.IsRunning()); |
| 42 } | 42 } |
| 43 | 43 |
| 44 // Tear down called after each test. | 44 // Tear down called after each test. |
| 45 - (void)tearDown { | 45 - (void)tearDown { |
| 46 [super tearDown]; | 46 [super tearDown]; |
| 47 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); | 47 web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance(); |
| 48 server.RemoveAllResponseProviders(); | 48 server.RemoveAllResponseProviders(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Tests clicking a link to about:blank. | 51 // Tests clicking a link to about:blank. |
| 52 - (void)testNavigationLinkToAboutBlank { | 52 - (void)testNavigationLinkToAboutBlank { |
| 53 const GURL URL = web::test::HttpServer::MakeUrl( | 53 const GURL URL = web::test::HttpServer::MakeUrl( |
| 54 "http://ios/web/shell/test/http_server_files/basic_navigation_test.html"); | 54 "http://ios/web/shell/test/http_server_files/basic_navigation_test.html"); |
| 55 NSString* URLSpec = base::SysUTF8ToNSString(URL.spec()); | |
| 56 web::test::SetUpFileBasedHttpServer(); | 55 web::test::SetUpFileBasedHttpServer(); |
| 57 | 56 |
| 58 // TODO(crbug.com/611515): Create web shell utility that only requires URL, | 57 // TODO(crbug.com/611515): Create web shell utility that only requires URL, |
| 59 // and gets the web state and passes it in to the web view utility. | 58 // and gets the web state and passes it in to the web view utility. |
| 60 web::shell_test_util::LoadUrl(URL); | 59 web::shell_test_util::LoadUrl(URL); |
| 61 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URLSpec)] | 60 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL.spec())] |
| 62 assertWithMatcher:grey_notNil()]; | 61 assertWithMatcher:grey_notNil()]; |
| 63 | 62 |
| 64 web::shell_test_util::TapWebViewElementWithId( | 63 web::shell_test_util::TapWebViewElementWithId( |
| 65 "basic-link-navigation-to-about-blank"); | 64 "basic-link-navigation-to-about-blank"); |
| 66 | 65 |
| 67 [[EarlGrey selectElementWithMatcher:web::addressFieldText(@"about:blank")] | 66 [[EarlGrey selectElementWithMatcher:web::addressFieldText("about:blank")] |
| 68 assertWithMatcher:grey_notNil()]; | 67 assertWithMatcher:grey_notNil()]; |
| 69 } | 68 } |
| 70 | 69 |
| 71 // Tests the back and forward button after entering two URLs. | 70 // Tests the back and forward button after entering two URLs. |
| 72 - (void)testNavigationBackAndForward { | 71 - (void)testNavigationBackAndForward { |
| 73 // Create map of canned responses and set up the test HTML server. | 72 // Create map of canned responses and set up the test HTML server. |
| 74 std::map<GURL, std::string> responses; | 73 std::map<GURL, std::string> responses; |
| 75 const GURL URL1 = web::test::HttpServer::MakeUrl("http://firstURL"); | 74 const GURL URL1 = web::test::HttpServer::MakeUrl("http://firstURL"); |
| 76 NSString* URL1Text = base::SysUTF8ToNSString(URL1.spec()); | 75 std::string response1 = "Test Page 1"; |
| 77 NSString* response1 = @"Test Page 1"; | 76 responses[URL1] = response1; |
| 78 responses[URL1] = base::SysNSStringToUTF8(response1); | |
| 79 | 77 |
| 80 const GURL URL2 = web::test::HttpServer::MakeUrl("http://secondURL"); | 78 const GURL URL2 = web::test::HttpServer::MakeUrl("http://secondURL"); |
| 81 NSString* URL2Text = base::SysUTF8ToNSString(URL2.spec()); | 79 std::string response2 = "Test Page 2"; |
| 82 NSString* response2 = @"Test Page 2"; | 80 responses[URL2] = response2; |
| 83 responses[URL2] = base::SysNSStringToUTF8(response2); | |
| 84 | 81 |
| 85 web::test::SetUpSimpleHttpServer(responses); | 82 web::test::SetUpSimpleHttpServer(responses); |
| 86 | 83 |
| 87 web::shell_test_util::LoadUrl(URL1); | 84 web::shell_test_util::LoadUrl(URL1); |
| 88 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1Text)] | 85 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1.spec())] |
| 89 assertWithMatcher:grey_notNil()]; | 86 assertWithMatcher:grey_notNil()]; |
| 90 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response1)] | 87 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response1)] |
| 91 assertWithMatcher:grey_notNil()]; | 88 assertWithMatcher:grey_notNil()]; |
| 92 | 89 |
| 93 web::shell_test_util::LoadUrl(URL2); | 90 web::shell_test_util::LoadUrl(URL2); |
| 94 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2Text)] | 91 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2.spec())] |
| 95 assertWithMatcher:grey_notNil()]; | 92 assertWithMatcher:grey_notNil()]; |
| 96 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response2)] | 93 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response2)] |
| 97 assertWithMatcher:grey_notNil()]; | 94 assertWithMatcher:grey_notNil()]; |
| 98 | 95 |
| 99 [[EarlGrey selectElementWithMatcher:web::backButton()] | 96 [[EarlGrey selectElementWithMatcher:web::backButton()] |
| 100 performAction:grey_tap()]; | 97 performAction:grey_tap()]; |
| 101 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1Text)] | 98 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1.spec())] |
| 102 assertWithMatcher:grey_notNil()]; | 99 assertWithMatcher:grey_notNil()]; |
| 103 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response1)] | 100 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response1)] |
| 104 assertWithMatcher:grey_notNil()]; | 101 assertWithMatcher:grey_notNil()]; |
| 105 | 102 |
| 106 [[EarlGrey selectElementWithMatcher:web::forwardButton()] | 103 [[EarlGrey selectElementWithMatcher:web::forwardButton()] |
| 107 performAction:grey_tap()]; | 104 performAction:grey_tap()]; |
| 108 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2Text)] | 105 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2.spec())] |
| 109 assertWithMatcher:grey_notNil()]; | 106 assertWithMatcher:grey_notNil()]; |
| 110 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response2)] | 107 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response2)] |
| 111 assertWithMatcher:grey_notNil()]; | 108 assertWithMatcher:grey_notNil()]; |
| 112 } | 109 } |
| 113 | 110 |
| 114 // Tests back and forward navigation where a fragment link is tapped. | 111 // Tests back and forward navigation where a fragment link is tapped. |
| 115 - (void)testNavigationBackAndForwardAfterFragmentLink { | 112 - (void)testNavigationBackAndForwardAfterFragmentLink { |
| 116 // Create map of canned responses and set up the test HTML server. | 113 // Create map of canned responses and set up the test HTML server. |
| 117 std::map<GURL, std::string> responses; | 114 std::map<GURL, std::string> responses; |
| 118 const GURL URL1 = web::test::HttpServer::MakeUrl("http://fragmentLink"); | 115 const GURL URL1 = web::test::HttpServer::MakeUrl("http://fragmentLink"); |
| 119 NSString* URL1Text = base::SysUTF8ToNSString(URL1.spec()); | |
| 120 const std::string response = "<a href='#hash' id='link'>link</a>"; | 116 const std::string response = "<a href='#hash' id='link'>link</a>"; |
| 121 responses[URL1] = response; | 117 responses[URL1] = response; |
| 122 | 118 |
| 123 const GURL URL2 = web::test::HttpServer::MakeUrl("http://fragmentLink/#hash"); | 119 const GURL URL2 = web::test::HttpServer::MakeUrl("http://fragmentLink/#hash"); |
| 124 NSString* URL2Text = base::SysUTF8ToNSString(URL2.spec()); | |
| 125 | 120 |
| 126 web::test::SetUpSimpleHttpServer(responses); | 121 web::test::SetUpSimpleHttpServer(responses); |
| 127 | 122 |
| 128 web::shell_test_util::LoadUrl(URL1); | 123 web::shell_test_util::LoadUrl(URL1); |
| 129 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1Text)] | 124 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1.spec())] |
| 130 assertWithMatcher:grey_notNil()]; | 125 assertWithMatcher:grey_notNil()]; |
| 131 | 126 |
| 132 web::shell_test_util::TapWebViewElementWithId("link"); | 127 web::shell_test_util::TapWebViewElementWithId("link"); |
| 133 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2Text)] | 128 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2.spec())] |
| 134 assertWithMatcher:grey_notNil()]; | 129 assertWithMatcher:grey_notNil()]; |
| 135 | 130 |
| 136 [[EarlGrey selectElementWithMatcher:web::backButton()] | 131 [[EarlGrey selectElementWithMatcher:web::backButton()] |
| 137 performAction:grey_tap()]; | 132 performAction:grey_tap()]; |
| 138 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1Text)] | 133 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1.spec())] |
| 139 assertWithMatcher:grey_notNil()]; | 134 assertWithMatcher:grey_notNil()]; |
| 140 | 135 |
| 141 [[EarlGrey selectElementWithMatcher:web::forwardButton()] | 136 [[EarlGrey selectElementWithMatcher:web::forwardButton()] |
| 142 performAction:grey_tap()]; | 137 performAction:grey_tap()]; |
| 143 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2Text)] | 138 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2.spec())] |
| 144 assertWithMatcher:grey_notNil()]; | 139 assertWithMatcher:grey_notNil()]; |
| 145 } | 140 } |
| 146 | 141 |
| 147 @end | 142 @end |
| OLD | NEW |