Commit fafb1062 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

Add test to ensure context menu triggers on image urls.

Expose GetActiveViewController from chrome_test_util.

Bug: 777313
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ib819e24af2abb9f49ce6a4e44114142ba8d0ce42
Reviewed-on: https://chromium-review.googlesource.com/795095
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#520861}
parent 582ff658
......@@ -73,16 +73,17 @@ void WaitForContextMenuItemDisappeared(
@"Waiting for matcher %@ failed.", context_menu_item_button);
}
// Long press on |element_id| to trigger context menu and then tap on
// |contextMenuItemButton| item.
void LongPressElementAndTapOnButton(const char* element_id,
id<GREYMatcher> context_menu_item_button) {
// Long press on |element_id| to trigger context menu.
void LongPressElement(const char* element_id) {
id<GREYMatcher> web_view_matcher =
web::WebViewInWebState(chrome_test_util::GetCurrentWebState());
[[EarlGrey selectElementWithMatcher:web_view_matcher]
performAction:chrome_test_util::LongPressElementForContextMenu(
element_id, true /* menu should appear */)];
}
// Tap on |context_menu_item_button| context menu item.
void TapOnContextMenuButton(id<GREYMatcher> context_menu_item_button) {
[[EarlGrey selectElementWithMatcher:context_menu_item_button]
assertWithMatcher:grey_notNil()];
[[EarlGrey selectElementWithMatcher:context_menu_item_button]
......@@ -131,7 +132,8 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) {
[ChromeEarlGrey loadURL:pageURL];
[ChromeEarlGrey waitForMainTabCount:1];
LongPressElementAndTapOnButton(kChromiumImageID, OpenImageButton());
LongPressElement(kChromiumImageID);
TapOnContextMenuButton(OpenImageButton());
// Verify url and tab count.
[[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
......@@ -149,7 +151,8 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) {
[ChromeEarlGrey loadURL:pageURL];
[ChromeEarlGrey waitForMainTabCount:1];
LongPressElementAndTapOnButton(kChromiumImageID, OpenImageInNewTabButton());
LongPressElement(kChromiumImageID);
TapOnContextMenuButton(OpenImageInNewTabButton());
SelectTabAtIndexInCurrentMode(1U);
......@@ -183,7 +186,8 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) {
[ChromeEarlGrey loadURL:initialURL];
[ChromeEarlGrey waitForMainTabCount:1];
LongPressElementAndTapOnButton(kDestinationLinkID, OpenLinkInNewTabButton());
LongPressElement(kDestinationLinkID);
TapOnContextMenuButton(OpenLinkInNewTabButton());
SelectTabAtIndexInCurrentMode(1U);
......@@ -194,4 +198,33 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) {
[ChromeEarlGrey waitForMainTabCount:2];
}
// Tests that the context menu is displayed for an image url.
- (void)testContextMenuDisplayedOnImage {
GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg);
web::test::SetUpFileBasedHttpServer();
[ChromeEarlGrey loadURL:imageURL];
[ChromeEarlGrey waitForMainTabCount:1];
// Calculate a point inside the displayed image. Javascript can not be used to
// find the element because no DOM exists.
CGPoint point = CGPointMake(
CGRectGetMidX([chrome_test_util::GetActiveViewController() view].bounds),
20.0);
id<GREYMatcher> web_view_matcher =
web::WebViewInWebState(chrome_test_util::GetCurrentWebState());
[[EarlGrey selectElementWithMatcher:web_view_matcher]
performAction:grey_longPressAtPointWithDuration(
point, kGREYLongPressDefaultDuration)];
TapOnContextMenuButton(OpenImageInNewTabButton());
[ChromeEarlGrey waitForMainTabCount:2];
SelectTabAtIndexInCurrentMode(1U);
// Verify url.
[[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
imageURL.GetContent())]
assertWithMatcher:grey_notNil()];
}
@end
......@@ -39,6 +39,7 @@ source_set("eg_tests") {
"//ios/chrome/app:app_internal",
"//ios/chrome/app/strings",
"//ios/chrome/browser/ui/main",
"//ios/chrome/test/app:test_support",
"//ios/chrome/test/base",
"//ios/chrome/test/earl_grey:test_support",
"//ios/third_party/earl_grey:earl_grey+link",
......
......@@ -7,10 +7,9 @@
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#import "ios/chrome/app/chrome_overlay_window.h"
#import "ios/chrome/browser/ui/main/view_controller_swapping.h"
#import "ios/chrome/browser/ui/safe_mode/safe_mode_view_controller.h"
#include "ios/chrome/grit/ios_chromium_strings.h"
#import "ios/chrome/test/app/chrome_test_util.h"
#import "ios/chrome/test/base/scoped_block_swizzler.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h"
......@@ -20,15 +19,6 @@
namespace {
// Returns the top view controller for rendering the Safe Mode Controller.
UIViewController* GetActiveViewController() {
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
DCHECK([mainWindow isKindOfClass:[ChromeOverlayWindow class]]);
id<ViewControllerSwapping> main_view_controller =
static_cast<id<ViewControllerSwapping>>([mainWindow rootViewController]);
return main_view_controller.activeViewController;
}
// Verifies that |message| is displayed.
void AssertMessageOnPage(NSString* message) {
id<GREYMatcher> messageMatcher = [GREYMatchers matcherForText:message];
......@@ -85,7 +75,8 @@ void AssertTryAgainButtonOnPage() {
// Instantiates a Safe Mode controller and displays it.
SafeModeViewController* safeModeController =
[[SafeModeViewController alloc] initWithDelegate:nil];
[GetActiveViewController() presentViewController:safeModeController
[chrome_test_util::GetActiveViewController()
presentViewController:safeModeController
animated:NO
completion:nil];
// Verifies screen content that shows that crash report is being uploaded.
......@@ -115,7 +106,8 @@ void AssertTryAgainButtonOnPage() {
// Instantiates a Safe Mode controller and displays it.
SafeModeViewController* safeModeController =
[[SafeModeViewController alloc] initWithDelegate:nil];
[GetActiveViewController() presentViewController:safeModeController
[chrome_test_util::GetActiveViewController()
presentViewController:safeModeController
animated:NO
completion:nil];
// Verifies screen content that does not show crash report being uploaded.
......@@ -151,7 +143,8 @@ void AssertTryAgainButtonOnPage() {
// Instantiates a Safe Mode controller and displays it.
SafeModeViewController* safeModeController =
[[SafeModeViewController alloc] initWithDelegate:nil];
[GetActiveViewController() presentViewController:safeModeController
[chrome_test_util::GetActiveViewController()
presentViewController:safeModeController
animated:NO
completion:nil];
// Verifies screen content that does not show crash report being uploaded.
......
......@@ -18,6 +18,7 @@ class ChromeBrowserState;
@class GenericChromeCommand;
@class MainController;
@class NewTabPageController;
@class UIViewController;
namespace chrome_test_util {
......@@ -47,6 +48,11 @@ NSUInteger GetRegisteredKeyCommandsCount();
// TODO(crbug.com/738881): Use DispatcherForActiveViewController() instead.
id<BrowserCommands> BrowserCommandDispatcherForMainBVC();
// Returns the active view controller.
// NOTE: It is preferred to not directly access the active view controller if
// possible.
UIViewController* GetActiveViewController();
// Returns the dispatcher for the active view controller.
id<ApplicationCommands, BrowserCommands> DispatcherForActiveViewController();
......
......@@ -67,15 +67,6 @@ ios::ChromeBrowserState* GetBrowserState(bool incognito) {
: browser_state;
}
// Gets the root UIViewController.
UIViewController* GetActiveViewController() {
UIWindow* main_window = [[UIApplication sharedApplication] keyWindow];
DCHECK([main_window isKindOfClass:[ChromeOverlayWindow class]]);
id<ViewControllerSwapping> main_view_controller =
static_cast<id<ViewControllerSwapping>>([main_window rootViewController]);
return main_view_controller.activeViewController;
}
} // namespace
namespace chrome_test_util {
......@@ -120,6 +111,14 @@ id<BrowserCommands> BrowserCommandDispatcherForMainBVC() {
return mainBVC.dispatcher;
}
UIViewController* GetActiveViewController() {
UIWindow* main_window = [[UIApplication sharedApplication] keyWindow];
DCHECK([main_window isKindOfClass:[ChromeOverlayWindow class]]);
id<ViewControllerSwapping> main_view_controller =
static_cast<id<ViewControllerSwapping>>([main_window rootViewController]);
return main_view_controller.activeViewController;
}
id<ApplicationCommands, BrowserCommands> DispatcherForActiveViewController() {
UIViewController* vc = GetActiveViewController();
BrowserViewController* bvc = base::mac::ObjCCast<BrowserViewController>(vc);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment