Commit b2e8acdc authored by Noel Gordon's avatar Noel Gordon Committed by Commit Bot

Closure compile drive_sync_handler externs file and its mock file

Add drive sync handler externs file to the Closure build, and "fix" up
Closure compile issues. Make DriveSyncHandler an interface and use new
type DriveSyncHandlerImpl for the concrete implementation. Add a BUILD
rule for mock_drive_sync_handler and fix Closure compile issues.

Actions model unittest defined MockDriveSyncHandler methods.  Move the
methods to mock_drive_sync_handler and update the actions model test.

Bug: 908045
Change-Id: I6b150f13f47e3b6263b16a1faefa0a1342cb36bb
Reviewed-on: https://chromium-review.googlesource.com/c/1349509Reviewed-by: default avatarStuart Langley <slangley@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Commit-Queue: Noel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610747}
parent bcc24571
......@@ -3,28 +3,36 @@
// found in the LICENSE file.
/**
* Handler of the background page for the drive sync events.
* @constructor
* @extends {cr.EventTarget}
* @struct
* Handler of the background page for the Drive sync events. Implementations
* of this interface must @extends {cr.EventTarget}.
*
* @interface
* @extends {EventTarget}
*/
function DriveSyncHandler() {}
DriveSyncHandler.prototype = /** @struct */ {
__proto__: EventTarget.prototype,
/**
* @return {boolean} Whether the handler is having syncing items or not.
* @return {boolean} Whether the handler is syncing items or not.
*/
get syncing() {}
};
/**
* Returns whether the drive sync is currently suppressed or not.
* Returns the completed event name.
* @return {string}
*/
DriveSyncHandler.prototype.getCompletedEventName = function() {};
/**
* Returns whether the Drive sync is currently suppressed or not.
* @return {boolean}
*/
DriveSyncHandler.prototype.isSyncSuppressed = function() {};
/**
* Shows the notification saying that the drive sync is disabled on cellular
* network.
* Shows a notification that Drive sync is disabled on cellular networks.
*/
DriveSyncHandler.prototype.showDisabledMobileSyncNotification = function() {};
......@@ -66,6 +66,7 @@ js_type_check("closure_compile_module") {
js_type_check("test_support_type_check") {
testonly = true
deps = [
":mock_drive_sync_handler",
":mock_media_scanner",
":mock_progress_center",
":mock_volume_manager",
......@@ -77,6 +78,7 @@ js_library("closure_compile_externs") {
sources = []
externs_list = [
"$externs_path/metrics_private.js",
"../../../externs/background/drive_sync_handler.js",
"../../../externs/background/file_browser_background.js",
"../../../externs/background/file_browser_background_full.js",
"../../../externs/background/import_history.js",
......@@ -163,9 +165,18 @@ js_library("device_handler") {
]
}
js_library("mock_drive_sync_handler") {
testonly = true
deps = [
"//ui/webui/resources/js/cr:event_target",
]
externs_list = [ "../../../externs/background/drive_sync_handler.js" ]
}
js_library("drive_sync_handler") {
deps = [
":progress_center",
"//ui/webui/resources/js/cr:event_target",
]
}
......@@ -312,11 +323,9 @@ js_library("mock_volume_manager") {
js_library("mock_progress_center") {
testonly = true
deps = [
":progress_center",
]
externs_list = [ "//ui/file_manager/externs/background/progress_center.js" ]
}
......@@ -327,7 +336,6 @@ js_library("progress_center") {
"../../common/js:util",
"../../foreground/js/ui:progress_center_panel",
]
externs_list = [ "//ui/file_manager/externs/background/progress_center.js" ]
}
......
......@@ -53,7 +53,7 @@ function FileBrowserBackgroundImpl() {
* Drive sync handler.
* @type {!DriveSyncHandler}
*/
this.driveSyncHandler = new DriveSyncHandler(this.progressCenter);
this.driveSyncHandler = new DriveSyncHandlerImpl(this.progressCenter);
/**
* @type {!importer.DispositionChecker.CheckerFunction}
......
......@@ -3,14 +3,14 @@
// found in the LICENSE file.
/**
* Handler of the background page for the drive sync events.
* @param {ProgressCenter} progressCenter Progress center to submit the
* progressing items.
* Handler of the background page for the Drive sync events.
*
* @constructor
* @extends {cr.EventTarget}
* @struct
* @implements {DriveSyncHandler}
* @extends {cr.EventTarget}
*/
function DriveSyncHandler(progressCenter) {
function DriveSyncHandlerImpl(progressCenter) {
/**
* Progress center to submit the progressing item.
* @type {ProgressCenter}
......@@ -89,63 +89,63 @@ function DriveSyncHandler(progressCenter) {
this.onPreferencesChanged_();
}
/**
* Completed event name.
* @type {string}
* @const
*/
DriveSyncHandler.COMPLETED_EVENT = 'completed';
DriveSyncHandlerImpl.prototype = /** @struct */ {
__proto__: cr.EventTarget.prototype,
/**
* Progress ID of the drive sync error.
* @type {string}
* @const
*/
DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX = 'drive-sync-error-';
/**
* @return {boolean} Whether the handler is syncing items or not.
*/
get syncing() {
return this.syncing_;
}
};
/**
* Notification ID of the disabled mobile sync notification.
* Completed event name.
* @type {string}
* @private
* @const
*/
DriveSyncHandler.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_ = 'disabled-mobile-sync';
DriveSyncHandler.prototype = /** @struct */ {
__proto__: cr.EventTarget.prototype,
DriveSyncHandlerImpl.DRIVE_SYNC_COMPLETED_EVENT = 'completed';
/**
* @return {boolean} Whether the handler is having syncing items or not.
*/
get syncing() {
return this.syncing_;
}
/**
* Returns the completed event name.
* @return {string}
*/
DriveSyncHandlerImpl.prototype.getCompletedEventName = function() {
return DriveSyncHandlerImpl.DRIVE_SYNC_COMPLETED_EVENT;
};
/**
* Returns whether the drive sync is currently suppressed or not.
* Returns whether the Drive sync is currently suppressed or not.
* @return {boolean}
*/
DriveSyncHandler.prototype.isSyncSuppressed = function() {
DriveSyncHandlerImpl.prototype.isSyncSuppressed = function() {
return navigator.connection.type === 'cellular' &&
this.cellularDisabled_;
};
/**
* Shows the notification saying that the drive sync is disabled on cellular
* network.
* Notification ID of the disabled mobile sync notification.
* @type {string}
* @private
* @const
*/
DriveSyncHandler.prototype.showDisabledMobileSyncNotification = function() {
DriveSyncHandlerImpl.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_ =
'disabled-mobile-sync';
/**
* Shows a notification that Drive sync is disabled on cellular networks.
*/
DriveSyncHandlerImpl.prototype.showDisabledMobileSyncNotification = function() {
chrome.notifications.create(
DriveSyncHandler.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_,
{
DriveSyncHandlerImpl.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_, {
type: 'basic',
title: chrome.runtime.getManifest().name,
message: str('DISABLED_MOBILE_SYNC_NOTIFICATION_MESSAGE'),
iconUrl: chrome.runtime.getURL('/common/images/icon96.png'),
buttons: [
{title: str('DISABLED_MOBILE_SYNC_NOTIFICATION_ENABLE_BUTTON')}
]
buttons:
[{title: str('DISABLED_MOBILE_SYNC_NOTIFICATION_ENABLE_BUTTON')}]
},
function() {});
};
......@@ -155,7 +155,7 @@ DriveSyncHandler.prototype.showDisabledMobileSyncNotification = function() {
* @param {chrome.fileManagerPrivate.FileTransferStatus} status Transfer status.
* @private
*/
DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(status) {
DriveSyncHandlerImpl.prototype.onFileTransfersUpdated_ = function(status) {
switch (status.transferState) {
case 'in_progress':
this.updateItem_(status);
......@@ -178,7 +178,7 @@ DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(status) {
* @param {chrome.fileManagerPrivate.FileTransferStatus} status Transfer status.
* @private
*/
DriveSyncHandler.prototype.updateItem_ = function(status) {
DriveSyncHandlerImpl.prototype.updateItem_ = function(status) {
this.queue_.run(function(callback) {
window.webkitResolveLocalFileSystemURL(
status.fileUrl,
......@@ -211,24 +211,32 @@ DriveSyncHandler.prototype.updateItem_ = function(status) {
* @param {chrome.fileManagerPrivate.FileTransferStatus} status Transfer status.
* @private
*/
DriveSyncHandler.prototype.removeItem_ = function(status) {
DriveSyncHandlerImpl.prototype.removeItem_ = function(status) {
this.queue_.run(function(callback) {
this.item_.state = status.transferState === 'completed' ?
ProgressItemState.COMPLETED : ProgressItemState.CANCELED;
this.progressCenter_.updateItem(this.item_);
this.syncing_ = false;
this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT));
this.dispatchEvent(new Event(this.getCompletedEventName()));
callback();
}.bind(this));
};
/**
* Drive sync error prefix.
* @type {string}
* @private
* @const
*/
DriveSyncHandlerImpl.DRIVE_SYNC_ERROR_PREFIX = 'drive-sync-error-';
/**
* Handles drive's sync errors.
* @param {chrome.fileManagerPrivate.DriveSyncErrorEvent} event Drive sync
* error event.
* @private
*/
DriveSyncHandler.prototype.onDriveSyncError_ = function(event) {
DriveSyncHandlerImpl.prototype.onDriveSyncError_ = function(event) {
window.webkitResolveLocalFileSystemURL(event.fileUrl, function(entry) {
var item = new ProgressCenterItem();
item.type = ProgressItemType.SYNC;
......@@ -246,7 +254,7 @@ DriveSyncHandler.prototype.onDriveSyncError_ = function(event) {
item.message = strf('SYNC_NO_SERVER_SPACE', entry.name);
// This error will reappear every time sync is retried, so we use a
// fixed ID to avoid spamming the user.
item.id = DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX +
item.id = DriveSyncHandlerImpl.DRIVE_SYNC_ERROR_PREFIX +
this.driveErrorIdOutOfQuota_;
break;
case 'misc':
......@@ -254,8 +262,8 @@ DriveSyncHandler.prototype.onDriveSyncError_ = function(event) {
break;
}
if (!item.id) {
item.id =
DriveSyncHandler.DRIVE_SYNC_ERROR_PREFIX + (this.errorIdCounter_++);
item.id = DriveSyncHandlerImpl.DRIVE_SYNC_ERROR_PREFIX +
(this.errorIdCounter_++);
}
this.progressCenter_.updateItem(item);
}.bind(this));
......@@ -267,13 +275,12 @@ DriveSyncHandler.prototype.onDriveSyncError_ = function(event) {
* @param {number} buttonIndex Index of the button.
* @private
*/
DriveSyncHandler.prototype.onNotificationButtonClicked_ = function(
DriveSyncHandlerImpl.prototype.onNotificationButtonClicked_ = function(
notificationId, buttonIndex) {
if (notificationId !== DriveSyncHandler.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_)
var expectedId = DriveSyncHandlerImpl.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_;
if (notificationId !== expectedId)
return;
chrome.notifications.clear(
DriveSyncHandler.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_,
function() {});
chrome.notifications.clear(notificationId, function() {});
chrome.fileManagerPrivate.setPreferences({cellularDisabled: false});
};
......@@ -281,7 +288,7 @@ DriveSyncHandler.prototype.onNotificationButtonClicked_ = function(
* Handles preferences change.
* @private
*/
DriveSyncHandler.prototype.onPreferencesChanged_ = function() {
DriveSyncHandlerImpl.prototype.onPreferencesChanged_ = function() {
chrome.fileManagerPrivate.getPreferences(function(pref) {
this.cellularDisabled_ = pref.cellularDisabled;
}.bind(this));
......@@ -291,7 +298,7 @@ DriveSyncHandler.prototype.onPreferencesChanged_ = function() {
* Handles connection state change.
* @private
*/
DriveSyncHandler.prototype.onDriveConnectionStatusChanged_ = function() {
DriveSyncHandlerImpl.prototype.onDriveConnectionStatusChanged_ = function() {
chrome.fileManagerPrivate.getDriveConnectionState((state) => {
// If offline, hide any sync progress notifications. When online again, the
// Drive sync client may retry syncing and trigger onFileTransfersUpdated
......@@ -301,7 +308,7 @@ DriveSyncHandler.prototype.onDriveConnectionStatusChanged_ = function() {
this.syncing_ = false;
this.item_.state = ProgressItemState.CANCELED;
this.progressCenter_.updateItem(this.item_);
this.dispatchEvent(new Event(DriveSyncHandler.COMPLETED_EVENT));
this.dispatchEvent(new Event(this.getCompletedEventName()));
}
});
};
......@@ -3,11 +3,15 @@
// found in the LICENSE file.
'use strict';
// Mock items.
var progressCenter = null;
/**
* @type {!MockProgressCenter}
*/
var progressCenter;
// Test target.
var handler = null;
/**
* @type {!DriveSyncHandlerImpl}
*/
var driveSyncHandler;
/**
* Mock of chrome.fileManagerPrivate.
......@@ -90,9 +94,11 @@ window.strf = window.str;
// Set up the test components.
function setUp() {
// Make ProgressCenterHandler.
// Create a mock ProgressCenter.
progressCenter = new MockProgressCenter();
handler = new DriveSyncHandler(progressCenter);
// Create DriveSyncHandlerImpl.
driveSyncHandler = new DriveSyncHandlerImpl(progressCenter);
}
// Test that in general case item IDs produced for errors are unique.
......@@ -153,12 +159,12 @@ function testOffline() {
assertEquals(1, Object.keys(progressCenter.items).length);
assertEquals(
ProgressItemState.PROGRESSING, progressCenter.items['drive-sync'].state);
assertTrue(handler.syncing);
assertTrue(driveSyncHandler.syncing);
chrome.fileManagerPrivate.onDriveConnectionStatusChanged.listener_();
assertEquals(1, Object.keys(progressCenter.items).length);
assertEquals(
ProgressItemState.CANCELED, progressCenter.items['drive-sync'].state);
assertFalse(handler.syncing);
assertFalse(driveSyncHandler.syncing);
}
......@@ -44,6 +44,10 @@ importer.MediaImportHandler = function(
/** @private {!DriveSyncHandler} */
this.driveSyncHandler_ = driveSyncHandler;
if (this.driveSyncHandler_.getCompletedEventName() !== 'completed') {
throw new Error('invalid drive sync completed event name');
}
};
// The name of the Drive property used to tag imported files. Used to look up
......@@ -119,7 +123,7 @@ importer.MediaImportHandler.prototype.onTaskProgress_ =
case UpdateType.COMPLETE:
// Remove the event handler that gets attached for retries.
this.driveSyncHandler_.removeEventListener(
DriveSyncHandler.COMPLETED_EVENT, task.driveListener_);
this.driveSyncHandler_.getCompletedEventName(), task.driveListener_);
if (task.failedEntries.length > 0 &&
task.failedEntries.length < task.importEntries.length) {
......@@ -175,7 +179,7 @@ importer.MediaImportHandler.prototype.retryTaskFailedEntries_ = function(task) {
this.queue_.queueTask(task);
}.bind(this, task);
this.driveSyncHandler_.addEventListener(
DriveSyncHandler.COMPLETED_EVENT, task.driveListener_);
this.driveSyncHandler_.getCompletedEventName(), task.driveListener_);
};
/**
......@@ -285,7 +289,8 @@ importer.MediaImportHandler.ImportTask = function(
/**
* A placeholder for identifying the appropriate retry function for a given
* task.
* @private {EventListenerType} */
* @private {EventListener}
*/
this.driveListener_ = null;
};
......
// Copyright 2017 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.
'use strict';
/**
* Mock of DriveSyncHandler.
*
* @constructor
* @struct
* @implements {DriveSyncHandler}
* @extends {cr.EventTarget}
*/
function MockDriveSyncHandler() {}
function MockDriveSyncHandler() {
/**
* @type {boolean} Drive sync suppressed state.
* @private
*/
this.syncSuppressed_ = false;
/**
* @type {boolean} Drive sync disabled on mobile notification state.
* @private
*/
this.showingDisabledMobileSyncNotification_ = false;
}
MockDriveSyncHandler.prototype = {
MockDriveSyncHandler.prototype = /** @struct */ {
__proto__: cr.EventTarget.prototype,
/**
* @return {boolean} Whether the handler is syncing items or not.
*/
get syncing() {
return false;
},
};
/**
* Completed event name.
* @type {string}
* @private
* @const
*/
MockDriveSyncHandler.DRIVE_SYNC_COMPLETED_EVENT = 'completed';
/**
* Returns the completed event name.
* @return {string}
*/
MockDriveSyncHandler.prototype.getCompletedEventName = function() {
return MockDriveSyncHandler.DRIVE_SYNC_COMPLETED_EVENT;
};
/**
* Returns whether the Drive sync is currently suppressed or not.
* @return {boolean}
*/
MockDriveSyncHandler.prototype.isSyncSuppressed = function() {
return this.syncSuppressed_;
};
/**
* Shows a notification that Drive sync is disabled on cellular networks.
*/
MockDriveSyncHandler.prototype.showDisabledMobileSyncNotification = function() {
this.showingDisabledMobileSyncNotification_ = true;
};
......@@ -27,6 +27,7 @@
<script src="../../background/js/volume_manager_impl.js"></script>
<script src="../../background/js/volume_manager_util.js"></script>
<script src="../../background/js/mock_volume_manager.js"></script>
<script src="../../background/js/mock_drive_sync_handler.js"></script>
<script src="actions_model.js"></script>
<script src="actions_model_unittest.js"></script>
......@@ -26,14 +26,6 @@ MockFolderShortcutsModel.prototype.remove = function(entry) {
this.has = false;
};
MockDriveSyncHandler = function() {
this.syncSuppressed = false;
};
MockDriveSyncHandler.prototype.isSyncSuppressed = function() {
return this.syncSuppressed;
};
function MockUI() {
this.listContainer = {
currentView: {
......
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