Commit b693bb9f authored by Eugene But's avatar Eugene But Committed by Commit Bot

Add DownloadManagerCoordinatorTest.OpenIn test.

This test verifies UIDocumentInteractionController presentation.

Bug: 791806
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I5f97d54edc3d6d18597b6278993957fe866c636e
Reviewed-on: https://chromium-review.googlesource.com/960965Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Eugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543090}
parent 016bd97e
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#import <StoreKit/StoreKit.h> #import <StoreKit/StoreKit.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#include "base/files/file_util.h"
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
...@@ -14,6 +15,7 @@ ...@@ -14,6 +15,7 @@
#import "ios/chrome/browser/download/download_manager_tab_helper.h" #import "ios/chrome/browser/download/download_manager_tab_helper.h"
#import "ios/chrome/browser/ui/download/download_manager_view_controller.h" #import "ios/chrome/browser/ui/download/download_manager_view_controller.h"
#import "ios/chrome/test/fakes/fake_contained_presenter.h" #import "ios/chrome/test/fakes/fake_contained_presenter.h"
#import "ios/chrome/test/fakes/fake_document_interaction_controller.h"
#import "ios/chrome/test/scoped_key_window.h" #import "ios/chrome/test/scoped_key_window.h"
#import "ios/testing/wait_util.h" #import "ios/testing/wait_util.h"
#import "ios/web/public/test/fakes/fake_download_task.h" #import "ios/web/public/test/fakes/fake_download_task.h"
...@@ -22,6 +24,7 @@ ...@@ -22,6 +24,7 @@
#include "net/url_request/url_fetcher_response_writer.h" #include "net/url_request/url_fetcher_response_writer.h"
#include "testing/gtest_mac.h" #include "testing/gtest_mac.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
...@@ -65,17 +68,23 @@ class DownloadManagerCoordinatorTest : public PlatformTest { ...@@ -65,17 +68,23 @@ class DownloadManagerCoordinatorTest : public PlatformTest {
DownloadManagerCoordinatorTest() DownloadManagerCoordinatorTest()
: presenter_([[FakeContainedPresenter alloc] init]), : presenter_([[FakeContainedPresenter alloc] init]),
base_view_controller_([[UIViewController alloc] init]), base_view_controller_([[UIViewController alloc] init]),
document_interaction_controller_class_(
OCMClassMock([UIDocumentInteractionController class])),
tab_helper_(&web_state_), tab_helper_(&web_state_),
coordinator_([[DownloadManagerCoordinator alloc] coordinator_([[DownloadManagerCoordinator alloc]
initWithBaseViewController:base_view_controller_]) { initWithBaseViewController:base_view_controller_]) {
[scoped_key_window_.Get() setRootViewController:base_view_controller_]; [scoped_key_window_.Get() setRootViewController:base_view_controller_];
coordinator_.presenter = presenter_; coordinator_.presenter = presenter_;
} }
~DownloadManagerCoordinatorTest() override {
[document_interaction_controller_class_ stopMocking];
}
FakeContainedPresenter* presenter_; FakeContainedPresenter* presenter_;
UIViewController* base_view_controller_; UIViewController* base_view_controller_;
ScopedKeyWindow scoped_key_window_; ScopedKeyWindow scoped_key_window_;
web::TestWebState web_state_; web::TestWebState web_state_;
id document_interaction_controller_class_;
StubTabHelper tab_helper_; StubTabHelper tab_helper_;
DownloadManagerCoordinator* coordinator_; DownloadManagerCoordinator* coordinator_;
}; };
...@@ -314,6 +323,49 @@ TEST_F(DownloadManagerCoordinatorTest, InstallDrive) { ...@@ -314,6 +323,49 @@ TEST_F(DownloadManagerCoordinatorTest, InstallDrive) {
[coordinator_ stop]; [coordinator_ stop];
} }
// Tests presenting Open In... menu.
TEST_F(DownloadManagerCoordinatorTest, OpenIn) {
web::TestWebThreadBundle thread_bundle;
web::FakeDownloadTask task(GURL(kTestUrl), kTestMimeType);
task.SetSuggestedFilename(base::SysNSStringToUTF16(kTestSuggestedFileName));
coordinator_.downloadTask = &task;
[coordinator_ start];
EXPECT_EQ(1U, base_view_controller_.childViewControllers.count);
DownloadManagerViewController* viewController =
base_view_controller_.childViewControllers.firstObject;
ASSERT_EQ([DownloadManagerViewController class], [viewController class]);
// Start and complete the download.
base::FilePath path;
ASSERT_TRUE(base::GetTempDir(&path));
task.Start(std::make_unique<net::URLFetcherFileWriter>(
base::ThreadTaskRunnerHandle::Get(), path));
task.SetDone(true);
// Stub UIDocumentInteractionController.
FakeDocumentInteractionController* document_interaction_controller =
[[FakeDocumentInteractionController alloc] init];
NSURL* url = [NSURL fileURLWithPath:base::SysUTF8ToNSString(path.value())];
OCMStub(
[document_interaction_controller_class_ interactionControllerWithURL:url])
.andReturn(document_interaction_controller);
// Present Open In... menu.
UILayoutGuide* guide = [[UILayoutGuide alloc] init];
UIView* view = [[UIView alloc] init];
[view addLayoutGuide:guide];
ASSERT_FALSE(document_interaction_controller.presentedOpenInMenu);
[viewController.delegate downloadManagerViewController:viewController
presentOpenInMenuWithLayoutGuide:guide];
ASSERT_TRUE(document_interaction_controller.presentedOpenInMenu);
ASSERT_TRUE(CGRectEqualToRect(
CGRectZero, document_interaction_controller.presentedOpenInMenu.rect));
ASSERT_EQ(view, document_interaction_controller.presentedOpenInMenu.view);
ASSERT_TRUE(document_interaction_controller.presentedOpenInMenu.animated);
}
// Tests closing view controller while the download is in progress. Coordinator // Tests closing view controller while the download is in progress. Coordinator
// should present the confirmation dialog. // should present the confirmation dialog.
TEST_F(DownloadManagerCoordinatorTest, CloseInProgressDownload) { TEST_F(DownloadManagerCoordinatorTest, CloseInProgressDownload) {
......
...@@ -9,6 +9,8 @@ source_set("fakes") { ...@@ -9,6 +9,8 @@ source_set("fakes") {
sources = [ sources = [
"fake_contained_presenter.h", "fake_contained_presenter.h",
"fake_contained_presenter.mm", "fake_contained_presenter.mm",
"fake_document_interaction_controller.h",
"fake_document_interaction_controller.mm",
"fake_download_manager_consumer.h", "fake_download_manager_consumer.h",
"fake_download_manager_consumer.mm", "fake_download_manager_consumer.mm",
"fake_download_manager_tab_helper_delegate.h", "fake_download_manager_tab_helper_delegate.h",
......
// Copyright 2018 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_TEST_FAKES_FAKE_DOCUMENT_INTERACTION_CONTROLLER_H_
#define IOS_CHROME_TEST_FAKES_FAKE_DOCUMENT_INTERACTION_CONTROLLER_H_
#import <UIKit/UIKit.h>
// Holds configuration for the presented Open In... menu.
@interface OpenInMenu : NSObject
// These properties hold arguments passed to |presentOpenInMenu...| methods.
@property(nonatomic, readonly) CGRect rect;
@property(nonatomic, readonly) UIView* view;
@property(nonatomic, readonly) BOOL animated;
@end
// Fake class to stub out UIDocumentInteractionController. This class does not
// present any UI, but simply captures the presentation requests.
@interface FakeDocumentInteractionController : NSObject
// Fake implementations of UIDocumentInteractionController methods:
- (BOOL)presentOpenInMenuFromRect:(CGRect)rect
inView:(UIView*)view
animated:(BOOL)animated;
// Menu that is currently being presented.
@property(nonatomic, readonly) OpenInMenu* presentedOpenInMenu;
@end
#endif // IOS_CHROME_TEST_FAKES_FAKE_DOCUMENT_INTERACTION_CONTROLLER_H_
// Copyright 2018 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/test/fakes/fake_document_interaction_controller.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface OpenInMenu ()
// Properties redefined as readwrite.
@property(nonatomic) CGRect rect;
@property(nonatomic) UIView* view;
@property(nonatomic) BOOL animated;
@end
@implementation OpenInMenu
@synthesize rect = _rect;
@synthesize view = _view;
@synthesize animated = _animated;
@end
@implementation FakeDocumentInteractionController
@synthesize presentedOpenInMenu = _presentedOpenInMenu;
- (BOOL)presentOpenInMenuFromRect:(CGRect)rect
inView:(UIView*)view
animated:(BOOL)animated {
_presentedOpenInMenu = [[OpenInMenu alloc] init];
_presentedOpenInMenu.rect = rect;
_presentedOpenInMenu.view = view;
_presentedOpenInMenu.animated = animated;
return YES;
}
@end
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