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