| 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/disabled_test_macros.h" | 12 #import "ios/testing/earl_grey/disabled_test_macros.h" |
| 13 #import "ios/testing/earl_grey/matchers.h" | 13 #import "ios/testing/earl_grey/matchers.h" |
| 14 #import "ios/web/public/test/http_server.h" | 14 #import "ios/web/public/test/http_server.h" |
| 15 #include "ios/web/public/test/http_server_util.h" | 15 #include "ios/web/public/test/http_server_util.h" |
| 16 #import "ios/web/public/test/web_view_interaction_test_util.h" | 16 #import "ios/web/public/test/web_view_interaction_test_util.h" |
| 17 #import "ios/web/shell/test/app/web_shell_test_util.h" | 17 #import "ios/web/shell/test/app/web_shell_test_util.h" |
| 18 #include "ios/web/shell/test/app/web_view_interaction_test_util.h" | 18 #include "ios/web/shell/test/app/web_view_interaction_test_util.h" |
| 19 #import "ios/web/shell/test/earl_grey/shell_base_test_case.h" | 19 #import "ios/web/shell/test/earl_grey/shell_base_test_case.h" |
| 20 #import "ios/web/shell/test/earl_grey/shell_actions.h" | 20 #import "ios/web/shell/test/earl_grey/shell_actions.h" |
| 21 #import "ios/web/shell/test/earl_grey/shell_earl_grey.h" | 21 #import "ios/web/shell/test/earl_grey/shell_earl_grey.h" |
| 22 #import "ios/web/shell/test/earl_grey/shell_matchers.h" | 22 #import "ios/web/shell/test/earl_grey/shell_matchers.h" |
| 23 | 23 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) | 24 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." | 25 #error "This file requires ARC support." |
| 26 #endif | 26 #endif |
| 27 | 27 |
| 28 using testing::ContextMenuItemWithText; | 28 using testing::ContextMenuItemWithText; |
| 29 using testing::ElementToDismissContextMenu; | 29 using testing::ElementToDismissContextMenu; |
| 30 using web::WebViewContainingText; |
| 30 | 31 |
| 31 // Context menu test cases for the web shell. | 32 // Context menu test cases for the web shell. |
| 32 @interface ContextMenuTestCase : ShellBaseTestCase | 33 @interface ContextMenuTestCase : ShellBaseTestCase |
| 33 @end | 34 @end |
| 34 | 35 |
| 35 @implementation ContextMenuTestCase | 36 @implementation ContextMenuTestCase |
| 36 | 37 |
| 37 // Tests context menu appears on a regular link. | 38 // Tests context menu appears on a regular link. |
| 38 - (void)testContextMenu { | 39 - (void)testContextMenu { |
| 39 // TODO(crbug.com/687546): Tests disabled on devices. | |
| 40 #if !TARGET_IPHONE_SIMULATOR | |
| 41 EARL_GREY_TEST_DISABLED(@"Disabled for devices because it is very flaky."); | |
| 42 #endif | |
| 43 | |
| 44 // Create map of canned responses and set up the test HTML server. | 40 // Create map of canned responses and set up the test HTML server. |
| 45 std::map<GURL, std::string> responses; | 41 std::map<GURL, std::string> responses; |
| 46 GURL initialURL = web::test::HttpServer::MakeUrl("http://contextMenuOpen"); | 42 GURL initialURL = web::test::HttpServer::MakeUrl("http://contextMenuOpen"); |
| 47 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); | 43 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); |
| 48 // The initial page contains a link to the destination URL. | 44 // The initial page contains a link to the destination URL. |
| 49 std::string linkID = "link"; | 45 std::string linkID = "link"; |
| 46 std::string linkText = "link for context menu"; |
| 50 responses[initialURL] = | 47 responses[initialURL] = |
| 51 "<body>" | 48 "<body>" |
| 52 "<a href='" + | 49 "<a href='" + |
| 53 destinationURL.spec() + "' id='" + linkID + | 50 destinationURL.spec() + "' id='" + linkID + "'>" + linkText + |
| 54 "'>link for context menu</a>" | 51 "</a>" |
| 55 "</span></body>"; | 52 "</span></body>"; |
| 56 | 53 |
| 57 web::test::SetUpSimpleHttpServer(responses); | 54 web::test::SetUpSimpleHttpServer(responses); |
| 58 [ShellEarlGrey loadURL:initialURL]; | 55 [ShellEarlGrey loadURL:initialURL]; |
| 56 [[EarlGrey selectElementWithMatcher:WebViewContainingText(linkText)] |
| 57 assertWithMatcher:grey_notNil()]; |
| 59 | 58 |
| 60 [[EarlGrey selectElementWithMatcher:web::WebView()] | 59 [[EarlGrey selectElementWithMatcher:web::WebView()] |
| 61 performAction:web::longPressElementForContextMenu( | 60 performAction:web::longPressElementForContextMenu( |
| 62 linkID, true /* menu should appear */)]; | 61 linkID, true /* menu should appear */)]; |
| 63 | 62 |
| 64 id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); | 63 id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); |
| 65 | 64 |
| 66 // Context menu should have a "copy link" item. | 65 // Context menu should have a "copy link" item. |
| 67 [[EarlGrey selectElementWithMatcher:copyItem] | 66 [[EarlGrey selectElementWithMatcher:copyItem] |
| 68 assertWithMatcher:grey_notNil()]; | 67 assertWithMatcher:grey_notNil()]; |
| 69 | 68 |
| 70 // Dismiss the context menu. | 69 // Dismiss the context menu. |
| 71 [[EarlGrey selectElementWithMatcher:ElementToDismissContextMenu(@"Cancel")] | 70 [[EarlGrey selectElementWithMatcher:ElementToDismissContextMenu(@"Cancel")] |
| 72 performAction:grey_tap()]; | 71 performAction:grey_tap()]; |
| 73 | 72 |
| 74 // Context menu should go away after the tap. | 73 // Context menu should go away after the tap. |
| 75 [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; | 74 [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; |
| 76 } | 75 } |
| 77 | 76 |
| 78 // Tests context menu on element that has WebkitTouchCallout set to none. | 77 // Tests context menu on element that has WebkitTouchCallout set to none. |
| 79 - (void)testContextMenuWebkitTouchCalloutNone { | 78 - (void)testContextMenuWebkitTouchCalloutNone { |
| 80 // Create map of canned responses and set up the test HTML server. | 79 // Create map of canned responses and set up the test HTML server. |
| 81 std::map<GURL, std::string> responses; | 80 std::map<GURL, std::string> responses; |
| 82 GURL initialURL = | 81 GURL initialURL = |
| 83 web::test::HttpServer::MakeUrl("http://contextMenuDisabledByWebkit"); | 82 web::test::HttpServer::MakeUrl("http://contextMenuDisabledByWebkit"); |
| 84 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); | 83 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); |
| 85 // The initial page contains a link to the destination URL that has an | 84 // The initial page contains a link to the destination URL that has an |
| 86 // ancestor that disables the context menu via -webkit-touch-callout. | 85 // ancestor that disables the context menu via -webkit-touch-callout. |
| 87 std::string linkID = "link"; | 86 std::string linkID = "link"; |
| 87 std::string linkText = "no-callout link"; |
| 88 responses[initialURL] = "<body><a href='" + destinationURL.spec() + | 88 responses[initialURL] = "<body><a href='" + destinationURL.spec() + |
| 89 "' style='-webkit-touch-callout: none' id='" + | 89 "' style='-webkit-touch-callout: none' id='" + |
| 90 linkID + | 90 linkID + "'>" + linkText + |
| 91 "'>no-callout link</a>" | 91 "</a>" |
| 92 "</body>"; | 92 "</body>"; |
| 93 | 93 |
| 94 web::test::SetUpSimpleHttpServer(responses); | 94 web::test::SetUpSimpleHttpServer(responses); |
| 95 [ShellEarlGrey loadURL:initialURL]; | 95 [ShellEarlGrey loadURL:initialURL]; |
| 96 [[EarlGrey selectElementWithMatcher:WebViewContainingText(linkText)] |
| 97 assertWithMatcher:grey_notNil()]; |
| 96 | 98 |
| 97 [[EarlGrey selectElementWithMatcher:web::WebView()] | 99 [[EarlGrey selectElementWithMatcher:web::WebView()] |
| 98 performAction:web::longPressElementForContextMenu( | 100 performAction:web::longPressElementForContextMenu( |
| 99 linkID, false /* menu shouldn't appear */)]; | 101 linkID, false /* menu shouldn't appear */)]; |
| 100 | 102 |
| 101 id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); | 103 id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); |
| 102 | 104 |
| 103 // Verify no context menu. | 105 // Verify no context menu. |
| 104 [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; | 106 [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; |
| 105 } | 107 } |
| 106 | 108 |
| 107 // Tests context menu on element that has WebkitTouchCallout set to none from an | 109 // Tests context menu on element that has WebkitTouchCallout set to none from an |
| 108 // ancestor. | 110 // ancestor. |
| 109 - (void)testContextMenuWebkitTouchCalloutNoneFromAncestor { | 111 - (void)testContextMenuWebkitTouchCalloutNoneFromAncestor { |
| 110 // Create map of canned responses and set up the test HTML server. | 112 // Create map of canned responses and set up the test HTML server. |
| 111 std::map<GURL, std::string> responses; | 113 std::map<GURL, std::string> responses; |
| 112 GURL initialURL = | 114 GURL initialURL = |
| 113 web::test::HttpServer::MakeUrl("http://contextMenuDisabledByWebkit"); | 115 web::test::HttpServer::MakeUrl("http://contextMenuDisabledByWebkit"); |
| 114 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); | 116 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); |
| 115 // The initial page contains a link to the destination URL that has an | 117 // The initial page contains a link to the destination URL that has an |
| 116 // ancestor that disables the context menu via -webkit-touch-callout. | 118 // ancestor that disables the context menu via -webkit-touch-callout. |
| 117 std::string linkID = "link"; | 119 std::string linkID = "link"; |
| 120 std::string linkText = "ancestor no-callout link"; |
| 118 responses[initialURL] = | 121 responses[initialURL] = |
| 119 "<body style='-webkit-touch-callout: none'>" | 122 "<body style='-webkit-touch-callout: none'>" |
| 120 "<a href='" + | 123 "<a href='" + |
| 121 destinationURL.spec() + "' id='" + linkID + | 124 destinationURL.spec() + "' id='" + linkID + "'>" + linkText + |
| 122 "'>ancestor no-callout link</a>" | 125 "</a>" |
| 123 "</body>"; | 126 "</body>"; |
| 124 | 127 |
| 125 web::test::SetUpSimpleHttpServer(responses); | 128 web::test::SetUpSimpleHttpServer(responses); |
| 126 [ShellEarlGrey loadURL:initialURL]; | 129 [ShellEarlGrey loadURL:initialURL]; |
| 130 [[EarlGrey selectElementWithMatcher:WebViewContainingText(linkText)] |
| 131 assertWithMatcher:grey_notNil()]; |
| 127 | 132 |
| 128 [[EarlGrey selectElementWithMatcher:web::WebView()] | 133 [[EarlGrey selectElementWithMatcher:web::WebView()] |
| 129 performAction:web::longPressElementForContextMenu( | 134 performAction:web::longPressElementForContextMenu( |
| 130 linkID, false /* menu shouldn't appear */)]; | 135 linkID, false /* menu shouldn't appear */)]; |
| 131 | 136 |
| 132 id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); | 137 id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); |
| 133 | 138 |
| 134 // Verify no context menu. | 139 // Verify no context menu. |
| 135 [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; | 140 [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; |
| 136 } | 141 } |
| 137 | 142 |
| 138 // Tests context menu on element that has WebkitTouchCallout set to none from an | 143 // Tests context menu on element that has WebkitTouchCallout set to none from an |
| 139 // ancestor and overridden. | 144 // ancestor and overridden. |
| 140 - (void)testContextMenuWebkitTouchCalloutOverride { | 145 - (void)testContextMenuWebkitTouchCalloutOverride { |
| 141 // Create map of canned responses and set up the test HTML server. | 146 // Create map of canned responses and set up the test HTML server. |
| 142 std::map<GURL, std::string> responses; | 147 std::map<GURL, std::string> responses; |
| 143 GURL initialURL = | 148 GURL initialURL = |
| 144 web::test::HttpServer::MakeUrl("http://contextMenuDisabledByWebkit"); | 149 web::test::HttpServer::MakeUrl("http://contextMenuDisabledByWebkit"); |
| 145 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); | 150 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); |
| 146 // The initial page contains a link to the destination URL that has an | 151 // The initial page contains a link to the destination URL that has an |
| 147 // ancestor that disables the context menu via -webkit-touch-callout. | 152 // ancestor that disables the context menu via -webkit-touch-callout. |
| 148 std::string linkID = "link"; | 153 std::string linkID = "link"; |
| 154 std::string linkText = "override no-callout link"; |
| 149 responses[initialURL] = | 155 responses[initialURL] = |
| 150 "<body style='-webkit-touch-callout: none'>" | 156 "<body style='-webkit-touch-callout: none'>" |
| 151 "<a href='" + | 157 "<a href='" + |
| 152 destinationURL.spec() + "' style='-webkit-touch-callout: default' id='" + | 158 destinationURL.spec() + "' style='-webkit-touch-callout: default' id='" + |
| 153 linkID + | 159 linkID + "'>" + linkText + |
| 154 "'>override no-callout link</a>" | 160 "</a>" |
| 155 "</body>"; | 161 "</body>"; |
| 156 | 162 |
| 157 web::test::SetUpSimpleHttpServer(responses); | 163 web::test::SetUpSimpleHttpServer(responses); |
| 158 [ShellEarlGrey loadURL:initialURL]; | 164 [ShellEarlGrey loadURL:initialURL]; |
| 165 [[EarlGrey selectElementWithMatcher:WebViewContainingText(linkText)] |
| 166 assertWithMatcher:grey_notNil()]; |
| 159 | 167 |
| 160 [[EarlGrey selectElementWithMatcher:web::WebView()] | 168 [[EarlGrey selectElementWithMatcher:web::WebView()] |
| 161 performAction:web::longPressElementForContextMenu( | 169 performAction:web::longPressElementForContextMenu( |
| 162 linkID, true /* menu should appear */)]; | 170 linkID, true /* menu should appear */)]; |
| 163 | 171 |
| 164 id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); | 172 id<GREYMatcher> copyItem = ContextMenuItemWithText(@"Copy Link"); |
| 165 | 173 |
| 166 // Context menu should have a "copy link" item. | 174 // Context menu should have a "copy link" item. |
| 167 [[EarlGrey selectElementWithMatcher:copyItem] | 175 [[EarlGrey selectElementWithMatcher:copyItem] |
| 168 assertWithMatcher:grey_notNil()]; | 176 assertWithMatcher:grey_notNil()]; |
| 169 | 177 |
| 170 // Dismiss the context menu. | 178 // Dismiss the context menu. |
| 171 [[EarlGrey selectElementWithMatcher:ElementToDismissContextMenu(@"Cancel")] | 179 [[EarlGrey selectElementWithMatcher:ElementToDismissContextMenu(@"Cancel")] |
| 172 performAction:grey_tap()]; | 180 performAction:grey_tap()]; |
| 173 | 181 |
| 174 // Context menu should go away after the tap. | 182 // Context menu should go away after the tap. |
| 175 [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; | 183 [[EarlGrey selectElementWithMatcher:copyItem] assertWithMatcher:grey_nil()]; |
| 176 } | 184 } |
| 177 | 185 |
| 178 @end | 186 @end |
| OLD | NEW |