| 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 #import <UIKit/UIKit.h> |    6 #import <UIKit/UIKit.h> | 
|    7 #import <WebKit/WebKit.h> |    7 #import <WebKit/WebKit.h> | 
|    8 #import <XCTest/XCTest.h> |    8 #import <XCTest/XCTest.h> | 
|    9  |    9  | 
|   10 #import "base/ios/block_types.h" |   10 #import "base/ios/block_types.h" | 
|   11 #include "base/strings/sys_string_conversions.h" |   11 #include "base/strings/sys_string_conversions.h" | 
|   12 #import "ios/testing/earl_grey/matchers.h" |   12 #import "ios/testing/earl_grey/matchers.h" | 
|   13 #import "ios/web/public/test/http_server/http_server.h" |   13 #import "ios/web/public/test/http_server/http_server.h" | 
|   14 #include "ios/web/public/test/http_server/http_server_util.h" |   14 #include "ios/web/public/test/http_server/http_server_util.h" | 
|   15 #import "ios/web/public/test/web_view_interaction_test_util.h" |   15 #import "ios/web/public/test/web_view_interaction_test_util.h" | 
|   16 #import "ios/web/shell/test/app/web_shell_test_util.h" |   16 #import "ios/web/shell/test/app/web_shell_test_util.h" | 
|   17 #include "ios/web/shell/test/app/web_view_interaction_test_util.h" |   17 #include "ios/web/shell/test/app/web_view_interaction_test_util.h" | 
|   18 #import "ios/web/shell/test/earl_grey/shell_actions.h" |   18 #import "ios/web/shell/test/earl_grey/shell_actions.h" | 
|   19 #import "ios/web/shell/test/earl_grey/shell_earl_grey.h" |   19 #import "ios/web/shell/test/earl_grey/shell_earl_grey.h" | 
|   20 #import "ios/web/shell/test/earl_grey/shell_matchers.h" |   20 #import "ios/web/shell/test/earl_grey/shell_matchers.h" | 
|   21 #import "ios/web/shell/test/earl_grey/web_shell_test_case.h" |   21 #import "ios/web/shell/test/earl_grey/web_shell_test_case.h" | 
|   22  |   22  | 
|   23 #if !defined(__has_feature) || !__has_feature(objc_arc) |   23 #if !defined(__has_feature) || !__has_feature(objc_arc) | 
|   24 #error "This file requires ARC support." |   24 #error "This file requires ARC support." | 
|   25 #endif |   25 #endif | 
|   26  |   26  | 
|   27 using testing::ContextMenuItemWithText; |   27 using testing::ContextMenuItemWithText; | 
|   28 using testing::ElementToDismissContextMenu; |   28 using testing::ElementToDismissContextMenu; | 
|   29 using web::WebViewContainingText; |  | 
|   30  |   29  | 
|   31 // Context menu test cases for the web shell. |   30 // Context menu test cases for the web shell. | 
|   32 @interface ContextMenuTestCase : WebShellTestCase |   31 @interface ContextMenuTestCase : WebShellTestCase | 
|   33 @end |   32 @end | 
|   34  |   33  | 
|   35 @implementation ContextMenuTestCase |   34 @implementation ContextMenuTestCase | 
|   36  |   35  | 
|   37 // Tests context menu appears on a regular link. |   36 // Tests context menu appears on a regular link. | 
|   38 - (void)testContextMenu { |   37 - (void)testContextMenu { | 
|   39   // Create map of canned responses and set up the test HTML server. |   38   // Create map of canned responses and set up the test HTML server. | 
|   40   std::map<GURL, std::string> responses; |   39   std::map<GURL, std::string> responses; | 
|   41   GURL initialURL = web::test::HttpServer::MakeUrl("http://contextMenuOpen"); |   40   GURL initialURL = web::test::HttpServer::MakeUrl("http://contextMenuOpen"); | 
|   42   GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); |   41   GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); | 
|   43   // The initial page contains a link to the destination URL. |   42   // The initial page contains a link to the destination URL. | 
|   44   std::string linkID = "link"; |   43   std::string linkID = "link"; | 
|   45   std::string linkText = "link for context menu"; |   44   std::string linkText = "link for context menu"; | 
|   46   responses[initialURL] = |   45   responses[initialURL] = | 
|   47       "<body>" |   46       "<body>" | 
|   48       "<a href='" + |   47       "<a href='" + | 
|   49       destinationURL.spec() + "' id='" + linkID + "'>" + linkText + |   48       destinationURL.spec() + "' id='" + linkID + "'>" + linkText + | 
|   50       "</a>" |   49       "</a>" | 
|   51       "</span></body>"; |   50       "</span></body>"; | 
|   52  |   51  | 
|   53   web::test::SetUpSimpleHttpServer(responses); |   52   web::test::SetUpSimpleHttpServer(responses); | 
|   54   [ShellEarlGrey loadURL:initialURL]; |   53   [ShellEarlGrey loadURL:initialURL]; | 
|   55   [[EarlGrey selectElementWithMatcher:WebViewContainingText(linkText)] |   54   [ShellEarlGrey waitForWebViewContainingText:linkText]; | 
|   56       assertWithMatcher:grey_notNil()]; |  | 
|   57  |   55  | 
|   58   [[EarlGrey selectElementWithMatcher:web::WebView()] |   56   [[EarlGrey selectElementWithMatcher:web::WebView()] | 
|   59       performAction:web::LongPressElementForContextMenu( |   57       performAction:web::LongPressElementForContextMenu( | 
|   60                         linkID, true /* menu should appear */)]; |   58                         linkID, true /* menu should appear */)]; | 
|   61  |   59  | 
|   62   id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); |   60   id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); | 
|   63  |   61  | 
|   64   // Context menu should have a "copy link" item. |   62   // Context menu should have a "copy link" item. | 
|   65   [[EarlGrey selectElementWithMatcher:copyItem] |   63   [[EarlGrey selectElementWithMatcher:copyItem] | 
|   66       assertWithMatcher:grey_notNil()]; |   64       assertWithMatcher:grey_notNil()]; | 
| (...skipping 18 matching lines...) Expand all  Loading... | 
|   85   std::string linkID = "link"; |   83   std::string linkID = "link"; | 
|   86   std::string linkText = "no-callout link"; |   84   std::string linkText = "no-callout link"; | 
|   87   responses[initialURL] = "<body><a href='" + destinationURL.spec() + |   85   responses[initialURL] = "<body><a href='" + destinationURL.spec() + | 
|   88                           "' style='-webkit-touch-callout: none' id='" + |   86                           "' style='-webkit-touch-callout: none' id='" + | 
|   89                           linkID + "'>" + linkText + |   87                           linkID + "'>" + linkText + | 
|   90                           "</a>" |   88                           "</a>" | 
|   91                           "</body>"; |   89                           "</body>"; | 
|   92  |   90  | 
|   93   web::test::SetUpSimpleHttpServer(responses); |   91   web::test::SetUpSimpleHttpServer(responses); | 
|   94   [ShellEarlGrey loadURL:initialURL]; |   92   [ShellEarlGrey loadURL:initialURL]; | 
|   95   [[EarlGrey selectElementWithMatcher:WebViewContainingText(linkText)] |   93   [ShellEarlGrey waitForWebViewContainingText:linkText]; | 
|   96       assertWithMatcher:grey_notNil()]; |  | 
|   97  |   94  | 
|   98   [[EarlGrey selectElementWithMatcher:web::WebView()] |   95   [[EarlGrey selectElementWithMatcher:web::WebView()] | 
|   99       performAction:web::LongPressElementForContextMenu( |   96       performAction:web::LongPressElementForContextMenu( | 
|  100                         linkID, false /* menu shouldn't appear */)]; |   97                         linkID, false /* menu shouldn't appear */)]; | 
|  101  |   98  | 
|  102   id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); |   99   id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); | 
|  103  |  100  | 
|  104   // Verify no context menu. |  101   // Verify no context menu. | 
|  105   [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; |  102   [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; | 
|  106 } |  103 } | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
|  119   std::string linkText = "ancestor no-callout link"; |  116   std::string linkText = "ancestor no-callout link"; | 
|  120   responses[initialURL] = |  117   responses[initialURL] = | 
|  121       "<body style='-webkit-touch-callout: none'>" |  118       "<body style='-webkit-touch-callout: none'>" | 
|  122       "<a href='" + |  119       "<a href='" + | 
|  123       destinationURL.spec() + "' id='" + linkID + "'>" + linkText + |  120       destinationURL.spec() + "' id='" + linkID + "'>" + linkText + | 
|  124       "</a>" |  121       "</a>" | 
|  125       "</body>"; |  122       "</body>"; | 
|  126  |  123  | 
|  127   web::test::SetUpSimpleHttpServer(responses); |  124   web::test::SetUpSimpleHttpServer(responses); | 
|  128   [ShellEarlGrey loadURL:initialURL]; |  125   [ShellEarlGrey loadURL:initialURL]; | 
|  129   [[EarlGrey selectElementWithMatcher:WebViewContainingText(linkText)] |  126   [ShellEarlGrey waitForWebViewContainingText:linkText]; | 
|  130       assertWithMatcher:grey_notNil()]; |  | 
|  131  |  127  | 
|  132   [[EarlGrey selectElementWithMatcher:web::WebView()] |  128   [[EarlGrey selectElementWithMatcher:web::WebView()] | 
|  133       performAction:web::LongPressElementForContextMenu( |  129       performAction:web::LongPressElementForContextMenu( | 
|  134                         linkID, false /* menu shouldn't appear */)]; |  130                         linkID, false /* menu shouldn't appear */)]; | 
|  135  |  131  | 
|  136   id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); |  132   id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); | 
|  137  |  133  | 
|  138   // Verify no context menu. |  134   // Verify no context menu. | 
|  139   [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; |  135   [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; | 
|  140 } |  136 } | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
|  154   responses[initialURL] = |  150   responses[initialURL] = | 
|  155       "<body style='-webkit-touch-callout: none'>" |  151       "<body style='-webkit-touch-callout: none'>" | 
|  156       "<a href='" + |  152       "<a href='" + | 
|  157       destinationURL.spec() + "' style='-webkit-touch-callout: default' id='" + |  153       destinationURL.spec() + "' style='-webkit-touch-callout: default' id='" + | 
|  158       linkID + "'>" + linkText + |  154       linkID + "'>" + linkText + | 
|  159       "</a>" |  155       "</a>" | 
|  160       "</body>"; |  156       "</body>"; | 
|  161  |  157  | 
|  162   web::test::SetUpSimpleHttpServer(responses); |  158   web::test::SetUpSimpleHttpServer(responses); | 
|  163   [ShellEarlGrey loadURL:initialURL]; |  159   [ShellEarlGrey loadURL:initialURL]; | 
|  164   [[EarlGrey selectElementWithMatcher:WebViewContainingText(linkText)] |  160   [ShellEarlGrey waitForWebViewContainingText:linkText]; | 
|  165       assertWithMatcher:grey_notNil()]; |  | 
|  166  |  161  | 
|  167   [[EarlGrey selectElementWithMatcher:web::WebView()] |  162   [[EarlGrey selectElementWithMatcher:web::WebView()] | 
|  168       performAction:web::LongPressElementForContextMenu( |  163       performAction:web::LongPressElementForContextMenu( | 
|  169                         linkID, true /* menu should appear */)]; |  164                         linkID, true /* menu should appear */)]; | 
|  170  |  165  | 
|  171   id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); |  166   id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); | 
|  172  |  167  | 
|  173   // Context menu should have a "copy link" item. |  168   // Context menu should have a "copy link" item. | 
|  174   [[EarlGrey selectElementWithMatcher:copyItem] |  169   [[EarlGrey selectElementWithMatcher:copyItem] | 
|  175       assertWithMatcher:grey_notNil()]; |  170       assertWithMatcher:grey_notNil()]; | 
|  176  |  171  | 
|  177   // Dismiss the context menu. |  172   // Dismiss the context menu. | 
|  178   [[EarlGrey selectElementWithMatcher:ElementToDismissContextMenu(@"Cancel")] |  173   [[EarlGrey selectElementWithMatcher:ElementToDismissContextMenu(@"Cancel")] | 
|  179       performAction:grey_tap()]; |  174       performAction:grey_tap()]; | 
|  180  |  175  | 
|  181   // Context menu should go away after the tap. |  176   // Context menu should go away after the tap. | 
|  182   [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; |  177   [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; | 
|  183 } |  178 } | 
|  184  |  179  | 
|  185 @end |  180 @end | 
| OLD | NEW |