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

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

Issue 2599233002: Created test for window.location.assign. (Closed)
Patch Set: move shared code to WebIntTest 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"
6 #include "base/mac/foundation_util.h"
7 #include "base/memory/ptr_util.h"
8 #import "base/strings/sys_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 kWindowLocationTestURL[] =
36 "http://ios/testing/data/http_server_files/window_location.html";
37
38 // Button IDs used in the window.location test page.
39 const char kWindowLocationAssignID[] = "location-assign";
40
41 // JavaScript functions on the window.location test page.
42 NSString* const kUpdateURLScriptFormat = @"updateUrlToLoadText('%s')";
43 NSString* const kGetURLScript = @"getUrl()";
44 NSString* const kOnLoadCheckScript = @"isOnLoadTextVisible()";
45
46 // URL of a sample file-based page.
47 const char kSampleFileBasedURL[] =
48 "http://ios/testing/data/http_server_files/chromium_logo_page.html";
49
50 } // namespace
51
52 // Test fixture for window.location integration tests.
53 class WindowLocationTest : public web::WebIntTest {
54 protected:
55 void SetUp() override {
56 web::WebIntTest::SetUp();
57
58 // window.location tests use file-based test pages.
59 web::test::SetUpFileBasedHttpServer();
60
61 // Load the window.location test page.
62 window_location_url_ =
63 web::test::HttpServer::MakeUrl(kWindowLocationTestURL);
64 LoadUrl(window_location_url());
65 }
66
67 // The URL of the window.location test page.
68 const GURL& window_location_url() { return window_location_url_; }
69
70 // Executes JavaScript on the window.location test page to use |url| as the
71 // parameter for the window.location calls executed by tapping the buttons on
72 // the page.
73 void SetWindowLocationUrl(const GURL& url) {
74 ASSERT_EQ(window_location_url(), web_state()->GetLastCommittedURL());
75 std::string url_spec = url.possibly_invalid_spec();
76 NSString* set_url_script =
77 [NSString stringWithFormat:kUpdateURLScriptFormat, url_spec.c_str()];
78 ExecuteJavaScript(set_url_script);
79 NSString* injected_url =
80 base::mac::ObjCCastStrict<NSString>(ExecuteJavaScript(kGetURLScript));
81 ASSERT_EQ(url_spec, base::SysNSStringToUTF8(injected_url));
82 }
83
84 // Executes JavaScript on the window.location test page and returns whether
85 // |kOnLoadText| is visible.
86 bool IsOnLoadTextVisible() {
87 NSNumber* text_visible = base::mac::ObjCCastStrict<NSNumber>(
88 ExecuteJavaScript(kOnLoadCheckScript));
89 return [text_visible boolValue];
90 }
91
92 private:
93 GURL window_location_url_;
94 };
95
96 // Tests that calling window.location.assign() creates a new NavigationItem.
97 TEST_F(WindowLocationTest, Assign) {
98 // Navigate to about:blank so there is a forward entry to prune.
99 GURL about_blank("about:blank");
100 LoadUrl(about_blank);
101 web::NavigationItem* about_blank_item =
102 navigation_manager()->GetLastCommittedItem();
103
104 // Navigate back to the window.location test page.
105 ExecuteBlockAndWaitForLoad(window_location_url(), ^{
106 navigation_manager()->GoBack();
107 });
108
109 // Set the window.location test URL and tap the window.location.assign()
110 // button.
111 GURL sample_url = web::test::HttpServer::MakeUrl(kSampleFileBasedURL);
112 SetWindowLocationUrl(sample_url);
113 ExecuteBlockAndWaitForLoad(sample_url, ^{
114 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(),
115 kWindowLocationAssignID));
116 });
117
118 // Verify that |sample_url| was loaded and that |about_blank_item| was pruned.
119 EXPECT_EQ(sample_url, navigation_manager()->GetLastCommittedItem()->GetURL());
120 EXPECT_EQ(NSNotFound, GetIndexOfNavigationItem(about_blank_item));
121 }
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