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

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, 11 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 #import "base/mac/bind_objc_block.h"
Eugene But (OOO till 7-30) 2017/01/21 02:15:23 Do you need this and many other includes in this l
kkhorimoto 2017/01/24 04:08:15 Nope, removed.
6 #include "base/memory/ptr_util.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/test/ios/wait_util.h"
10 #import "ios/web/public/navigation_item.h"
11 #import "ios/web/public/navigation_manager.h"
12 #import "ios/web/public/test/http_server.h"
13 #include "ios/web/public/test/http_server_util.h"
14 #import "ios/web/public/test/web_view_interaction_test_util.h"
15 #import "ios/web/public/web_state/web_state.h"
16 #include "ios/web/public/web_state/web_state_observer.h"
17 #import "ios/web/test/web_int_test.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/gtest_mac.h"
20
21 namespace {
22
23 // URL for the test window.location test file. The page at this URL contains
24 // several buttons that trigger window.location commands. The page supports
25 // several JavaScript functions:
26 // - updateUrlToLoadText(), which takes a URL and updates a div on the page to
27 // contain that text. This URL is used as the parameter for window.location
28 // function calls triggered by button taps.
29 // - getUrl(), which returns the URL that was set via updateUrlToLoadText().
30 // - isOnLoadTextVisible(), which returns whether a placeholder string is
31 // present on the page. This string is added to the page in the onload event
32 // and is removed once a button is tapped. Verifying that the onload text is
33 // visible after tapping a button is equivalent to checking that a load has
34 // occurred as the result of the button tap.
35 const char kHistoryStateOperationsTestURL[] =
Eugene But (OOO till 7-30) 2017/01/21 02:15:23 Optional nit: Camel case Url would be more appropr
kkhorimoto 2017/01/24 04:08:15 Done.
36 "http://ios/testing/data/http_server_files/state_operations.html";
37
38 // Button IDs used in the window.location test page.
39 const char kPushStateID[] = "push-state";
Eugene But (OOO till 7-30) 2017/01/21 02:15:24 Same thing for Id
kkhorimoto 2017/01/24 04:08:16 Done.
40 const char kReplaceStateID[] = "replace-state";
41
42 // JavaScript functions on the history state test page.
43 NSString* const kUpdateStateParamsScriptFormat =
44 @"updateStateParams('%s', '%s', '%s')";
45 NSString* const kOnLoadCheckScript = @"isOnLoadPlaceholderTextVisible()";
46 NSString* const kNoOpCheckScript = @"isNoOpPlaceholderTextVisible()";
47
48 } // namespace
49
50 // Test fixture for integration tests involving html5 window.history state
51 // operations.
52 class HistoryStateOperationsTest : public web::WebIntTest {
53 protected:
54 void SetUp() override {
55 web::WebIntTest::SetUp();
56
57 // History state tests use file-based test pages.
58 web::test::SetUpFileBasedHttpServer();
59
60 // Load the history state test page.
61 state_operations_url_ =
62 web::test::HttpServer::MakeUrl(kHistoryStateOperationsTestURL);
63 LoadUrl(state_operations_url());
64 }
65
66 // The URL of the window.location test page.
67 const GURL& state_operations_url() { return state_operations_url_; }
68
69 // Set the parameters to use for state operations on the test page. This
Eugene But (OOO till 7-30) 2017/01/21 02:15:23 s/Set/Sets
kkhorimoto 2017/01/24 04:08:15 Done.
70 // function executes a script that populates JavaScript values on the test
71 // page. When the "push-state" or "replace-state" buttons are tapped, these
72 // parameters will be passed to their corresponding JavaScript function calls.
73 void SetStateParams(const std::string& state_object,
74 const std::string& title,
75 const GURL& url) {
76 ASSERT_EQ(state_operations_url(), GetLastCommittedItem()->GetURL());
77 std::string url_spec = url.possibly_invalid_spec();
78 NSString* set_params_script = [NSString
79 stringWithFormat:kUpdateStateParamsScriptFormat, state_object.c_str(),
80 title.c_str(), url_spec.c_str()];
81 ExecuteJavaScript(set_params_script);
82 }
83
84 // Executes JavaScript to check whether the onload text is visible.
85 bool IsOnLoadTextVisible() {
86 return [ExecuteJavaScript(kOnLoadCheckScript) boolValue];
87 }
88
89 // Executes JavaScript to check whether the no-op text is visible.
90 bool IsNoOpTextVisible() {
91 return [ExecuteJavaScript(kNoOpCheckScript) boolValue];
92 }
93
94 // Waits for the NoOp text to be visible and returns whether the text was
95 // shown.
96 bool LastOperationWasNoOp() {
97 __block bool operation_was_no_op = false;
98 base::test::ios::WaitUntilCondition(^bool {
99 operation_was_no_op = IsNoOpTextVisible();
100 return operation_was_no_op;
101 });
102 return operation_was_no_op;
Eugene But (OOO till 7-30) 2017/01/21 02:15:23 Can this ever return false? Should the result be v
103 }
104
105 private:
106 GURL state_operations_url_;
107 };
108
109 // Tests that calling window.history.[push/replace]State() is a no-op for
110 // unresolvable URLs.
111 TEST_F(HistoryStateOperationsTest, NoOpUnresolvable) {
112 std::string empty_state;
113 std::string empty_title;
114 GURL unresolvable_url("http://www.google.invalid");
115 // Perform a window.history.pushState() with an unresolvable URL. This will
116 // clear the OnLoad and NoOp text, so checking below that the NoOp text is
117 // displayed and the OnLoad text is empty ensures that no navigation occurred
118 // as the result of the pushState() call.
119 SetStateParams(empty_state, empty_title, unresolvable_url);
120 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateID));
121 EXPECT_TRUE(LastOperationWasNoOp());
122 // Reload the page and perform the same check with
Eugene But (OOO till 7-30) 2017/01/21 02:15:23 Should this be a separate test? Smaller tests are
kkhorimoto 2017/01/24 04:08:15 Done.
123 // window.history.replaceState().
124 LoadUrl(state_operations_url());
125 ASSERT_TRUE(IsOnLoadTextVisible());
126 SetStateParams(empty_state, empty_title, unresolvable_url);
127 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID));
128 EXPECT_TRUE(LastOperationWasNoOp());
129 }
130
131 // Tests that calling window.history.[push/replace]State() is a no-op for URLs
132 // with a different scheme.
133 TEST_F(HistoryStateOperationsTest, NoOpDifferentScheme) {
134 std::string empty_state;
135 std::string empty_title;
136 GURL different_scheme_url("https://google.com");
137 // Perform a window.history.pushState() with a URL with a different scheme.
138 // This will clear the OnLoad and NoOp text, so checking below that the NoOp
139 // text is displayed and the OnLoad text is empty ensures that no navigation
140 // occurred as the result of the pushState() call.
141 ASSERT_TRUE(IsOnLoadTextVisible());
142 SetStateParams(empty_state, empty_title, different_scheme_url);
143 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateID));
144 EXPECT_TRUE(LastOperationWasNoOp());
145 LoadUrl(state_operations_url());
Eugene But (OOO till 7-30) 2017/01/21 02:15:23 Should this be a separate test? Or at least have "
kkhorimoto 2017/01/24 04:08:15 Done.
146 ASSERT_TRUE(IsOnLoadTextVisible());
147 SetStateParams(empty_state, empty_title, different_scheme_url);
148 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID));
149 EXPECT_TRUE(LastOperationWasNoOp());
150 }
151
152 // Tests that calling window.history.[push/replace]State() is a no-op for URLs
153 // with a shceme differing from that of the current page.
154 TEST_F(HistoryStateOperationsTest, NoOpDifferentOrigin) {
155 std::string empty_state;
156 std::string empty_title;
157 GURL different_origin_url("http://localhost:1000");
Eugene But (OOO till 7-30) 2017/01/21 02:15:23 Do you want to use |HttpServer::GetPort() + 1|?
kkhorimoto 2017/01/24 04:08:15 Done.
158 // Perform a window.history.pushState() with a URL with a different origin.
159 // This will clear the OnLoad and NoOp text, so checking below that the NoOp
160 // text is displayed and the OnLoad text is empty ensures that no navigation
161 // occurred as the result of the pushState() call.
162 ASSERT_TRUE(IsOnLoadTextVisible());
163 SetStateParams(empty_state, empty_title, different_origin_url);
164 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kPushStateID));
165 EXPECT_TRUE(LastOperationWasNoOp());
166 LoadUrl(state_operations_url());
Eugene But (OOO till 7-30) 2017/01/21 02:15:23 Should this be a separate test? Or at least have "
kkhorimoto 2017/01/24 04:08:15 Done.
167 ASSERT_TRUE(IsOnLoadTextVisible());
168 SetStateParams(empty_state, empty_title, different_origin_url);
169 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), kReplaceStateID));
170 EXPECT_TRUE(LastOperationWasNoOp());
171 }
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