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