| 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 <EarlGrey/EarlGrey.h> | 5 #import <EarlGrey/EarlGrey.h> |
| 6 | 6 |
| 7 #include "base/ios/ios_util.h" | 7 #include "base/ios/ios_util.h" |
| 8 #include "base/strings/sys_string_conversions.h" |
| 8 #include "components/content_settings/core/common/content_settings.h" | 9 #include "components/content_settings/core/common/content_settings.h" |
| 9 #include "ios/chrome/test/app/settings_test_util.h" | 10 #include "ios/chrome/test/app/settings_test_util.h" |
| 10 #import "ios/chrome/test/app/tab_test_util.h" | 11 #import "ios/chrome/test/app/tab_test_util.h" |
| 11 #include "ios/chrome/test/app/web_view_interaction_test_util.h" | 12 #include "ios/chrome/test/app/web_view_interaction_test_util.h" |
| 12 #import "ios/chrome/test/earl_grey/chrome_assertions.h" | 13 #import "ios/chrome/test/earl_grey/chrome_assertions.h" |
| 13 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" | 14 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 14 #import "ios/chrome/test/earl_grey/chrome_matchers.h" | 15 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 15 #import "ios/chrome/test/earl_grey/chrome_test_case.h" | 16 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 16 #import "ios/web/public/test/http_server.h" | 17 #import "ios/web/public/test/http_server.h" |
| 17 #include "ios/web/public/test/http_server_util.h" | 18 #include "ios/web/public/test/http_server_util.h" |
| 18 | 19 |
| 20 using base::SysUTF8ToNSString; |
| 19 using chrome_test_util::AssertMainTabCount; | 21 using chrome_test_util::AssertMainTabCount; |
| 20 using chrome_test_util::TapWebViewElementWithId; | 22 using chrome_test_util::TapWebViewElementWithId; |
| 21 using chrome_test_util::WebViewContainingText; | |
| 22 using web::test::HttpServer; | 23 using web::test::HttpServer; |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 // Test link text and ids. | 26 // Test link text and ids. |
| 26 const char kNamedWindowLink[] = "openWindowWithName"; | 27 const char kNamedWindowLink[] = "openWindowWithName"; |
| 27 const char kUnnamedWindowLink[] = "openWindowNoName"; | 28 const char kUnnamedWindowLink[] = "openWindowNoName"; |
| 28 | 29 |
| 29 // Web view text that indicates window's closed state. | 30 // Web view text that indicates window's closed state. |
| 30 const char kWindow2NeverOpen[] = "window2.closed: never opened"; | 31 const char kWindow2NeverOpen[] = "window2.closed: never opened"; |
| 31 const char kWindow1Open[] = "window1.closed: false"; | 32 const char kWindow1Open[] = "window1.closed: false"; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 51 chrome_test_util::SetContentSettingsBlockPopups(CONTENT_SETTING_DEFAULT); | 52 chrome_test_util::SetContentSettingsBlockPopups(CONTENT_SETTING_DEFAULT); |
| 52 [super tearDown]; | 53 [super tearDown]; |
| 53 } | 54 } |
| 54 | 55 |
| 55 - (void)setUp { | 56 - (void)setUp { |
| 56 [super setUp]; | 57 [super setUp]; |
| 57 // Open the test page. There should only be one tab open. | 58 // Open the test page. There should only be one tab open. |
| 58 const char kChildWindowTestURL[] = | 59 const char kChildWindowTestURL[] = |
| 59 "http://ios/testing/data/http_server_files/window_proxy.html"; | 60 "http://ios/testing/data/http_server_files/window_proxy.html"; |
| 60 [ChromeEarlGrey loadURL:HttpServer::MakeUrl(kChildWindowTestURL)]; | 61 [ChromeEarlGrey loadURL:HttpServer::MakeUrl(kChildWindowTestURL)]; |
| 61 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kNamedWindowLink)] | 62 [ChromeEarlGrey |
| 62 assertWithMatcher:grey_notNil()]; | 63 waitForWebViewContainingText:SysUTF8ToNSString(kNamedWindowLink)]; |
| 63 AssertMainTabCount(1); | 64 AssertMainTabCount(1); |
| 64 } | 65 } |
| 65 | 66 |
| 66 // Tests that multiple calls to window.open() with the same window name returns | 67 // Tests that multiple calls to window.open() with the same window name returns |
| 67 // the same window object. | 68 // the same window object. |
| 68 - (void)test2ChildWindowsWithName { | 69 - (void)test2ChildWindowsWithName { |
| 69 // Open two windows with the same name. | 70 // Open two windows with the same name. |
| 70 TapWebViewElementWithId(kNamedWindowLink); | 71 TapWebViewElementWithId(kNamedWindowLink); |
| 71 AssertMainTabCount(2); | 72 AssertMainTabCount(2); |
| 72 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 73 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 73 | 74 |
| 74 TapWebViewElementWithId(kNamedWindowLink); | 75 TapWebViewElementWithId(kNamedWindowLink); |
| 75 AssertMainTabCount(2); | 76 AssertMainTabCount(2); |
| 76 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 77 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 77 | 78 |
| 78 // Check that they're the same window. | 79 // Check that they're the same window. |
| 79 TapWebViewElementWithId("compareNamedWindows"); | 80 TapWebViewElementWithId("compareNamedWindows"); |
| 80 const char kWindowsEqualText[] = "named windows equal: true"; | 81 const char kWindowsEqualText[] = "named windows equal: true"; |
| 81 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindowsEqualText)] | 82 [ChromeEarlGrey |
| 82 assertWithMatcher:grey_notNil()]; | 83 waitForWebViewContainingText:SysUTF8ToNSString(kWindowsEqualText)]; |
| 83 } | 84 } |
| 84 | 85 |
| 85 // Tests that multiple calls to window.open() with no window name passed in | 86 // Tests that multiple calls to window.open() with no window name passed in |
| 86 // returns a unique window object each time. | 87 // returns a unique window object each time. |
| 87 - (void)test2ChildWindowsWithoutName { | 88 - (void)test2ChildWindowsWithoutName { |
| 88 // Open two unnamed windows. | 89 // Open two unnamed windows. |
| 89 TapWebViewElementWithId(kUnnamedWindowLink); | 90 TapWebViewElementWithId(kUnnamedWindowLink); |
| 90 AssertMainTabCount(2); | 91 AssertMainTabCount(2); |
| 91 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 92 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 92 | 93 |
| 93 TapWebViewElementWithId(kUnnamedWindowLink); | 94 TapWebViewElementWithId(kUnnamedWindowLink); |
| 94 AssertMainTabCount(3); | 95 AssertMainTabCount(3); |
| 95 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 96 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 96 | 97 |
| 97 // Check that they aren't the same window object. | 98 // Check that they aren't the same window object. |
| 98 TapWebViewElementWithId("compareUnnamedWindows"); | 99 TapWebViewElementWithId("compareUnnamedWindows"); |
| 99 const char kWindowsEqualText[] = "unnamed windows equal: false"; | 100 const char kWindowsEqualText[] = "unnamed windows equal: false"; |
| 100 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindowsEqualText)] | 101 [ChromeEarlGrey |
| 101 assertWithMatcher:grey_notNil()]; | 102 waitForWebViewContainingText:SysUTF8ToNSString(kWindowsEqualText)]; |
| 102 } | 103 } |
| 103 | 104 |
| 104 // Tests that calling window.open() with a name returns a different window | 105 // Tests that calling window.open() with a name returns a different window |
| 105 // object than a subsequent call to window.open() without a name. | 106 // object than a subsequent call to window.open() without a name. |
| 106 - (void)testChildWindowsWithAndWithoutName { | 107 - (void)testChildWindowsWithAndWithoutName { |
| 107 // Open a named window. | 108 // Open a named window. |
| 108 TapWebViewElementWithId(kNamedWindowLink); | 109 TapWebViewElementWithId(kNamedWindowLink); |
| 109 AssertMainTabCount(2); | 110 AssertMainTabCount(2); |
| 110 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 111 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 111 | 112 |
| 112 // Open an unnamed window. | 113 // Open an unnamed window. |
| 113 TapWebViewElementWithId(kUnnamedWindowLink); | 114 TapWebViewElementWithId(kUnnamedWindowLink); |
| 114 AssertMainTabCount(3); | 115 AssertMainTabCount(3); |
| 115 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 116 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 116 | 117 |
| 117 // Check that they aren't the same window object. | 118 // Check that they aren't the same window object. |
| 118 TapWebViewElementWithId("compareNamedAndUnnamedWindows"); | 119 TapWebViewElementWithId("compareNamedAndUnnamedWindows"); |
| 119 const char kWindowsEqualText[] = "named and unnamed equal: false"; | 120 const char kWindowsEqualText[] = "named and unnamed equal: false"; |
| 120 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindowsEqualText)] | 121 [ChromeEarlGrey |
| 121 assertWithMatcher:grey_notNil()]; | 122 waitForWebViewContainingText:SysUTF8ToNSString(kWindowsEqualText)]; |
| 122 } | 123 } |
| 123 | 124 |
| 124 // Tests that window.closed is correctly set to true when the corresponding tab | 125 // Tests that window.closed is correctly set to true when the corresponding tab |
| 125 // is closed. Verifies that calling window.open() multiple times with the same | 126 // is closed. Verifies that calling window.open() multiple times with the same |
| 126 // name returns the same window object, and thus closing the corresponding tab | 127 // name returns the same window object, and thus closing the corresponding tab |
| 127 // results in window.closed being set to true for all references to the window | 128 // results in window.closed being set to true for all references to the window |
| 128 // object for that tab. | 129 // object for that tab. |
| 129 - (void)testWindowClosedWithName { | 130 - (void)testWindowClosedWithName { |
| 130 TapWebViewElementWithId("openWindowWithName"); | 131 TapWebViewElementWithId("openWindowWithName"); |
| 131 AssertMainTabCount(2); | 132 AssertMainTabCount(2); |
| 132 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 133 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 133 | 134 |
| 134 // Check that named window 1 is opened and named window 2 isn't. | 135 // Check that named window 1 is opened and named window 2 isn't. |
| 135 const char kCheckWindow1Link[] = "checkNamedWindow1Closed"; | 136 const char kCheckWindow1Link[] = "checkNamedWindow1Closed"; |
| 136 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kCheckWindow1Link)] | 137 [ChromeEarlGrey |
| 137 assertWithMatcher:grey_notNil()]; | 138 waitForWebViewContainingText:SysUTF8ToNSString(kCheckWindow1Link)]; |
| 138 TapWebViewElementWithId(kCheckWindow1Link); | 139 TapWebViewElementWithId(kCheckWindow1Link); |
| 139 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow1Open)] | 140 [ChromeEarlGrey waitForWebViewContainingText:SysUTF8ToNSString(kWindow1Open)]; |
| 140 assertWithMatcher:grey_notNil()]; | |
| 141 const char kCheckWindow2Link[] = "checkNamedWindow2Closed"; | 141 const char kCheckWindow2Link[] = "checkNamedWindow2Closed"; |
| 142 TapWebViewElementWithId(kCheckWindow2Link); | 142 TapWebViewElementWithId(kCheckWindow2Link); |
| 143 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow2NeverOpen)] | 143 [ChromeEarlGrey |
| 144 assertWithMatcher:grey_notNil()]; | 144 waitForWebViewContainingText:SysUTF8ToNSString(kWindow2NeverOpen)]; |
| 145 | 145 |
| 146 // Open another window with the same name. Check that named window 2 is now | 146 // Open another window with the same name. Check that named window 2 is now |
| 147 // opened. | 147 // opened. |
| 148 TapWebViewElementWithId("openWindowWithName"); | 148 TapWebViewElementWithId("openWindowWithName"); |
| 149 AssertMainTabCount(2); | 149 AssertMainTabCount(2); |
| 150 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 150 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 151 TapWebViewElementWithId(kCheckWindow2Link); | 151 TapWebViewElementWithId(kCheckWindow2Link); |
| 152 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow2Open)] | 152 [ChromeEarlGrey waitForWebViewContainingText:SysUTF8ToNSString(kWindow2Open)]; |
| 153 assertWithMatcher:grey_notNil()]; | |
| 154 | 153 |
| 155 // Close the opened window. Check that named window 1 and 2 are both closed. | 154 // Close the opened window. Check that named window 1 and 2 are both closed. |
| 156 chrome_test_util::CloseTabAtIndex(1); | 155 chrome_test_util::CloseTabAtIndex(1); |
| 157 AssertMainTabCount(1); | 156 AssertMainTabCount(1); |
| 158 TapWebViewElementWithId(kCheckWindow1Link); | 157 TapWebViewElementWithId(kCheckWindow1Link); |
| 159 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow1Closed)] | 158 [ChromeEarlGrey |
| 160 assertWithMatcher:grey_notNil()]; | 159 waitForWebViewContainingText:SysUTF8ToNSString(kWindow1Closed)]; |
| 161 TapWebViewElementWithId(kCheckWindow2Link); | 160 TapWebViewElementWithId(kCheckWindow2Link); |
| 162 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow2Closed)] | 161 [ChromeEarlGrey |
| 163 assertWithMatcher:grey_notNil()]; | 162 waitForWebViewContainingText:SysUTF8ToNSString(kWindow2Closed)]; |
| 164 } | 163 } |
| 165 | 164 |
| 166 // Tests that closing a tab will set window.closed to true for only | 165 // Tests that closing a tab will set window.closed to true for only |
| 167 // corresponding window object and not for any other window objects. | 166 // corresponding window object and not for any other window objects. |
| 168 - (void)testWindowClosedWithoutName { | 167 - (void)testWindowClosedWithoutName { |
| 169 TapWebViewElementWithId("openWindowNoName"); | 168 TapWebViewElementWithId("openWindowNoName"); |
| 170 AssertMainTabCount(2); | 169 AssertMainTabCount(2); |
| 171 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 170 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 172 | 171 |
| 173 // Check that unnamed window 1 is opened and unnamed window 2 isn't. | 172 // Check that unnamed window 1 is opened and unnamed window 2 isn't. |
| 174 const char kCheckWindow1Link[] = "checkUnnamedWindow1Closed"; | 173 const char kCheckWindow1Link[] = "checkUnnamedWindow1Closed"; |
| 175 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kCheckWindow1Link)] | 174 [ChromeEarlGrey |
| 176 assertWithMatcher:grey_notNil()]; | 175 waitForWebViewContainingText:SysUTF8ToNSString(kCheckWindow1Link)]; |
| 177 TapWebViewElementWithId(kCheckWindow1Link); | 176 TapWebViewElementWithId(kCheckWindow1Link); |
| 178 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow1Open)] | 177 [ChromeEarlGrey waitForWebViewContainingText:SysUTF8ToNSString(kWindow1Open)]; |
| 179 assertWithMatcher:grey_notNil()]; | |
| 180 const char kCheckWindow2Link[] = "checkUnnamedWindow2Closed"; | 178 const char kCheckWindow2Link[] = "checkUnnamedWindow2Closed"; |
| 181 TapWebViewElementWithId(kCheckWindow2Link); | 179 TapWebViewElementWithId(kCheckWindow2Link); |
| 182 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow2NeverOpen)] | 180 [ChromeEarlGrey |
| 183 assertWithMatcher:grey_notNil()]; | 181 waitForWebViewContainingText:SysUTF8ToNSString(kWindow2NeverOpen)]; |
| 184 | 182 |
| 185 // Open another unnamed window. Check that unnamed window 2 is now opened. | 183 // Open another unnamed window. Check that unnamed window 2 is now opened. |
| 186 TapWebViewElementWithId("openWindowNoName"); | 184 TapWebViewElementWithId("openWindowNoName"); |
| 187 AssertMainTabCount(3); | 185 AssertMainTabCount(3); |
| 188 chrome_test_util::SelectTabAtIndexInCurrentMode(0); | 186 chrome_test_util::SelectTabAtIndexInCurrentMode(0); |
| 189 TapWebViewElementWithId(kCheckWindow2Link); | 187 TapWebViewElementWithId(kCheckWindow2Link); |
| 190 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow2Open)] | 188 [ChromeEarlGrey waitForWebViewContainingText:SysUTF8ToNSString(kWindow2Open)]; |
| 191 assertWithMatcher:grey_notNil()]; | |
| 192 | 189 |
| 193 // Close the first opened window. Check that unnamed window 1 is closed and | 190 // Close the first opened window. Check that unnamed window 1 is closed and |
| 194 // unnamed window 2 is still open. | 191 // unnamed window 2 is still open. |
| 195 chrome_test_util::CloseTabAtIndex(1); | 192 chrome_test_util::CloseTabAtIndex(1); |
| 196 TapWebViewElementWithId(kCheckWindow1Link); | 193 TapWebViewElementWithId(kCheckWindow1Link); |
| 197 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow1Closed)] | 194 [ChromeEarlGrey |
| 198 assertWithMatcher:grey_notNil()]; | 195 waitForWebViewContainingText:SysUTF8ToNSString(kWindow1Closed)]; |
| 199 TapWebViewElementWithId(kCheckWindow2Link); | 196 TapWebViewElementWithId(kCheckWindow2Link); |
| 200 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow2Open)] | 197 [ChromeEarlGrey waitForWebViewContainingText:SysUTF8ToNSString(kWindow2Open)]; |
| 201 assertWithMatcher:grey_notNil()]; | |
| 202 | 198 |
| 203 // Close the second opened window. Check that unnamed window 2 is closed. | 199 // Close the second opened window. Check that unnamed window 2 is closed. |
| 204 chrome_test_util::CloseTabAtIndex(1); | 200 chrome_test_util::CloseTabAtIndex(1); |
| 205 TapWebViewElementWithId(kCheckWindow2Link); | 201 TapWebViewElementWithId(kCheckWindow2Link); |
| 206 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kWindow2Closed)] | 202 [ChromeEarlGrey |
| 207 assertWithMatcher:grey_notNil()]; | 203 waitForWebViewContainingText:SysUTF8ToNSString(kWindow2Closed)]; |
| 208 } | 204 } |
| 209 | 205 |
| 210 @end | 206 @end |
| OLD | NEW |