Commit 139ffb82 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

Migrate AlertCoordinatorTestCase to unittests

These test cases can be moved to unittests. Additionally, EG coverage
already exists for AlertCoordinator (which is used by DialogPresenter)
from the JavaScriptDialogTestCase tests.

Bug: 754642, 987646
Change-Id: I5e6d9c37f0603bf72bdf854d048381519b920aae
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1907203Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714556}
parent 5a5e5f9b
......@@ -71,24 +71,3 @@ source_set("alert_coordinator_internal") {
]
libs = [ "UIKit.framework" ]
}
source_set("eg_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"alert_coordinator_egtest.mm",
]
deps = [
":alert_coordinator",
"//base",
"//components/strings",
"//ios/chrome/browser/ui/util",
"//ios/chrome/test/earl_grey:test_support",
"//ios/testing/earl_grey:earl_grey_support",
"//ios/third_party/earl_grey:earl_grey+link",
]
libs = [
"UIKit.framework",
"XCTest.framework",
]
}
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <EarlGrey/EarlGrey.h>
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#include "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h"
#import "ios/chrome/browser/ui/util/top_view_controller.h"
#import "ios/chrome/test/earl_grey/chrome_matchers.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
NSString* kTitle = @"Foo Title";
} // namespace
// Integration test for the alert coordinator using Earl Grey.
@interface AlertCoordinatorTestCase : ChromeTestCase
// Whether an alert is presented.
- (BOOL)isPresentingAlert;
@end
@implementation AlertCoordinatorTestCase
// Tests that if the alert coordinator is destroyed, the alert is dismissed.
- (void)testDismissOnDestroy {
// TODO(crbug.com/754642): Remove TopPresentedViewControllerFrom().
UIViewController* topViewController =
top_view_controller::TopPresentedViewController();
AlertCoordinator* alertCoordinator =
[[AlertCoordinator alloc] initWithBaseViewController:topViewController
title:kTitle
message:nil];
[alertCoordinator start];
GREYAssertTrue([self isPresentingAlert], @"An alert should be presented");
alertCoordinator = nil;
GREYAssertFalse([self isPresentingAlert], @"The alert should be removed");
}
- (void)testNoInteractionActionAfterTap {
// TODO(crbug.com/754642): Remove TopPresentedViewControllerFrom().
UIViewController* topViewController =
top_view_controller::TopPresentedViewController();
AlertCoordinator* alertCoordinator =
[[AlertCoordinator alloc] initWithBaseViewController:topViewController
title:kTitle
message:nil];
__block BOOL blockCalled = NO;
[alertCoordinator setNoInteractionAction:^{
blockCalled = YES;
}];
[alertCoordinator start];
GREYAssertTrue([self isPresentingAlert], @"An alert should be presented");
[[EarlGrey
selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabelId(
IDS_OK)] performAction:grey_tap()];
GREYAssertFalse([self isPresentingAlert], @"The alert should be removed");
GREYAssertFalse(blockCalled,
@"The noInteractionBlock should not have been called.");
}
- (BOOL)isPresentingAlert {
NSError* error = nil;
[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(kTitle)]
assertWithMatcher:grey_sufficientlyVisible()
error:&error];
return (error == nil);
}
@end
......@@ -228,6 +228,8 @@ TEST_F(AlertCoordinatorTest, OnlyOneCancelAction) {
EXPECT_EQ(UIAlertActionStyleCancel, action.style);
}
// Tests that the |noInteractionAction| block is called for an alert coordinator
// which is stopped before the user has interacted with it.
TEST_F(AlertCoordinatorTest, NoInteractionActionTest) {
// Setup.
UIViewController* viewController = getViewController();
......@@ -248,6 +250,8 @@ TEST_F(AlertCoordinatorTest, NoInteractionActionTest) {
EXPECT_TRUE(blockCalled);
}
// Tests that the |noInteractionAction| block is not called for an alert
// coordinator which is dismissed with the cancel button.
TEST_F(AlertCoordinatorTest, NoInteractionActionWithCancelTest) {
// Setup.
UIViewController* viewController = getViewController();
......@@ -268,3 +272,26 @@ TEST_F(AlertCoordinatorTest, NoInteractionActionWithCancelTest) {
// Test.
EXPECT_FALSE(blockCalled);
}
// Tests that the alert coordinator is dismissed if destroyed without being
// stopped.
TEST_F(AlertCoordinatorTest, AlertDismissedOnDestroy) {
// Setup.
UIViewController* viewController = getViewController();
AlertCoordinator* alertCoordinator = getAlertCoordinator(viewController);
ASSERT_FALSE(alertCoordinator.isVisible);
ASSERT_EQ(nil, viewController.presentedViewController);
__block BOOL blockCalled = NO;
alertCoordinator.noInteractionAction = ^{
blockCalled = YES;
};
startAlertCoordinator();
alertCoordinator = nil;
EXPECT_FALSE(blockCalled);
}
......@@ -74,7 +74,6 @@ chrome_ios_eg_test("ios_chrome_settings_egtests") {
chrome_ios_eg_test("ios_chrome_ui_egtests") {
deps = [
"//ios/chrome/browser/ui/activity_services:eg_tests",
"//ios/chrome/browser/ui/alert_coordinator:eg_tests",
"//ios/chrome/browser/ui/browser_view:eg_tests",
"//ios/chrome/browser/ui/dialogs:eg_tests",
"//ios/chrome/browser/ui/download:eg_tests",
......
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