Commit dbaa3abe authored by Kurt Horimoto's avatar Kurt Horimoto Committed by Commit Bot

[iOS] Create OverlayModality::kTesting

This modality can be used in tests rather than choosing
an arbitrary modality for the test, reducing the coupling
between overlay functionality and specific implementations
for the web content area or infobars.

This CL adds support for contained and presented overlay
UI to test overlay UI code.  A follow-up CL will add
tests to OverlayPresentationContextImpl that leverages these
classes.

Change-Id: I648f95a6af1fc78c007fb8d96a7c6ca295928bae
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2068254
Commit-Queue: Kurt Horimoto <kkhorimoto@chromium.org>
Reviewed-by: default avatarMike Dougherty <michaeldo@chromium.org>
Auto-Submit: Kurt Horimoto <kkhorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#746513}
parent eb2fdf73
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
// level of modality. Additional values should be added for each desired level // level of modality. Additional values should be added for each desired level
// of modality (e.g. Browser-level modality, etc.). // of modality (e.g. Browser-level modality, etc.).
enum class OverlayModality { enum class OverlayModality {
// Modality used for testing.
kTesting,
// Used to schedule overlay UI to be displayed modally over a WebState's // Used to schedule overlay UI to be displayed modally over a WebState's
// content area (i.e. present UI requested by script execution on a page). // content area (i.e. present UI requested by script execution on a page).
kWebContentArea, kWebContentArea,
......
...@@ -14,7 +14,6 @@ class OverlayPresentationContextObserver; ...@@ -14,7 +14,6 @@ class OverlayPresentationContextObserver;
// Object that handles presenting the overlay UI for OverlayRequests. // Object that handles presenting the overlay UI for OverlayRequests.
class OverlayPresentationContext { class OverlayPresentationContext {
public: public:
OverlayPresentationContext() = default;
virtual ~OverlayPresentationContext() = default; virtual ~OverlayPresentationContext() = default;
// Adds and removes |observer|. // Adds and removes |observer|.
......
# Copyright 2020 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.
source_set("test_modality") {
testonly = true
sources = [
"test_contained_overlay_request_config.h",
"test_contained_overlay_request_config.mm",
"test_presented_overlay_request_config.h",
"test_presented_overlay_request_config.mm",
]
configs += [ "//build/config/compiler:enable_arc" ]
deps = [
"//base",
"//ios/chrome/browser/overlays",
]
}
// Copyright 2020 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.
#ifndef IOS_CHROME_BROWSER_OVERLAYS_PUBLIC_TEST_MODALITY_TEST_CONTAINED_OVERLAY_REQUEST_CONFIG_H_
#define IOS_CHROME_BROWSER_OVERLAYS_PUBLIC_TEST_MODALITY_TEST_CONTAINED_OVERLAY_REQUEST_CONFIG_H_
#include "ios/chrome/browser/overlays/public/overlay_request_config.h"
// An OverlayRequestConfig to use in tests for contained UIViewControllers.
DEFINE_STATELESS_OVERLAY_REQUEST_CONFIG(TestContainedOverlay);
#endif // IOS_CHROME_BROWSER_OVERLAYS_PUBLIC_TEST_MODALITY_TEST_CONTAINED_OVERLAY_REQUEST_CONFIG_H_
// Copyright 2020 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 "ios/chrome/browser/overlays/public/test_modality/test_contained_overlay_request_config.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
OVERLAY_USER_DATA_SETUP_IMPL(TestContainedOverlay);
// Copyright 2020 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.
#ifndef IOS_CHROME_BROWSER_OVERLAYS_PUBLIC_TEST_MODALITY_TEST_PRESENTED_OVERLAY_REQUEST_CONFIG_H_
#define IOS_CHROME_BROWSER_OVERLAYS_PUBLIC_TEST_MODALITY_TEST_PRESENTED_OVERLAY_REQUEST_CONFIG_H_
#include "ios/chrome/browser/overlays/public/overlay_request_config.h"
// An OverlayRequestConfig to use in tests for presented UIViewControllers.
DEFINE_STATELESS_OVERLAY_REQUEST_CONFIG(TestPresentedOverlay);
#endif // IOS_CHROME_BROWSER_OVERLAYS_PUBLIC_TEST_MODALITY_TEST_PRESENTED_OVERLAY_REQUEST_CONFIG_H_
// Copyright 2020 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 "ios/chrome/browser/overlays/public/test_modality/test_presented_overlay_request_config.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
OVERLAY_USER_DATA_SETUP_IMPL(TestPresentedOverlay);
...@@ -24,6 +24,7 @@ source_set("overlays") { ...@@ -24,6 +24,7 @@ source_set("overlays") {
friend = [ friend = [
":unit_tests", ":unit_tests",
"//ios/chrome/browser/ui/overlays/web_content_area/java_script_dialogs/test", "//ios/chrome/browser/ui/overlays/web_content_area/java_script_dialogs/test",
"//ios/chrome/browser/ui/overlays/test",
] ]
deps = [ deps = [
......
...@@ -32,6 +32,13 @@ ...@@ -32,6 +32,13 @@
DCHECK(browser); DCHECK(browser);
NSArray<Class>* supportedCoordinatorClasses = @[]; NSArray<Class>* supportedCoordinatorClasses = @[];
switch (modality) { switch (modality) {
case OverlayModality::kTesting:
// Use TestOverlayRequestCoordinatorFactory to create factories for
// OverlayModality::kTesting.
// TODO(crbug.com/1056837): Remove requirement once modalities are
// converted to no longer use enums.
NOTREACHED();
break;
case OverlayModality::kWebContentArea: case OverlayModality::kWebContentArea:
supportedCoordinatorClasses = supportedCoordinatorClasses =
web_content_area::GetSupportedOverlayCoordinatorClasses(); web_content_area::GetSupportedOverlayCoordinatorClasses();
......
...@@ -75,9 +75,15 @@ class OverlayPresentationContextImpl : public OverlayPresentationContext { ...@@ -75,9 +75,15 @@ class OverlayPresentationContextImpl : public OverlayPresentationContext {
void HideOverlayUI(OverlayRequest* request) override; void HideOverlayUI(OverlayRequest* request) override;
void CancelOverlayUI(OverlayRequest* request) override; void CancelOverlayUI(OverlayRequest* request) override;
private: protected:
OverlayPresentationContextImpl(Browser* browser, OverlayModality modality); // Constructor called by the Container to instantiate a presentation context
// for |browser| at |modality|, using |factory| to create
// OverlayRequestCoordinators.
OverlayPresentationContextImpl(Browser* browser,
OverlayModality modality,
OverlayRequestCoordinatorFactory* factory);
private:
// Setter for |request_|. Setting to a new value will attempt to // Setter for |request_|. Setting to a new value will attempt to
// present the UI for |request|. // present the UI for |request|.
void SetRequest(OverlayRequest* request); void SetRequest(OverlayRequest* request);
......
...@@ -32,10 +32,19 @@ OverlayPresentationContextImpl::Container::~Container() = default; ...@@ -32,10 +32,19 @@ OverlayPresentationContextImpl::Container::~Container() = default;
OverlayPresentationContextImpl* OverlayPresentationContextImpl*
OverlayPresentationContextImpl::Container::PresentationContextForModality( OverlayPresentationContextImpl::Container::PresentationContextForModality(
OverlayModality modality) { OverlayModality modality) {
// Use TestOverlayPresentationContext to create presentation contexts for
// OverlayModality::kTesting.
// TODO(crbug.com/1056837): Remove requirement once modalities are converted
// to no longer use enums.
DCHECK_NE(modality, OverlayModality::kTesting);
auto& ui_delegate = ui_delegates_[modality]; auto& ui_delegate = ui_delegates_[modality];
if (!ui_delegate) { if (!ui_delegate) {
OverlayRequestCoordinatorFactory* factory =
[OverlayRequestCoordinatorFactory factoryForBrowser:browser_
modality:modality];
ui_delegate = base::WrapUnique( ui_delegate = base::WrapUnique(
new OverlayPresentationContextImpl(browser_, modality)); new OverlayPresentationContextImpl(browser_, modality, factory));
} }
return ui_delegate.get(); return ui_delegate.get();
} }
...@@ -44,14 +53,13 @@ OverlayPresentationContextImpl::Container::PresentationContextForModality( ...@@ -44,14 +53,13 @@ OverlayPresentationContextImpl::Container::PresentationContextForModality(
OverlayPresentationContextImpl::OverlayPresentationContextImpl( OverlayPresentationContextImpl::OverlayPresentationContextImpl(
Browser* browser, Browser* browser,
OverlayModality modality) OverlayModality modality,
OverlayRequestCoordinatorFactory* factory)
: presenter_(OverlayPresenter::FromBrowser(browser, modality)), : presenter_(OverlayPresenter::FromBrowser(browser, modality)),
shutdown_helper_(browser, presenter_), shutdown_helper_(browser, presenter_),
coordinator_delegate_(this), coordinator_delegate_(this),
fullscreen_disabler_(browser, modality), fullscreen_disabler_(browser, modality),
coordinator_factory_([OverlayRequestCoordinatorFactory coordinator_factory_(factory),
factoryForBrowser:browser
modality:modality]),
weak_factory_(this) { weak_factory_(this) {
DCHECK(presenter_); DCHECK(presenter_);
DCHECK(coordinator_factory_); DCHECK(coordinator_factory_);
......
...@@ -11,6 +11,10 @@ source_set("test") { ...@@ -11,6 +11,10 @@ source_set("test") {
"fake_overlay_request_coordinator_delegate.h", "fake_overlay_request_coordinator_delegate.h",
"mock_overlay_coordinator_delegate.h", "mock_overlay_coordinator_delegate.h",
"mock_overlay_coordinator_delegate.mm", "mock_overlay_coordinator_delegate.mm",
"test_overlay_presentation_context.h",
"test_overlay_presentation_context.mm",
"test_overlay_request_coordinator_factory.h",
"test_overlay_request_coordinator_factory.mm",
] ]
configs += [ "//build/config/compiler:enable_arc" ] configs += [ "//build/config/compiler:enable_arc" ]
...@@ -18,7 +22,9 @@ source_set("test") { ...@@ -18,7 +22,9 @@ source_set("test") {
deps = [ deps = [
"//base", "//base",
"//ios/chrome/browser/overlays", "//ios/chrome/browser/overlays",
"//ios/chrome/browser/ui/overlays",
"//ios/chrome/browser/ui/overlays:coordinators", "//ios/chrome/browser/ui/overlays:coordinators",
"//ios/chrome/browser/ui/overlays/test_modality",
"//testing/gmock", "//testing/gmock",
"//testing/gtest", "//testing/gtest",
] ]
......
// Copyright 2020 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.
#ifndef IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_TEST_OVERLAY_PRESENTATION_CONTEXT_H_
#define IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_TEST_OVERLAY_PRESENTATION_CONTEXT_H_
#import "ios/chrome/browser/ui/overlays/overlay_presentation_context_impl.h"
// OverlayPresentationContextImpl for OverlayModality::kTesting.
class TestOverlayPresentationContext : public OverlayPresentationContextImpl {
public:
explicit TestOverlayPresentationContext(Browser* browser);
~TestOverlayPresentationContext() override;
};
#endif // IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_TEST_OVERLAY_PRESENTATION_CONTEXT_H_
// Copyright 2020 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 "ios/chrome/browser/ui/overlays/test/test_overlay_presentation_context.h"
#import "ios/chrome/browser/ui/overlays/test/test_overlay_request_coordinator_factory.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
TestOverlayPresentationContext::TestOverlayPresentationContext(Browser* browser)
: OverlayPresentationContextImpl(
browser,
OverlayModality::kTesting,
[[TestOverlayRequestCoordinatorFactory alloc]
initWithBrowser:browser]) {}
TestOverlayPresentationContext::~TestOverlayPresentationContext() = default;
// Copyright 2020 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.
#ifndef IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_TEST_OVERLAY_REQUEST_COORDINATOR_FACTORY_H_
#define IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_TEST_OVERLAY_REQUEST_COORDINATOR_FACTORY_H_
#import "ios/chrome/browser/ui/overlays/overlay_coordinator_factory.h"
// OverlayRequestCoordinatorFactory for OverlayModality::kTesting.
@interface TestOverlayRequestCoordinatorFactory
: OverlayRequestCoordinatorFactory
// Initializer for a factory that vends OverlayRequestCoordinators for |browser|
// at OverlayModality::kTesting.
- (instancetype)initWithBrowser:(Browser*)browser;
@end
#endif // IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_TEST_OVERLAY_REQUEST_COORDINATOR_FACTORY_H_
// Copyright 2020 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 "ios/chrome/browser/ui/overlays/test/test_overlay_request_coordinator_factory.h"
#import "ios/chrome/browser/ui/overlays/overlay_coordinator_factory+initialization.h"
#import "ios/chrome/browser/ui/overlays/test_modality/test_contained_overlay_coordinator.h"
#import "ios/chrome/browser/ui/overlays/test_modality/test_presented_overlay_coordinator.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation TestOverlayRequestCoordinatorFactory
- (instancetype)initWithBrowser:(Browser*)browser {
return [super initWithBrowser:browser
supportedOverlayRequestCoordinatorClasses:
@ [[TestContainedOverlayCoordinator class],
[TestPresentedOverlayCoordinator class]]];
}
@end
# Copyright 2020 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.
source_set("test_modality") {
testonly = true
sources = [
"test_contained_overlay_coordinator.h",
"test_contained_overlay_coordinator.mm",
"test_presented_overlay_coordinator.h",
"test_presented_overlay_coordinator.mm",
]
configs += [ "//build/config/compiler:enable_arc" ]
deps = [
"//base",
"//ios/chrome/browser/overlays",
"//ios/chrome/browser/overlays/public/test_modality",
"//ios/chrome/browser/ui/overlays:coordinators",
"//ios/chrome/common/ui/util",
]
}
source_set("unit_tests") {
testonly = true
sources = [
"test_contained_overlay_coordinator_unittest.mm",
"test_presented_overlay_coordinator_unittest.mm",
]
configs += [ "//build/config/compiler:enable_arc" ]
deps = [
":test_modality",
"//base/test:test_support",
"//ios/chrome/browser/main:test_support",
"//ios/chrome/browser/overlays",
"//ios/chrome/browser/overlays/public/test_modality",
"//ios/chrome/browser/ui/overlays/test",
"//ios/chrome/test:test_support",
"//ios/web/public/test",
"//testing/gtest",
]
}
// Copyright 2020 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.
#ifndef IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_MODALITY_TEST_CONTAINED_OVERLAY_COORDINATOR_H_
#define IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_MODALITY_TEST_CONTAINED_OVERLAY_COORDINATOR_H_
#import "ios/chrome/browser/ui/overlays/overlay_request_coordinator.h"
// Coordinator to use in tests for overlay UI supported using child
// UIViewControllers.
@interface TestContainedOverlayCoordinator : OverlayRequestCoordinator
@end
#endif // IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_MODALITY_TEST_CONTAINED_OVERLAY_COORDINATOR_H_
// Copyright 2020 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 "ios/chrome/browser/ui/overlays/test_modality/test_contained_overlay_coordinator.h"
#import "ios/chrome/browser/overlays/public/test_modality/test_contained_overlay_request_config.h"
#import "ios/chrome/browser/ui/overlays/overlay_request_coordinator+subclassing.h"
#import "ios/chrome/browser/ui/overlays/overlay_request_coordinator_delegate.h"
#import "ios/chrome/common/ui/util/constraints_ui_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface TestContainedOverlayCoordinator ()
@property(nonatomic, readwrite) UIViewController* containedViewController;
@end
@implementation TestContainedOverlayCoordinator
#pragma mark - OverlayRequestCoordinator
+ (const OverlayRequestSupport*)requestSupport {
return TestContainedOverlay::RequestSupport();
}
+ (BOOL)showsOverlayUsingChildViewController {
return YES;
}
- (UIViewController*)viewController {
return self.containedViewController;
}
- (void)startAnimated:(BOOL)animated {
if (self.started)
return;
self.containedViewController = [[UIViewController alloc] init];
UIView* view = self.viewController.view;
view.translatesAutoresizingMaskIntoConstraints = NO;
[self.baseViewController addChildViewController:self.viewController];
[self.baseViewController.view addSubview:view];
AddSameConstraints(view, view.superview);
[self.viewController didMoveToParentViewController:self.baseViewController];
self.delegate->OverlayUIDidFinishPresentation(self.request);
self.started = YES;
}
- (void)stopAnimated:(BOOL)animated {
if (!self.started)
return;
[self.viewController willMoveToParentViewController:nil];
[self.viewController.view removeFromSuperview];
[self.viewController removeFromParentViewController];
self.containedViewController = nil;
self.delegate->OverlayUIDidFinishDismissal(self.request);
self.started = NO;
}
@end
// Copyright 2020 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 "ios/chrome/browser/ui/overlays/test_modality/test_contained_overlay_coordinator.h"
#import "ios/chrome/browser/main/test_browser.h"
#include "ios/chrome/browser/overlays/public/overlay_request.h"
#import "ios/chrome/browser/overlays/public/test_modality/test_contained_overlay_request_config.h"
#include "ios/chrome/browser/ui/overlays/test/fake_overlay_request_coordinator_delegate.h"
#include "ios/chrome/test/scoped_key_window.h"
#include "ios/web/public/test/web_task_environment.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
// Test fixture for TestContainedOverlayCoordinator.
class TestContainedOverlayCoordinatorTest : public PlatformTest {
public:
TestContainedOverlayCoordinatorTest()
: root_view_controller_([[UIViewController alloc] init]),
request_(OverlayRequest::CreateWithConfig<TestContainedOverlay>()),
coordinator_([[TestContainedOverlayCoordinator alloc]
initWithBaseViewController:root_view_controller_
browser:&browser_
request:request_.get()
delegate:&delegate_]) {
scoped_window_.Get().rootViewController = root_view_controller_;
}
protected:
web::WebTaskEnvironment task_environment_;
TestBrowser browser_;
ScopedKeyWindow scoped_window_;
UIViewController* root_view_controller_ = nil;
std::unique_ptr<OverlayRequest> request_;
FakeOverlayRequestCoordinatorDelegate delegate_;
TestContainedOverlayCoordinator* coordinator_ = nil;
};
// Tests that the coordinator sets up its view correctly.
TEST_F(TestContainedOverlayCoordinatorTest, ViewSetup) {
[coordinator_ startAnimated:NO];
EXPECT_EQ(coordinator_.viewController.view.superview,
root_view_controller_.view);
EXPECT_TRUE(delegate_.HasUIBeenPresented(request_.get()));
[coordinator_ stopAnimated:NO];
EXPECT_TRUE(delegate_.HasUIBeenDismissed(request_.get()));
}
// Copyright 2020 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.
#ifndef IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_MODALITY_TEST_PRESENTED_OVERLAY_COORDINATOR_H_
#define IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_MODALITY_TEST_PRESENTED_OVERLAY_COORDINATOR_H_
#import "ios/chrome/browser/ui/overlays/overlay_request_coordinator.h"
// Coordinator to use in tests for overlay UI supported using presented
// UIViewControllers.
@interface TestPresentedOverlayCoordinator : OverlayRequestCoordinator
@end
#endif // IOS_CHROME_BROWSER_UI_OVERLAYS_TEST_MODALITY_TEST_PRESENTED_OVERLAY_COORDINATOR_H_
// Copyright 2020 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 "ios/chrome/browser/ui/overlays/test_modality/test_presented_overlay_coordinator.h"
#import "ios/chrome/browser/overlays/public/test_modality/test_presented_overlay_request_config.h"
#import "ios/chrome/browser/ui/overlays/overlay_request_coordinator+subclassing.h"
#import "ios/chrome/browser/ui/overlays/overlay_request_coordinator_delegate.h"
#import "ios/chrome/common/ui/util/constraints_ui_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface TestPresentedOverlayCoordinator ()
// Redefine as readwrite.
@property(nonatomic, readwrite) UIViewController* presentedViewController;
@end
@implementation TestPresentedOverlayCoordinator
#pragma mark - OverlayRequestCoordinator
+ (const OverlayRequestSupport*)requestSupport {
return TestPresentedOverlay::RequestSupport();
}
- (UIViewController*)viewController {
return self.presentedViewController;
}
- (void)startAnimated:(BOOL)animated {
if (self.started)
return;
self.presentedViewController = [[UIViewController alloc] init];
self.viewController.modalPresentationStyle =
UIModalPresentationOverCurrentContext;
self.baseViewController.definesPresentationContext = YES;
[self.baseViewController
presentViewController:self.viewController
animated:animated
completion:^{
self.delegate->OverlayUIDidFinishPresentation(self.request);
}];
self.started = YES;
}
- (void)stopAnimated:(BOOL)animated {
if (!self.started)
return;
[self.baseViewController
dismissViewControllerAnimated:animated
completion:^{
self.delegate->OverlayUIDidFinishDismissal(
self.request);
}];
self.presentedViewController = nil;
self.started = NO;
}
@end
// Copyright 2020 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 "ios/chrome/browser/ui/overlays/test_modality/test_presented_overlay_coordinator.h"
#import "base/test/ios/wait_util.h"
#import "ios/chrome/browser/main/test_browser.h"
#include "ios/chrome/browser/overlays/public/overlay_request.h"
#import "ios/chrome/browser/overlays/public/test_modality/test_presented_overlay_request_config.h"
#include "ios/chrome/browser/ui/overlays/test/fake_overlay_request_coordinator_delegate.h"
#include "ios/chrome/test/scoped_key_window.h"
#include "ios/web/public/test/web_task_environment.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
using base::test::ios::WaitUntilConditionOrTimeout;
using base::test::ios::kWaitForUIElementTimeout;
// Test fixture for TestPresentedOverlayCoordinator.
class TestPresentedOverlayCoordinatorTest : public PlatformTest {
public:
TestPresentedOverlayCoordinatorTest()
: root_view_controller_([[UIViewController alloc] init]),
request_(OverlayRequest::CreateWithConfig<TestPresentedOverlay>()),
coordinator_([[TestPresentedOverlayCoordinator alloc]
initWithBaseViewController:root_view_controller_
browser:&browser_
request:request_.get()
delegate:&delegate_]) {
scoped_window_.Get().rootViewController = root_view_controller_;
}
protected:
web::WebTaskEnvironment task_environment_;
TestBrowser browser_;
ScopedKeyWindow scoped_window_;
UIViewController* root_view_controller_ = nil;
std::unique_ptr<OverlayRequest> request_;
FakeOverlayRequestCoordinatorDelegate delegate_;
TestPresentedOverlayCoordinator* coordinator_ = nil;
};
// Tests that the coordinator sets up its view correctly.
TEST_F(TestPresentedOverlayCoordinatorTest, ViewSetup) {
// Start the coordinator and wait until the view is finished being presented.
// This is necessary because UIViewController presentation is asynchronous,
// even when performed without animation.
[coordinator_ startAnimated:NO];
UIViewController* view_controller = coordinator_.viewController;
ASSERT_TRUE(view_controller);
ASSERT_TRUE(WaitUntilConditionOrTimeout(kWaitForUIElementTimeout, ^BOOL {
return view_controller.presentingViewController &&
!view_controller.beingPresented;
}));
EXPECT_TRUE(delegate_.HasUIBeenPresented(request_.get()));
// Verify that the view hierarchy is set up properly. UIViewControllers
// presented with UIModalPresentationOverCurrentContext inserts its
// presentation container view in front of the presenting view controller's
// view.
UIView* superview = root_view_controller_.view.superview;
UIView* presentation_container_view =
view_controller.presentationController.containerView;
EXPECT_EQ(superview, presentation_container_view.superview);
NSArray* siblings = superview.subviews;
EXPECT_EQ([siblings indexOfObject:presentation_container_view],
[siblings indexOfObject:root_view_controller_.view] + 1);
// Stop the coordinator and wait for the dismissal to finish, verifying that
// the delegate was notified.
[coordinator_ stopAnimated:NO];
ASSERT_TRUE(WaitUntilConditionOrTimeout(kWaitForUIElementTimeout, ^BOOL {
return !root_view_controller_.presentedViewController;
}));
EXPECT_TRUE(delegate_.HasUIBeenDismissed(request_.get()));
}
...@@ -268,6 +268,7 @@ test("ios_chrome_unittests") { ...@@ -268,6 +268,7 @@ test("ios_chrome_unittests") {
"//ios/chrome/browser/ui/overlays/infobar_banner/passwords:unit_tests", "//ios/chrome/browser/ui/overlays/infobar_banner/passwords:unit_tests",
"//ios/chrome/browser/ui/overlays/infobar_modal:unit_tests", "//ios/chrome/browser/ui/overlays/infobar_modal:unit_tests",
"//ios/chrome/browser/ui/overlays/infobar_modal/passwords:unit_tests", "//ios/chrome/browser/ui/overlays/infobar_modal/passwords:unit_tests",
"//ios/chrome/browser/ui/overlays/test_modality:unit_tests",
"//ios/chrome/browser/ui/overlays/web_content_area/app_launcher:unit_tests", "//ios/chrome/browser/ui/overlays/web_content_area/app_launcher:unit_tests",
"//ios/chrome/browser/ui/overlays/web_content_area/http_auth_dialogs:unit_tests", "//ios/chrome/browser/ui/overlays/web_content_area/http_auth_dialogs:unit_tests",
"//ios/chrome/browser/ui/overlays/web_content_area/java_script_dialogs:unit_tests", "//ios/chrome/browser/ui/overlays/web_content_area/java_script_dialogs:unit_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