Commit cf9deb8e authored by Sasha Morrissey's avatar Sasha Morrissey Committed by Commit Bot

Change Team Drives check to be asynchronous

Without this patch, shouldShowTeamDrives_ calls |callback| synchronously
when the --team-drives flag is disabled, and asynchronously if it is
enabled. The asynchronous call causes the navigation tree to updated
after the appropriate update methods are called, which causes undefined
behaviour.

This patch moves the check for Team Drives into an asynchronous function
called *after* the tree has been synchronously initialized, and simply
shows the element if the user has Team Drives (originally, it hid the
element, but this means the item may be visible for a split-second. To
fix this, it is added as a hidden element by default and then unhidden
after the check).

This also fixes the existing integration tests to work with the
--team-drives flag enabled.

Bug: 849497
Cq-Include-Trybots: luci.chromium.try:closure_compilation
Change-Id: Ie8e174995ca8b16371f32e8ab780f45748fcc55e
Reviewed-on: https://chromium-review.googlesource.com/1103983
Commit-Queue: Sasha Morrissey <sashab@chromium.org>
Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568336}
parent 6a6a1f26
...@@ -810,27 +810,28 @@ DriveVolumeItem.prototype.handleClick = function(e) { ...@@ -810,27 +810,28 @@ DriveVolumeItem.prototype.handleClick = function(e) {
}; };
/** /**
* Checks whether the Team Drives grand root should be shown. * Sets the hidden state of the given item depending on whether the user has any
* We show the Team Drives grand root if the user has at least one Team Drive. * Team Drives.
* *
* @param {function(boolean)} callback Called with True if the grand root and * Since we don't currently support any functionality with just the grand root
* its subtree should be shown, false if not. * (e.g. you can't create a new team drive from the root yet), hide the grand
* root unless the user has at least one Team Drive. If there is at least one
* Team Drive, show it.
*
* @param {!DirectoryItem} teamDrivesGrandRootItem The item to show if there is
* at least one Team Drive, or hide if not.
* @private * @private
*/ */
DriveVolumeItem.prototype.shouldShowTeamDrives_ = function(callback) { DriveVolumeItem.prototype.setHiddenForTeamDrivesGrandRoot_ = function(
teamDrivesGrandRootItem) {
var teamDriveEntry = this.volumeInfo_.teamDriveDisplayRoot; var teamDriveEntry = this.volumeInfo_.teamDriveDisplayRoot;
if (!teamDriveEntry) { if (!teamDriveEntry)
callback(false); return;
} else { var reader = teamDriveEntry.createReader();
// Don't show the Team Drives root unless the user has at least one team reader.readEntries(function(results) {
// drive, since we don't currently support any functionality with just the metrics.recordSmallCount('TeamDrivesCount', results.length);
// grand root (e.g. you can't create a new team drive from the root yet). teamDrivesGrandRootItem.hidden = results.length == 0;
var reader = teamDriveEntry.createReader(); });
reader.readEntries(function(results) {
metrics.recordSmallCount('TeamDrivesCount', results.length);
callback(results.length > 0);
});
}
}; };
/** /**
...@@ -841,35 +842,46 @@ DriveVolumeItem.prototype.shouldShowTeamDrives_ = function(callback) { ...@@ -841,35 +842,46 @@ DriveVolumeItem.prototype.shouldShowTeamDrives_ = function(callback) {
DriveVolumeItem.prototype.updateSubDirectories = function(recursive) { DriveVolumeItem.prototype.updateSubDirectories = function(recursive) {
if (!this.entry || this.hasChildren) if (!this.entry || this.hasChildren)
return; return;
this.shouldShowTeamDrives_(function(shouldShowTeamDrives) {
var entries = [this.entry];
if (shouldShowTeamDrives)
entries.push(this.volumeInfo_.teamDriveDisplayRoot);
// Drive volume has children including fake entries (offline, recent, ...)
var fakeEntries = [];
if (this.parentTree_.fakeEntriesVisible_) {
for (var key in this.volumeInfo_.fakeEntries)
fakeEntries.push(this.volumeInfo_.fakeEntries[key]);
// This list is sorted by URL on purpose.
fakeEntries.sort(function(a, b) {
if (a.toURL() === b.toURL())
return 0;
return b.toURL() > a.toURL() ? 1 : -1;
});
entries = entries.concat(fakeEntries);
}
for (var i = 0; i < entries.length; i++) { var entries = [this.entry];
var item = new SubDirectoryItem(
util.getEntryLabel( var teamDrivesDisplayRoot = this.volumeInfo_.teamDriveDisplayRoot;
this.parentTree_.volumeManager_.getLocationInfo(entries[i]), if (!!teamDrivesDisplayRoot) {
entries[i]) || '', entries.push(teamDrivesDisplayRoot);
entries[i], this, this.parentTree_); }
this.add(item);
item.updateSubDirectories(false); // Drive volume has children including fake entries (offline, recent, ...)
var fakeEntries = [];
if (this.parentTree_.fakeEntriesVisible_) {
for (var key in this.volumeInfo_.fakeEntries)
fakeEntries.push(this.volumeInfo_.fakeEntries[key]);
// This list is sorted by URL on purpose.
fakeEntries.sort(function(a, b) {
if (a.toURL() === b.toURL())
return 0;
return b.toURL() > a.toURL() ? 1 : -1;
});
entries = entries.concat(fakeEntries);
}
for (var i = 0; i < entries.length; i++) {
var item = new SubDirectoryItem(
util.getEntryLabel(
this.parentTree_.volumeManager_.getLocationInfo(entries[i]),
entries[i]) ||
'',
entries[i], this, this.parentTree_);
// Hide the team drives root in case we have no team drives.
if (entries[i] === teamDrivesDisplayRoot) {
item.hidden = true;
this.setHiddenForTeamDrivesGrandRoot_(item);
} }
this.expanded = true;
}.bind(this)); this.add(item);
item.updateSubDirectories(false);
}
this.expanded = true;
}; };
/** /**
......
...@@ -238,15 +238,21 @@ function testCreateDirectoryTreeWithEmptyTeamDrive(callback) { ...@@ -238,15 +238,21 @@ function testCreateDirectoryTreeWithEmptyTeamDrive(callback) {
reportPromise( reportPromise(
waitUntil(function() { waitUntil(function() {
// Root entries under Drive volume is generated except Team Drives. // Root entries under Drive volume is generated, plus Team Drives.
// See testCreateDirectoryTreeWithTeamDrive for detail. // See testCreateDirectoryTreeWithTeamDrive for detail.
return driveItem.items.length == 3; return driveItem.items.length == 4;
}).then(function() { }).then(function() {
var teamDrivesItemFound = false;
for (var i = 0; i < driveItem.items.length; i++) { for (var i = 0; i < driveItem.items.length; i++) {
assertFalse( if (driveItem.items[i].label == str('DRIVE_TEAM_DRIVES_LABEL')) {
driveItem.items[i].label == str('DRIVE_TEAM_DRIVES_LABEL'), assertEquals(
'Team Drives node should not be shown'); true, driveItem.items[i].hidden,
'Team Drives node should not be shown');
teamDrivesItemFound = true;
break;
}
} }
assertTrue(teamDrivesItemFound, 'Team Drives node should be generated');
}), }),
callback); callback);
} }
......
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