Skip to content

Commit

Permalink
Fix: Hiding download button in all cases if browser cannot download (#61
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Jeremy Press authored Apr 11, 2017
1 parent 14ac6f9 commit 3a5bb41
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/lib/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class Browser {
* @return {boolean} true if browser supports download
*/
static canDownload() {
return !window.externalHost && 'download' in document.createElement('a');
return !Browser.isMobile() || (!window.externalHost && 'download' in document.createElement('a'));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,8 @@ class Preview extends EventEmitter {
throw new Error(__('error_permissions'));
}

// Show download button if download permissions exist and preview options allow
if (checkPermission(this.file, PERMISSION_DOWNLOAD) && this.options.showDownload) {
// Show download button if download permissions exist, options allow, and browser has ability
if (checkPermission(this.file, PERMISSION_DOWNLOAD) && this.options.showDownload && Browser.canDownload()) {
showLoadingDownloadButton(this.download);
}

Expand Down Expand Up @@ -916,7 +916,7 @@ class Preview extends EventEmitter {
finishLoading(data = {}) {
// Show or hide annotate/print/download buttons
// canDownload is not supported by all of our browsers, so for now we need to check isMobile
if (checkPermission(this.file, PERMISSION_DOWNLOAD) && this.options.showDownload && (Browser.canDownload() || !Browser.isMobile())) {
if (checkPermission(this.file, PERMISSION_DOWNLOAD) && this.options.showDownload && Browser.canDownload()) {
showDownloadButton(this.download);

if (checkFeature(this.viewer, 'print') && !Browser.isMobile()) {
Expand Down
19 changes: 14 additions & 5 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ describe('lib/Preview', () => {

stubs.destroy = sandbox.stub(preview, 'destroy');
stubs.checkPermission = sandbox.stub(file, 'checkPermission').returns(true);
stubs.canDownload = sandbox.stub(Browser, 'canDownload').returns(false);
stubs.showLoadingDownloadButton = sandbox.stub(ui, 'showLoadingDownloadButton');
stubs.loadPromiseResolve = Promise.resolve();
stubs.determineRepresentationStatusPromise = Promise.resolve();
Expand Down Expand Up @@ -1167,7 +1168,7 @@ describe('lib/Preview', () => {
}
});

it('should show the loading download button if there are sufficient permissions', () => {
it('should show the loading download button if there are sufficient permissions and support', () => {
stubs.checkPermission.withArgs(sinon.match.any, 'can_download').returns(false);
preview.options.showDownload = false;

Expand All @@ -1190,6 +1191,16 @@ describe('lib/Preview', () => {

stubs.checkPermission.withArgs(sinon.match.any, 'can_download').returns(true);
preview.options.showDownload = true;
stubs.canDownload.returns(false);
preview.destroy();

preview.loadViewer({});
expect(stubs.showLoadingDownloadButton).to.not.be.called;

stubs.checkPermission.withArgs(sinon.match.any, 'can_download').returns(true);
preview.options.showDownload = true;
stubs.canDownload.returns(true);


preview.loadViewer({});
expect(stubs.showLoadingDownloadButton).to.be.called;
Expand Down Expand Up @@ -1300,7 +1311,7 @@ describe('lib/Preview', () => {
stubs.checkFeature.returns(true);
});

it('should show download button if there is download permission', () => {
it('should only show download button if there is download permission', () => {
stubs.checkPermission.returns(false);

preview.finishLoading();
Expand Down Expand Up @@ -1328,13 +1339,11 @@ describe('lib/Preview', () => {

it('should show download button if download is supported by browser', () => {
stubs.canDownload.returns(false);
stubs.isMobile.returns(true);

preview.finishLoading();
expect(stubs.showDownloadButton).to.not.be.called;

stubs.canDownload.returns(false);
stubs.isMobile.returns(false);
stubs.canDownload.returns(true);

preview.finishLoading();
expect(stubs.showDownloadButton).to.be.called;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/viewers/error/PreviewErrorViewer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import autobind from 'autobind-decorator';
import BaseViewer from '../BaseViewer';
import { checkPermission } from '../../file';
import Browser from '../../Browser';
import { PERMISSION_DOWNLOAD } from '../../constants';
import { ICON_FILE_DEFAULT, ICON_FILE_MEDIA, ICON_FILE_ZIP } from '../../icons/icons';
import './PreviewError.scss';
Expand Down Expand Up @@ -70,7 +71,7 @@ class PreviewErrorViewer extends BaseViewer {
this.messageEl.textContent = message;

// Add optional download button
if (checkPermission(file, PERMISSION_DOWNLOAD) && showDownload) {
if (checkPermission(file, PERMISSION_DOWNLOAD) && showDownload && Browser.canDownload()) {
this.addDownloadButton();
}

Expand Down
10 changes: 10 additions & 0 deletions src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-unused-expressions */
import PreviewErrorViewer from '../PreviewErrorViewer';
import Browser from '../../../Browser';
import * as file from '../../../file';
import { PERMISSION_DOWNLOAD } from '../../../constants';
import {
Expand Down Expand Up @@ -97,6 +98,15 @@ describe('lib/viewers/error/PreviewErrorViewer', () => {
expect(error.addDownloadButton).to.not.be.called;
});

it('should not add download button if the browser cannot download', () => {
sandbox.stub(error, 'addDownloadButton');
sandbox.stub(Browser, 'canDownload').returns(false);

error.load('reason');

expect(error.addDownloadButton).to.not.be.called;
});

it('should broadcast load', () => {
sandbox.stub(error, 'emit');

Expand Down

0 comments on commit 3a5bb41

Please sign in to comment.