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( ...@@ -73,16 +73,17 @@ void WaitForContextMenuItemDisappeared(
@"Waiting for matcher %@ failed.", context_menu_item_button); @"Waiting for matcher %@ failed.", context_menu_item_button);
} }
// Long press on |element_id| to trigger context menu and then tap on // Long press on |element_id| to trigger context menu.
// |contextMenuItemButton| item. void LongPressElement(const char* element_id) {
void LongPressElementAndTapOnButton(const char* element_id,
id<GREYMatcher> context_menu_item_button) {
id<GREYMatcher> web_view_matcher = id<GREYMatcher> web_view_matcher =
web::WebViewInWebState(chrome_test_util::GetCurrentWebState()); web::WebViewInWebState(chrome_test_util::GetCurrentWebState());
[[EarlGrey selectElementWithMatcher:web_view_matcher] [[EarlGrey selectElementWithMatcher:web_view_matcher]
performAction:chrome_test_util::LongPressElementForContextMenu( performAction:chrome_test_util::LongPressElementForContextMenu(
element_id, true /* menu should appear */)]; 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] [[EarlGrey selectElementWithMatcher:context_menu_item_button]
assertWithMatcher:grey_notNil()]; assertWithMatcher:grey_notNil()];
[[EarlGrey selectElementWithMatcher:context_menu_item_button] [[EarlGrey selectElementWithMatcher:context_menu_item_button]
...@@ -131,7 +132,8 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) { ...@@ -131,7 +132,8 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) {
[ChromeEarlGrey loadURL:pageURL]; [ChromeEarlGrey loadURL:pageURL];
[ChromeEarlGrey waitForMainTabCount:1]; [ChromeEarlGrey waitForMainTabCount:1];
LongPressElementAndTapOnButton(kChromiumImageID, OpenImageButton()); LongPressElement(kChromiumImageID);
TapOnContextMenuButton(OpenImageButton());
// Verify url and tab count. // Verify url and tab count.
[[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText( [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(
...@@ -149,7 +151,8 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) { ...@@ -149,7 +151,8 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) {
[ChromeEarlGrey loadURL:pageURL]; [ChromeEarlGrey loadURL:pageURL];
[ChromeEarlGrey waitForMainTabCount:1]; [ChromeEarlGrey waitForMainTabCount:1];
LongPressElementAndTapOnButton(kChromiumImageID, OpenImageInNewTabButton()); LongPressElement(kChromiumImageID);
TapOnContextMenuButton(OpenImageInNewTabButton());
SelectTabAtIndexInCurrentMode(1U); SelectTabAtIndexInCurrentMode(1U);
...@@ -183,7 +186,8 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) { ...@@ -183,7 +186,8 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) {
[ChromeEarlGrey loadURL:initialURL]; [ChromeEarlGrey loadURL:initialURL];
[ChromeEarlGrey waitForMainTabCount:1]; [ChromeEarlGrey waitForMainTabCount:1];
LongPressElementAndTapOnButton(kDestinationLinkID, OpenLinkInNewTabButton()); LongPressElement(kDestinationLinkID);
TapOnContextMenuButton(OpenLinkInNewTabButton());
SelectTabAtIndexInCurrentMode(1U); SelectTabAtIndexInCurrentMode(1U);
...@@ -194,4 +198,33 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) { ...@@ -194,4 +198,33 @@ void SelectTabAtIndexInCurrentMode(NSUInteger index) {
[ChromeEarlGrey waitForMainTabCount:2]; [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 @end
...@@ -39,6 +39,7 @@ source_set("eg_tests") { ...@@ -39,6 +39,7 @@ source_set("eg_tests") {
"//ios/chrome/app:app_internal", "//ios/chrome/app:app_internal",
"//ios/chrome/app/strings", "//ios/chrome/app/strings",
"//ios/chrome/browser/ui/main", "//ios/chrome/browser/ui/main",
"//ios/chrome/test/app:test_support",
"//ios/chrome/test/base", "//ios/chrome/test/base",
"//ios/chrome/test/earl_grey:test_support", "//ios/chrome/test/earl_grey:test_support",
"//ios/third_party/earl_grey:earl_grey+link", "//ios/third_party/earl_grey:earl_grey+link",
......
...@@ -7,10 +7,9 @@ ...@@ -7,10 +7,9 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/foundation_util.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" #import "ios/chrome/browser/ui/safe_mode/safe_mode_view_controller.h"
#include "ios/chrome/grit/ios_chromium_strings.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/base/scoped_block_swizzler.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h" #import "ios/chrome/test/earl_grey/chrome_test_case.h"
...@@ -20,15 +19,6 @@ ...@@ -20,15 +19,6 @@
namespace { 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. // Verifies that |message| is displayed.
void AssertMessageOnPage(NSString* message) { void AssertMessageOnPage(NSString* message) {
id<GREYMatcher> messageMatcher = [GREYMatchers matcherForText:message]; id<GREYMatcher> messageMatcher = [GREYMatchers matcherForText:message];
...@@ -85,7 +75,8 @@ void AssertTryAgainButtonOnPage() { ...@@ -85,7 +75,8 @@ void AssertTryAgainButtonOnPage() {
// Instantiates a Safe Mode controller and displays it. // Instantiates a Safe Mode controller and displays it.
SafeModeViewController* safeModeController = SafeModeViewController* safeModeController =
[[SafeModeViewController alloc] initWithDelegate:nil]; [[SafeModeViewController alloc] initWithDelegate:nil];
[GetActiveViewController() presentViewController:safeModeController [chrome_test_util::GetActiveViewController()
presentViewController:safeModeController
animated:NO animated:NO
completion:nil]; completion:nil];
// Verifies screen content that shows that crash report is being uploaded. // Verifies screen content that shows that crash report is being uploaded.
...@@ -115,7 +106,8 @@ void AssertTryAgainButtonOnPage() { ...@@ -115,7 +106,8 @@ void AssertTryAgainButtonOnPage() {
// Instantiates a Safe Mode controller and displays it. // Instantiates a Safe Mode controller and displays it.
SafeModeViewController* safeModeController = SafeModeViewController* safeModeController =
[[SafeModeViewController alloc] initWithDelegate:nil]; [[SafeModeViewController alloc] initWithDelegate:nil];
[GetActiveViewController() presentViewController:safeModeController [chrome_test_util::GetActiveViewController()
presentViewController:safeModeController
animated:NO animated:NO
completion:nil]; completion:nil];
// Verifies screen content that does not show crash report being uploaded. // Verifies screen content that does not show crash report being uploaded.
...@@ -151,7 +143,8 @@ void AssertTryAgainButtonOnPage() { ...@@ -151,7 +143,8 @@ void AssertTryAgainButtonOnPage() {
// Instantiates a Safe Mode controller and displays it. // Instantiates a Safe Mode controller and displays it.
SafeModeViewController* safeModeController = SafeModeViewController* safeModeController =
[[SafeModeViewController alloc] initWithDelegate:nil]; [[SafeModeViewController alloc] initWithDelegate:nil];
[GetActiveViewController() presentViewController:safeModeController [chrome_test_util::GetActiveViewController()
presentViewController:safeModeController
animated:NO animated:NO
completion:nil]; completion:nil];
// Verifies screen content that does not show crash report being uploaded. // Verifies screen content that does not show crash report being uploaded.
......
...@@ -18,6 +18,7 @@ class ChromeBrowserState; ...@@ -18,6 +18,7 @@ class ChromeBrowserState;
@class GenericChromeCommand; @class GenericChromeCommand;
@class MainController; @class MainController;
@class NewTabPageController; @class NewTabPageController;
@class UIViewController;
namespace chrome_test_util { namespace chrome_test_util {
...@@ -47,6 +48,11 @@ NSUInteger GetRegisteredKeyCommandsCount(); ...@@ -47,6 +48,11 @@ NSUInteger GetRegisteredKeyCommandsCount();
// TODO(crbug.com/738881): Use DispatcherForActiveViewController() instead. // TODO(crbug.com/738881): Use DispatcherForActiveViewController() instead.
id<BrowserCommands> BrowserCommandDispatcherForMainBVC(); 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. // Returns the dispatcher for the active view controller.
id<ApplicationCommands, BrowserCommands> DispatcherForActiveViewController(); id<ApplicationCommands, BrowserCommands> DispatcherForActiveViewController();
......
...@@ -67,15 +67,6 @@ ios::ChromeBrowserState* GetBrowserState(bool incognito) { ...@@ -67,15 +67,6 @@ ios::ChromeBrowserState* GetBrowserState(bool incognito) {
: browser_state; : 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
namespace chrome_test_util { namespace chrome_test_util {
...@@ -120,6 +111,14 @@ id<BrowserCommands> BrowserCommandDispatcherForMainBVC() { ...@@ -120,6 +111,14 @@ id<BrowserCommands> BrowserCommandDispatcherForMainBVC() {
return mainBVC.dispatcher; 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() { id<ApplicationCommands, BrowserCommands> DispatcherForActiveViewController() {
UIViewController* vc = GetActiveViewController(); UIViewController* vc = GetActiveViewController();
BrowserViewController* bvc = base::mac::ObjCCast<BrowserViewController>(vc); 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