Commit 4c9af76f authored by Eugene But's avatar Eugene But Committed by Commit Bot

Run confirmation dialog before cancelling in progress download.

There is no unit test for tapping OK and Cancel buttons, because
it's something that is very hard to test in unit test (buttons are
not tappable unless the UI got synchronized).

UI mock: https://docs.google.com/presentation/d/1GzbAoJrpW9IAQF78afh5SZLWJWErNcC67t_ctujjEus/edit#slide=id.g2b7a689b42_0_196

Bug: 791806
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ic291ef5db83eaea4220ff1ed3f9501020de7389f
Reviewed-on: https://chromium-review.googlesource.com/919492Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Commit-Queue: Eugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537357}
parent 1aa6c877
......@@ -8,6 +8,7 @@
#import "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/download/download_manager_tab_helper.h"
#import "ios/chrome/browser/ui/download/download_manager_mediator.h"
#import "ios/chrome/browser/ui/download/download_manager_view_controller.h"
......@@ -15,6 +16,7 @@
#import "ios/chrome/browser/ui/presenters/contained_presenter_delegate.h"
#import "ios/web/public/download/download_task.h"
#include "net/url_request/url_fetcher_response_writer.h"
#include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -25,6 +27,8 @@
DownloadManagerViewControllerDelegate> {
// View controller for presenting Download Manager UI.
DownloadManagerViewController* _viewController;
// A dialog which requests a confirmation from the user.
UIAlertController* _confirmationDialog;
// View controller for presenting "Open In.." dialog.
UIDocumentInteractionController* _openInController;
DownloadManagerMediator _mediator;
......@@ -59,6 +63,8 @@
[self.presenter dismissAnimated:YES];
_viewController = nil;
}
[_confirmationDialog dismissViewControllerAnimated:YES completion:nil];
_confirmationDialog = nil;
_mediator.SetDownloadTask(nullptr);
_downloadTask = nullptr;
}
......@@ -107,8 +113,20 @@
- (void)downloadManagerViewControllerDidClose:
(DownloadManagerViewController*)controller {
_downloadTask->Cancel();
[self stop];
if (_downloadTask->GetState() != web::DownloadTask::State::kInProgress) {
[self cancelDownload];
return;
}
__weak DownloadManagerCoordinator* weakSelf = self;
// TODO(crbug.com/805533): Localize this string.
[self runConfirmationDialogWithTitle:@"Cancel Download?"
message:nil
completionHandler:^(BOOL confirmed) {
if (confirmed) {
[weakSelf cancelDownload];
}
}];
}
- (void)downloadManagerViewControllerDidStartDownload:
......@@ -131,4 +149,47 @@
DCHECK(menuShown);
}
#pragma mark - Private
// Cancels the download task and stops the coordinator.
- (void)cancelDownload {
// |stop| nulls-our _downloadTask and |Cancel| destroys the task. Call |stop|
// first to perform all coordinator cleanups, but retain |_downloadTask|
// pointer to destroy the task.
web::DownloadTask* downloadTask = _downloadTask;
[self stop];
downloadTask->Cancel();
}
// Presents UIAlertController with |title|, |message| and two buttons (OK and
// Cancel). |handler| is called with YES if OK button was tapped and with NO
// if Cancel button was tapped.
- (void)runConfirmationDialogWithTitle:(NSString*)title
message:(NSString*)message
completionHandler:(void (^)(BOOL confirmed))handler {
_confirmationDialog =
[UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* OKAction =
[UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_OK)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction*) {
handler(YES);
}];
[_confirmationDialog addAction:OKAction];
UIAlertAction* cancelAction =
[UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_CANCEL)
style:UIAlertActionStyleCancel
handler:^(UIAlertAction*) {
handler(NO);
}];
[_confirmationDialog addAction:cancelAction];
[self.baseViewController presentViewController:_confirmationDialog
animated:YES
completion:nil];
}
@end
......@@ -6,6 +6,7 @@
#import <UIKit/UIKit.h>
#include "base/mac/foundation_util.h"
#include "base/run_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "ios/chrome/browser/download/download_directory_util.h"
......@@ -235,6 +236,38 @@ TEST_F(DownloadManagerCoordinatorTest, Close) {
[coordinator_ stop];
}
// Tests closing view controller while the download is in progress. Coordinator
// should present the confirmation dialog.
TEST_F(DownloadManagerCoordinatorTest, CloseInProgressDownload) {
web::FakeDownloadTask task(GURL(kTestUrl), kTestMimeType);
task.Start(std::make_unique<net::URLFetcherStringWriter>());
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]);
[viewController.delegate
downloadManagerViewControllerDidClose:viewController];
// Verify that UIAlert is presented.
ASSERT_TRUE([base_view_controller_.presentedViewController
isKindOfClass:[UIAlertController class]]);
UIAlertController* alert = base::mac::ObjCCast<UIAlertController>(
base_view_controller_.presentedViewController);
EXPECT_NSEQ(@"Cancel Download?", alert.title);
EXPECT_FALSE(alert.message);
// Stop to avoid holding a dangling pointer to destroyed task.
[coordinator_ stop];
// |stop| should dismiss the apert.
ASSERT_TRUE(WaitUntilConditionOrTimeout(testing::kWaitForUIElementTimeout, ^{
return !base_view_controller_.presentedViewController;
}));
}
// Tests starting the download. Verifies that download task is started and its
// file writer is configured to write into download directory.
TEST_F(DownloadManagerCoordinatorTest, StartDownload) {
......
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