Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: ios/web/navigation/history_state_operations_inttest.mm

Issue 2605223002: Created test for no-op history state operations. (Closed)
Patch Set: eugene's comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/web/BUILD.gn ('k') | ios/web/test/web_int_test.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "base/memory/ptr_util.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "base/test/ios/wait_util.h"
8 #import "ios/web/public/navigation_item.h"
9 #import "ios/web/public/navigation_manager.h"
10 #import "ios/web/public/test/http_server.h"
11 #include "ios/web/public/test/http_server_util.h"
12 #import "ios/web/public/test/web_view_interaction_test_util.h"
13 #import "ios/web/public/web_state/web_state.h"
14 #import "ios/web/test/web_int_test.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/gtest_mac.h"
17 #include "url/url_canon.h"
18
19 namespace {
20
21 // URL for the test window.location test file. The page at this URL contains
22 // several buttons that trigger window.location commands. The page supports
23 // several JavaScript functions:
24 // - updateUrlToLoadText(), which takes a URL and updates a div on the page to
25 // contain that text. This URL is used as the parameter for window.location
26 // function calls triggered by button taps.
27 // - getUrl(), which returns the URL that was set via updateUrlToLoadText().
28 // - isOnLoadTextVisible(), which returns whether a placeholder string is
29 // present on the page. This string is added to the page in the onload event
30 // and is removed once a button is tapped. Verifying that the onload text is
31 // visible after tapping a button is equivalent to checking that a load has
32 // occurred as the result of the button tap.
33 const char kHistoryStateOperationsTestUrl[] =
34 "http://ios/testing/data/http_server_files/state_operations.html";
35
36 // Button IDs used in the window.location test page.
37 const char kPushStateId[] = "push-state";
38 const char kReplaceStateId[] = "replace-state";
39
40 // JavaScript functions on the history state test page.
41 NSString* const kUpdateStateParamsScriptFormat =
42 @"updateStateParams('%s', '%s', '%s')";
43 NSString* const kOnLoadCheckScript = @"isOnLoadPlaceholderTextVisible()";
44 NSString* const kNoOpCheckScript = @"isNoOpPlaceholderTextVisible()";
45
46 } // namespace
47
48 // Test fixture for integration tests involving html5 window.history state
49 // operations.
50 class HistoryStateOperationsTest : public web::WebIntTest {
51 protected:
52 void SetUp() override {
53 web::WebIntTest::SetUp();
54
55 // History state tests use file-based test pages.
56 web::test::SetUpFileBasedHttpServer();
57
58 // Load the history state test page.
59 state_operations_url_ =
60 web::test::HttpServer::MakeUrl(kHistoryStateOperationsTestUrl);
61 LoadUrl(state_operations_url());
62 }
63
64 // The URL of the window.location test page.
65 const GURL& state_operations_url() { return state_operations_url_; }
66
67 // Sets the parameters to use for state operations on the test page. This
68 // function executes a script that populates JavaScript values on the test
69 // page. When the "push-state" or "replace-state" buttons are tapped, these
70 // parameters will be passed to their corresponding JavaScript function calls.
71 void SetStateParams(const std::string& state_object,
72 const std::string& title,
73 const GURL& url) {
74 ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL());
75 std::string url_spec = url.possibly_invalid_spec();
76 NSString* set_params_script = [NSString
77 stringWithFormat:kUpdateStateParamsScriptFormat, state_object.c_str(),
78 title.c_str(), url_spec.c_str()];
79 ExecuteJavaScript(set_params_script);
80 }
81
82 // Executes JavaScript to check whether the onload text is visible.
83 bool IsOnLoadTextVisible() {
84 return [ExecuteJavaScript(kOnLoadCheckScript) boolValue];
85 }
86
87 // Executes JavaScript to check whether the no-op text is visible.
88 bool IsNoOpTextVisible() {
89 return [ExecuteJavaScript(kNoOpCheckScript) boolValue];
90 }
91
92 // Waits for the NoOp text to be visible.
93 void WaitForNoOpText() {
94 base::test::ios::WaitUntilCondition(^bool {
95 return IsNoOpTextVisible();
96 });
97 }
98
99 private:
100 GURL state_operations_url_;
101 };
102
103 // Tests that calling window.history.pushState() is a no-op for unresolvable
104 // URLs.
105 TEST_F(HistoryStateOperationsTest, NoOpPushUnresolvable) {
106 // Perform a window.history.pushState() with an unresolvable URL. This will
107 // clear the OnLoad and NoOp text, so checking below that the NoOp text is
108 // displayed and the OnLoad text is empty ensures that no navigation occurred
109 // as the result of the pushState() call.
110 std::string empty_state;
111 std::string empty_title;
112 GURL unresolvable_url("http://www.google.invalid");
113 SetStateParams(empty_state, empty_title, unresolvable_url);
114 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateId));
115 WaitForNoOpText();
116 }
117
118 // Tests that calling window.history.replaceState() is a no-op for unresolvable
119 // URLs.
120 TEST_F(HistoryStateOperationsTest, NoOpReplaceUnresolvable) {
121 // Perform a window.history.replaceState() with an unresolvable URL. This
122 // will clear the OnLoad and NoOp text, so checking below that the NoOp text
123 // is displayed and the OnLoad text is empty ensures that no navigation
124 // occurred as the result of the pushState() call.
125 std::string empty_state;
126 std::string empty_title;
127 GURL unresolvable_url("http://www.google.invalid");
128 SetStateParams(empty_state, empty_title, unresolvable_url);
129 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId));
130 WaitForNoOpText();
131 }
132
133 // Tests that calling window.history.pushState() is a no-op for URLs with a
134 // different scheme.
135 TEST_F(HistoryStateOperationsTest, NoOpPushDifferentScheme) {
136 // Perform a window.history.pushState() with a URL with a different scheme.
137 // This will clear the OnLoad and NoOp text, so checking below that the NoOp
138 // text is displayed and the OnLoad text is empty ensures that no navigation
139 // occurred as the result of the pushState() call.
140 std::string empty_state;
141 std::string empty_title;
142 GURL different_scheme_url("https://google.com");
143 ASSERT_TRUE(IsOnLoadTextVisible());
144 SetStateParams(empty_state, empty_title, different_scheme_url);
145 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateId));
146 WaitForNoOpText();
147 }
148
149 // Tests that calling window.history.replaceState() is a no-op for URLs with a
150 // different scheme.
151 TEST_F(HistoryStateOperationsTest, NoOpRelaceDifferentScheme) {
152 // Perform a window.history.replaceState() with a URL with a different scheme.
153 // This will clear the OnLoad and NoOp text, so checking below that the NoOp
154 // text is displayed and the OnLoad text is empty ensures that no navigation
155 // occurred as the result of the pushState() call.
156 std::string empty_state;
157 std::string empty_title;
158 GURL different_scheme_url("https://google.com");
159 ASSERT_TRUE(IsOnLoadTextVisible());
160 SetStateParams(empty_state, empty_title, different_scheme_url);
161 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId));
162 WaitForNoOpText();
163 }
164
165 // Tests that calling window.history.pushState() is a no-op for URLs with a
166 // origin differing from that of the current page.
167 TEST_F(HistoryStateOperationsTest, NoOpPushDifferentOrigin) {
168 // Perform a window.history.pushState() with a URL with a different origin.
169 // This will clear the OnLoad and NoOp text, so checking below that the NoOp
170 // text is displayed and the OnLoad text is empty ensures that no navigation
171 // occurred as the result of the pushState() call.
172 std::string empty_state;
173 std::string empty_title;
174 std::string new_port_string = base::IntToString(
175 web::test::HttpServer::GetSharedInstance().GetPort() + 1);
176 url::Replacements<char> port_replacement;
177 port_replacement.SetPort(new_port_string.c_str(),
178 url::Component(0, new_port_string.length()));
179 GURL different_origin_url =
180 state_operations_url().ReplaceComponents(port_replacement);
181 ASSERT_TRUE(IsOnLoadTextVisible());
182 SetStateParams(empty_state, empty_title, different_origin_url);
183 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateId));
184 WaitForNoOpText();
185 }
186
187 // Tests that calling window.history.replaceState() is a no-op for URLs with a
188 // origin differing from that of the current page.
189 TEST_F(HistoryStateOperationsTest, NoOpReplaceDifferentOrigin) {
190 // Perform a window.history.replaceState() with a URL with a different origin.
191 // This will clear the OnLoad and NoOp text, so checking below that the NoOp
192 // text is displayed and the OnLoad text is empty ensures that no navigation
193 // occurred as the result of the pushState() call.
194 std::string empty_state;
195 std::string empty_title;
196 std::string new_port_string = base::IntToString(
197 web::test::HttpServer::GetSharedInstance().GetPort() + 1);
198 url::Replacements<char> port_replacement;
199 port_replacement.SetPort(new_port_string.c_str(),
200 url::Component(0, new_port_string.length()));
201 GURL different_origin_url =
202 state_operations_url().ReplaceComponents(port_replacement);
203 ASSERT_TRUE(IsOnLoadTextVisible());
204 SetStateParams(empty_state, empty_title, different_origin_url);
205 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateId));
206 WaitForNoOpText();
207 }
OLDNEW
« no previous file with comments | « ios/web/BUILD.gn ('k') | ios/web/test/web_int_test.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698