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 #import <UIKit/UIKit.h> | |
6 #import <WebKit/WebKit.h> | |
7 #import <XCTest/XCTest.h> | |
8 | |
9 #import <EarlGrey/EarlGrey.h> | |
10 | |
11 #include "base/strings/sys_string_conversions.h" | |
12 #import "ios/web/public/test/http_server.h" | |
13 #include "ios/web/public/test/http_server_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" | |
16 #import "ios/web/shell/test/earl_grey/shell_base_test_case.h" | |
17 #import "ios/web/shell/test/earl_grey/shell_matchers.h" | |
18 | |
19 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
20 #error "This file requires ARC support." | |
21 #endif | |
22 | |
23 // Navigation test cases for the web shell. These are Earl Grey integration | |
24 // tests, which are based on XCTest. | |
25 @interface CRWWebShellNavigationTest : ShellBaseTestCase | |
26 | |
27 @end | |
28 | |
29 @implementation CRWWebShellNavigationTest | |
30 | |
31 // Tests clicking a link to about:blank. | |
32 - (void)testNavigationLinkToAboutBlank { | |
33 const GURL URL = web::test::HttpServer::MakeUrl( | |
34 "http://ios/web/shell/test/http_server_files/basic_navigation_test.html"); | |
35 web::test::SetUpFileBasedHttpServer(); | |
36 | |
37 web::shell_test_util::LoadUrl(URL); | |
38 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL.spec())] | |
39 assertWithMatcher:grey_notNil()]; | |
40 | |
41 web::shell_test_util::TapWebViewElementWithId( | |
42 "basic-link-navigation-to-about-blank"); | |
43 | |
44 [[EarlGrey selectElementWithMatcher:web::addressFieldText("about:blank")] | |
45 assertWithMatcher:grey_notNil()]; | |
46 } | |
47 | |
48 // Tests the back and forward button after entering two URLs. | |
49 - (void)testNavigationBackAndForward { | |
50 // Create map of canned responses and set up the test HTML server. | |
51 std::map<GURL, std::string> responses; | |
52 const GURL URL1 = web::test::HttpServer::MakeUrl("http://firstURL"); | |
53 std::string response1 = "Test Page 1"; | |
54 responses[URL1] = response1; | |
55 | |
56 const GURL URL2 = web::test::HttpServer::MakeUrl("http://secondURL"); | |
57 std::string response2 = "Test Page 2"; | |
58 responses[URL2] = response2; | |
59 | |
60 web::test::SetUpSimpleHttpServer(responses); | |
61 | |
62 web::shell_test_util::LoadUrl(URL1); | |
63 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1.spec())] | |
64 assertWithMatcher:grey_notNil()]; | |
65 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response1)] | |
66 assertWithMatcher:grey_notNil()]; | |
67 | |
68 web::shell_test_util::LoadUrl(URL2); | |
69 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2.spec())] | |
70 assertWithMatcher:grey_notNil()]; | |
71 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response2)] | |
72 assertWithMatcher:grey_notNil()]; | |
73 | |
74 [[EarlGrey selectElementWithMatcher:web::backButton()] | |
75 performAction:grey_tap()]; | |
76 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1.spec())] | |
77 assertWithMatcher:grey_notNil()]; | |
78 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response1)] | |
79 assertWithMatcher:grey_notNil()]; | |
80 | |
81 [[EarlGrey selectElementWithMatcher:web::forwardButton()] | |
82 performAction:grey_tap()]; | |
83 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2.spec())] | |
84 assertWithMatcher:grey_notNil()]; | |
85 [[EarlGrey selectElementWithMatcher:web::webViewContainingText(response2)] | |
86 assertWithMatcher:grey_notNil()]; | |
87 } | |
88 | |
89 // Tests back and forward navigation where a fragment link is tapped. | |
90 - (void)testNavigationBackAndForwardAfterFragmentLink { | |
91 // Create map of canned responses and set up the test HTML server. | |
92 std::map<GURL, std::string> responses; | |
93 const GURL URL1 = web::test::HttpServer::MakeUrl("http://fragmentLink"); | |
94 const std::string response = "<a href='#hash' id='link'>link</a>"; | |
95 responses[URL1] = response; | |
96 | |
97 const GURL URL2 = web::test::HttpServer::MakeUrl("http://fragmentLink/#hash"); | |
98 | |
99 web::test::SetUpSimpleHttpServer(responses); | |
100 | |
101 web::shell_test_util::LoadUrl(URL1); | |
102 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1.spec())] | |
103 assertWithMatcher:grey_notNil()]; | |
104 | |
105 web::shell_test_util::TapWebViewElementWithId("link"); | |
106 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2.spec())] | |
107 assertWithMatcher:grey_notNil()]; | |
108 | |
109 [[EarlGrey selectElementWithMatcher:web::backButton()] | |
110 performAction:grey_tap()]; | |
111 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL1.spec())] | |
112 assertWithMatcher:grey_notNil()]; | |
113 | |
114 [[EarlGrey selectElementWithMatcher:web::forwardButton()] | |
115 performAction:grey_tap()]; | |
116 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL2.spec())] | |
117 assertWithMatcher:grey_notNil()]; | |
118 } | |
119 | |
120 // Tests tapping a link with onclick="event.preventDefault()" and verifies that | |
121 // the URL didn't change.. | |
122 - (void)testNavigationLinkPreventDefaultOverridesHref { | |
123 // Create map of canned responses and set up the test HTML server. | |
124 std::map<GURL, std::string> responses; | |
125 const GURL URL = web::test::HttpServer::MakeUrl("http://overridesHrefLink"); | |
126 const char pageHTML[] = | |
127 "<script>" | |
128 " function printMsg() {" | |
129 " document.body.appendChild(document.createTextNode('Default " | |
130 "prevented!'));" | |
131 " }" | |
132 "</script>" | |
133 "<a href='#hash' id='overrides-href' onclick='event.preventDefault(); " | |
134 "printMsg();'>redirectLink</a>"; | |
135 responses[URL] = pageHTML; | |
136 | |
137 web::test::SetUpSimpleHttpServer(responses); | |
138 | |
139 web::shell_test_util::LoadUrl(URL); | |
140 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL.spec())] | |
141 assertWithMatcher:grey_notNil()]; | |
142 | |
143 web::shell_test_util::TapWebViewElementWithId("overrides-href"); | |
144 | |
145 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL.spec())] | |
146 assertWithMatcher:grey_notNil()]; | |
147 [[EarlGrey | |
148 selectElementWithMatcher:web::webViewContainingText("Default prevented!")] | |
149 assertWithMatcher:grey_notNil()]; | |
150 } | |
151 | |
152 // Tests tapping on a link with unsupported URL scheme. | |
153 - (void)testNavigationUnsupportedSchema { | |
154 // Create map of canned responses and set up the test HTML server. | |
155 std::map<GURL, std::string> responses; | |
156 const GURL URL = | |
157 web::test::HttpServer::MakeUrl("http://urlWithUnsupportedSchemeLink"); | |
158 const char pageHTML[] = | |
159 "<script>" | |
160 " function printMsg() {" | |
161 " document.body.appendChild(document.createTextNode(" | |
162 " 'No navigation!'));" | |
163 " }" | |
164 "</script>" | |
165 "<a href='aaa://unsupported' id='link' " | |
166 "onclick='printMsg();'>unsupportedScheme</a>"; | |
167 responses[URL] = pageHTML; | |
168 | |
169 web::test::SetUpSimpleHttpServer(responses); | |
170 | |
171 web::shell_test_util::LoadUrl(URL); | |
172 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL.spec())] | |
173 assertWithMatcher:grey_notNil()]; | |
174 | |
175 web::shell_test_util::TapWebViewElementWithId("link"); | |
176 | |
177 [[EarlGrey selectElementWithMatcher:web::addressFieldText(URL.spec())] | |
178 assertWithMatcher:grey_notNil()]; | |
179 [[EarlGrey | |
180 selectElementWithMatcher:web::webViewContainingText("No navigation!")] | |
181 assertWithMatcher:grey_notNil()]; | |
182 } | |
183 | |
184 @end | |
OLD | NEW |