| 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 "base/mac/bind_objc_block.h" | 5 #import "base/mac/bind_objc_block.h" |
| 6 #include "base/mac/foundation_util.h" | 6 #include "base/mac/foundation_util.h" |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #import "base/strings/sys_string_conversions.h" | 8 #import "base/strings/sys_string_conversions.h" |
| 9 #include "base/test/ios/wait_util.h" | 9 #include "base/test/ios/wait_util.h" |
| 10 #import "ios/web/public/navigation_item.h" | 10 #import "ios/web/public/navigation_item.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 // Button IDs used in the window.location test page. | 38 // Button IDs used in the window.location test page. |
| 39 const char kWindowLocationAssignID[] = "location-assign"; | 39 const char kWindowLocationAssignID[] = "location-assign"; |
| 40 const char kWindowLocationReplaceID[] = "location-replace"; | 40 const char kWindowLocationReplaceID[] = "location-replace"; |
| 41 const char kWindowLocationReloadID[] = "location-reload"; | 41 const char kWindowLocationReloadID[] = "location-reload"; |
| 42 const char kWindowLocationSetToDOMStringID[] = "set-location-to-dom-string"; | 42 const char kWindowLocationSetToDOMStringID[] = "set-location-to-dom-string"; |
| 43 | 43 |
| 44 // JavaScript functions on the window.location test page. | 44 // JavaScript functions on the window.location test page. |
| 45 NSString* const kUpdateURLScriptFormat = @"updateUrlToLoadText('%s')"; | 45 NSString* const kUpdateURLScriptFormat = @"updateUrlToLoadText('%s')"; |
| 46 NSString* const kGetURLScript = @"getUrl()"; | 46 NSString* const kGetURLScript = @"getUrl()"; |
| 47 NSString* const kOnLoadCheckScript = @"isOnLoadTextVisible()"; | 47 NSString* const kOnLoadCheckScript = @"isOnLoadTextVisible()"; |
| 48 NSString* const kNoOpCheckScript = @"isNoOpTextVisible()"; |
| 48 | 49 |
| 49 // URL of a sample file-based page. | 50 // URL of a sample file-based page. |
| 50 const char kSampleFileBasedURL[] = | 51 const char kSampleFileBasedURL[] = |
| 51 "http://ios/testing/data/http_server_files/chromium_logo_page.html"; | 52 "http://ios/testing/data/http_server_files/chromium_logo_page.html"; |
| 52 | 53 |
| 53 } // namespace | 54 } // namespace |
| 54 | 55 |
| 55 // Test fixture for window.location integration tests. | 56 // Test fixture for window.location integration tests. |
| 56 class WindowLocationTest : public web::WebIntTest { | 57 class WindowLocationTest : public web::WebIntTest { |
| 57 protected: | 58 protected: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 85 } | 86 } |
| 86 | 87 |
| 87 // Executes JavaScript on the window.location test page and returns whether | 88 // Executes JavaScript on the window.location test page and returns whether |
| 88 // |kOnLoadText| is visible. | 89 // |kOnLoadText| is visible. |
| 89 bool IsOnLoadTextVisible() { | 90 bool IsOnLoadTextVisible() { |
| 90 NSNumber* text_visible = base::mac::ObjCCastStrict<NSNumber>( | 91 NSNumber* text_visible = base::mac::ObjCCastStrict<NSNumber>( |
| 91 ExecuteJavaScript(kOnLoadCheckScript)); | 92 ExecuteJavaScript(kOnLoadCheckScript)); |
| 92 return [text_visible boolValue]; | 93 return [text_visible boolValue]; |
| 93 } | 94 } |
| 94 | 95 |
| 96 // Executes JavaScript on the window.location test page and returns whether |
| 97 // the no-op text is visible. It is displayed 0.5 seconds after a button is |
| 98 // tapped, and can be used to verify that a navigation did not occur. |
| 99 bool IsNoOpTextVisible() { |
| 100 NSNumber* text_visible = base::mac::ObjCCastStrict<NSNumber>( |
| 101 ExecuteJavaScript(kNoOpCheckScript)); |
| 102 return [text_visible boolValue]; |
| 103 } |
| 104 |
| 95 private: | 105 private: |
| 96 GURL window_location_url_; | 106 GURL window_location_url_; |
| 97 }; | 107 }; |
| 98 | 108 |
| 99 // Tests that calling window.location.assign() creates a new NavigationItem. | 109 // Tests that calling window.location.assign() creates a new NavigationItem. |
| 100 TEST_F(WindowLocationTest, Assign) { | 110 TEST_F(WindowLocationTest, Assign) { |
| 101 // Navigate to about:blank so there is a forward entry to prune. | 111 // Navigate to about:blank so there is a forward entry to prune. |
| 102 GURL about_blank("about:blank"); | 112 GURL about_blank("about:blank"); |
| 103 LoadUrl(about_blank); | 113 LoadUrl(about_blank); |
| 104 web::NavigationItem* about_blank_item = | 114 web::NavigationItem* about_blank_item = |
| (...skipping 15 matching lines...) Expand all Loading... |
| 120 | 130 |
| 121 // Verify that |sample_url| was loaded and that |about_blank_item| was pruned. | 131 // Verify that |sample_url| was loaded and that |about_blank_item| was pruned. |
| 122 EXPECT_EQ(sample_url, navigation_manager()->GetLastCommittedItem()->GetURL()); | 132 EXPECT_EQ(sample_url, navigation_manager()->GetLastCommittedItem()->GetURL()); |
| 123 EXPECT_EQ(NSNotFound, GetIndexOfNavigationItem(about_blank_item)); | 133 EXPECT_EQ(NSNotFound, GetIndexOfNavigationItem(about_blank_item)); |
| 124 } | 134 } |
| 125 | 135 |
| 126 // Tests that calling window.location.assign() with an unresolvable URL loads | 136 // Tests that calling window.location.assign() with an unresolvable URL loads |
| 127 // about:blank. | 137 // about:blank. |
| 128 TEST_F(WindowLocationTest, WindowLocationAssignUnresolvable) { | 138 TEST_F(WindowLocationTest, WindowLocationAssignUnresolvable) { |
| 129 // Attempt to call window.location.assign() using an unresolvable URL. | 139 // Attempt to call window.location.assign() using an unresolvable URL. |
| 130 GURL about_blank("about:blank"); | |
| 131 GURL unresolvable_url("http:https:not a url"); | 140 GURL unresolvable_url("http:https:not a url"); |
| 132 SetWindowLocationUrl(unresolvable_url); | 141 SetWindowLocationUrl(unresolvable_url); |
| 133 ExecuteBlockAndWaitForLoad(about_blank, ^{ | 142 ASSERT_TRUE( |
| 134 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), | 143 web::test::TapWebViewElementWithId(web_state(), kWindowLocationAssignID)); |
| 135 kWindowLocationAssignID)); | 144 |
| 145 // Wait for the no-op text to appear. |
| 146 base::test::ios::WaitUntilCondition(^bool { |
| 147 return IsNoOpTextVisible(); |
| 136 }); | 148 }); |
| 137 | |
| 138 // Verify that about:blank was actually loaded. | |
| 139 EXPECT_EQ(about_blank, | |
| 140 navigation_manager()->GetLastCommittedItem()->GetURL()); | |
| 141 } | 149 } |
| 142 | 150 |
| 143 // Tests that calling window.location.replace() doesn't create a new | 151 // Tests that calling window.location.replace() doesn't create a new |
| 144 // NavigationItem. | 152 // NavigationItem. |
| 145 // TODO(crbug.com/307072): Enable test when location.replace is fixed. | 153 // TODO(crbug.com/307072): Enable test when location.replace is fixed. |
| 146 TEST_F(WindowLocationTest, DISABLED_Replace) { | 154 TEST_F(WindowLocationTest, DISABLED_Replace) { |
| 147 // Navigate to about:blank so there is a forward entry. | 155 // Navigate to about:blank so there is a forward entry. |
| 148 GURL about_blank("about:blank"); | 156 GURL about_blank("about:blank"); |
| 149 LoadUrl(about_blank); | 157 LoadUrl(about_blank); |
| 150 web::NavigationItem* about_blank_item = | 158 web::NavigationItem* about_blank_item = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 165 }); | 173 }); |
| 166 | 174 |
| 167 // Verify that |sample_url| was loaded and that |about_blank_item| was pruned. | 175 // Verify that |sample_url| was loaded and that |about_blank_item| was pruned. |
| 168 web::NavigationItem* current_item = | 176 web::NavigationItem* current_item = |
| 169 navigation_manager()->GetLastCommittedItem(); | 177 navigation_manager()->GetLastCommittedItem(); |
| 170 EXPECT_EQ(sample_url, current_item->GetURL()); | 178 EXPECT_EQ(sample_url, current_item->GetURL()); |
| 171 EXPECT_EQ(GetIndexOfNavigationItem(current_item) + 1, | 179 EXPECT_EQ(GetIndexOfNavigationItem(current_item) + 1, |
| 172 GetIndexOfNavigationItem(about_blank_item)); | 180 GetIndexOfNavigationItem(about_blank_item)); |
| 173 } | 181 } |
| 174 | 182 |
| 175 // Tests that calling window.location.replace() with an unresolvable URL loads | 183 // Tests that calling window.location.replace() with an unresolvable URL is a |
| 176 // about:blank. | 184 // no-op. |
| 177 TEST_F(WindowLocationTest, WindowLocationReplaceUnresolvable) { | 185 TEST_F(WindowLocationTest, WindowLocationReplaceUnresolvable) { |
| 178 // Attempt to call window.location.assign() using an unresolvable URL. | 186 // Attempt to call window.location.assign() using an unresolvable URL. |
| 179 GURL about_blank("about:blank"); | |
| 180 GURL unresolvable_url("http:https:not a url"); | 187 GURL unresolvable_url("http:https:not a url"); |
| 181 SetWindowLocationUrl(unresolvable_url); | 188 SetWindowLocationUrl(unresolvable_url); |
| 182 ExecuteBlockAndWaitForLoad(about_blank, ^{ | 189 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), |
| 183 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), | 190 kWindowLocationReplaceID)); |
| 184 kWindowLocationReplaceID)); | 191 |
| 192 // Wait for the no-op text to appear. |
| 193 base::test::ios::WaitUntilCondition(^bool { |
| 194 return IsNoOpTextVisible(); |
| 185 }); | 195 }); |
| 186 | |
| 187 // Verify that about:blank was actually loaded. | |
| 188 EXPECT_EQ(about_blank, | |
| 189 navigation_manager()->GetLastCommittedItem()->GetURL()); | |
| 190 } | 196 } |
| 191 | 197 |
| 192 // Tests that calling window.location.reload() causes an onload event to occur. | 198 // Tests that calling window.location.reload() causes an onload event to occur. |
| 193 TEST_F(WindowLocationTest, WindowLocationReload) { | 199 TEST_F(WindowLocationTest, WindowLocationReload) { |
| 194 // Tap the window.location.reload() button. | 200 // Tap the window.location.reload() button. |
| 195 ExecuteBlockAndWaitForLoad(window_location_url(), ^{ | 201 ExecuteBlockAndWaitForLoad(window_location_url(), ^{ |
| 196 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), | 202 ASSERT_TRUE(web::test::TapWebViewElementWithId(web_state(), |
| 197 kWindowLocationReloadID)); | 203 kWindowLocationReloadID)); |
| 198 }); | 204 }); |
| 199 | 205 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 222 SetWindowLocationUrl(sample_url); | 228 SetWindowLocationUrl(sample_url); |
| 223 ExecuteBlockAndWaitForLoad(sample_url, ^{ | 229 ExecuteBlockAndWaitForLoad(sample_url, ^{ |
| 224 ASSERT_TRUE(web::test::TapWebViewElementWithId( | 230 ASSERT_TRUE(web::test::TapWebViewElementWithId( |
| 225 web_state(), kWindowLocationSetToDOMStringID)); | 231 web_state(), kWindowLocationSetToDOMStringID)); |
| 226 }); | 232 }); |
| 227 | 233 |
| 228 // Verify that |sample_url| was loaded and that |about_blank_item| was pruned. | 234 // Verify that |sample_url| was loaded and that |about_blank_item| was pruned. |
| 229 EXPECT_EQ(sample_url, navigation_manager()->GetLastCommittedItem()->GetURL()); | 235 EXPECT_EQ(sample_url, navigation_manager()->GetLastCommittedItem()->GetURL()); |
| 230 EXPECT_EQ(NSNotFound, GetIndexOfNavigationItem(about_blank_item)); | 236 EXPECT_EQ(NSNotFound, GetIndexOfNavigationItem(about_blank_item)); |
| 231 } | 237 } |
| OLD | NEW |