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