Commit 9025984c authored by Trent Apted's avatar Trent Apted Committed by Commit Bot

Gallery: closure-compile image_view_unittest.js

Bug: 867700, 860355
Change-Id: Ifcc4ada00bc459b8cd59a5c2b234af0ef3554c61
Reviewed-on: https://chromium-review.googlesource.com/1150015Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Commit-Queue: Trent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579292}
parent c808b278
...@@ -91,20 +91,33 @@ MockFileSystem.prototype.findChildren_ = function(directory) { ...@@ -91,20 +91,33 @@ MockFileSystem.prototype.findChildren_ = function(directory) {
return children; return children;
}; };
/** @interface */
function MockEntryInterface() {}
/**
* Clones the entry with the new fullpath.
*
* @param {string} fullpath New fullpath.
* @param {FileSystem=} opt_filesystem New file system
* @return {Entry} Cloned entry.
*/
MockEntryInterface.prototype.clone = function(fullpath, opt_filesystem) {};
/** /**
* Base class of mock entries. * Base class of mock entries.
* *
* @param {FileSystem} filesystem File system where the entry is localed. * @param {FileSystem} filesystem File system where the entry is localed.
* @param {string} fullPath Full path of the entry. * @param {string} fullPath Full path of the entry.
* @param {!Metadata} metadata Metadata. * @param {Metadata=} opt_metadata Metadata.
* @constructor * @constructor
* @extends {Entry} * @extends {Entry}
* @implements {MockEntryInterface}
*/ */
function MockEntry(filesystem, fullPath, metadata) { function MockEntry(filesystem, fullPath, opt_metadata) {
filesystem.entries[fullPath] = this; filesystem.entries[fullPath] = this;
this.filesystem = filesystem; this.filesystem = filesystem;
this.fullPath = fullPath; this.fullPath = fullPath;
this.metadata = metadata; this.metadata = opt_metadata || /** @type {!Metadata} */ ({});
this.removed_ = false; this.removed_ = false;
} }
...@@ -221,13 +234,7 @@ MockEntry.prototype.assertRemoved = function() { ...@@ -221,13 +234,7 @@ MockEntry.prototype.assertRemoved = function() {
throw new Error('expected removed for file ' + this.name); throw new Error('expected removed for file ' + this.name);
}; };
/** /** @override */
* Clones the entry with the new fullpath.
*
* @param {string} fullpath New fullpath.
* @param {FileSystem=} opt_filesystem New file system
* @return {Entry} Cloned entry.
*/
MockEntry.prototype.clone = function(fullpath, opt_filesystem) { MockEntry.prototype.clone = function(fullpath, opt_filesystem) {
throw new Error('Not implemented.'); throw new Error('Not implemented.');
}; };
...@@ -237,14 +244,19 @@ MockEntry.prototype.clone = function(fullpath, opt_filesystem) { ...@@ -237,14 +244,19 @@ MockEntry.prototype.clone = function(fullpath, opt_filesystem) {
* *
* @param {FileSystem} filesystem File system where the entry is localed. * @param {FileSystem} filesystem File system where the entry is localed.
* @param {string} fullPath Full path for the entry. * @param {string} fullPath Full path for the entry.
* @param {!Metadata} metadata Metadata. * @param {Metadata=} opt_metadata Metadata.
* @param {Blob=} opt_content Optional content. * @param {Blob=} opt_content Optional content.
* @extends {MockEntry} * @extends {FileEntry}
* @implements {MockEntryInterface}
* @constructor * @constructor
*/ */
function MockFileEntry(filesystem, fullPath, metadata, opt_content) { function MockFileEntry(filesystem, fullPath, opt_metadata, opt_content) {
MockEntry.call(this, filesystem, fullPath, metadata); filesystem.entries[fullPath] = this;
this.filesystem = filesystem;
this.fullPath = fullPath;
this.metadata = opt_metadata || /** @type {!Metadata} */ ({});
this.content = opt_content || new Blob([]); this.content = opt_content || new Blob([]);
this.removed_ = false;
this.isFile = true; this.isFile = true;
this.isDirectory = false; this.isDirectory = false;
} }
...@@ -257,15 +269,13 @@ MockFileEntry.prototype = { ...@@ -257,15 +269,13 @@ MockFileEntry.prototype = {
* Returns a File that this represents. * Returns a File that this represents.
* *
* @param {function(!File)} onSuccess Function to take the file. * @param {function(!File)} onSuccess Function to take the file.
* @param {function(!Error)} onError * @param {function(!FileError)=} onError
*/ */
MockFileEntry.prototype.file = function(onSuccess, onError) { MockFileEntry.prototype.file = function(onSuccess, onError) {
onSuccess(new File([this.content], this.toURL())); onSuccess(new File([this.content], this.toURL()));
}; };
/** /** @override */
* @override
*/
MockFileEntry.prototype.clone = function(path, opt_filesystem) { MockFileEntry.prototype.clone = function(path, opt_filesystem) {
return new MockFileEntry( return new MockFileEntry(
opt_filesystem || this.filesystem, path, this.metadata, this.content); opt_filesystem || this.filesystem, path, this.metadata, this.content);
...@@ -293,9 +303,7 @@ MockDirectoryEntry.prototype = { ...@@ -293,9 +303,7 @@ MockDirectoryEntry.prototype = {
__proto__: MockEntry.prototype __proto__: MockEntry.prototype
}; };
/** /** @override */
* @override
*/
MockDirectoryEntry.prototype.clone = function(path, opt_filesystem) { MockDirectoryEntry.prototype.clone = function(path, opt_filesystem) {
return new MockDirectoryEntry(opt_filesystem || this.filesystem, path); return new MockDirectoryEntry(opt_filesystem || this.filesystem, path);
}; };
......
...@@ -22,6 +22,7 @@ js_type_check("closure_compile") { ...@@ -22,6 +22,7 @@ js_type_check("closure_compile") {
":image_transform", ":image_transform",
":image_util", ":image_util",
":image_view", ":image_view",
":image_view_unittest",
":test_util", ":test_util",
":viewport", ":viewport",
] ]
...@@ -180,6 +181,14 @@ js_library("image_view") { ...@@ -180,6 +181,14 @@ js_library("image_view") {
] ]
} }
js_library("image_view_unittest") {
deps = [
":image_view",
"../../../file_manager/common/js:mock_entry",
"//ui/webui/resources/js:webui_resource_test",
]
}
js_library("test_util") { js_library("test_util") {
} }
......
...@@ -2,53 +2,77 @@ ...@@ -2,53 +2,77 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
/**
* Helper to construct testing GalleryItem objects.
*
* @param {!FileEntry} entry
* @param {EntryLocation} locationInfo
* @param {Object} metadataItem
* @param {ThumbnailMetadataItem=} opt_thumbnailMetadataItem
* @param {boolean=} opt_original Whether the entry is original or edited.
* @constructor
* @extends GalleryItem
*/
function MockGalleryItem(
entry, locationInfo, metadataItem, opt_thumbnailMetadataItem,
opt_original) {
let entryLocation = locationInfo || /** @type {!EntryLocation} */ ({});
GalleryItem.call(
this, entry, entryLocation, /** @type {MetadataItem} */ (metadataItem),
opt_thumbnailMetadataItem || null, opt_original || false);
}
MockGalleryItem.prototype = {
__proto__: GalleryItem.prototype
};
function testImageView() { function testImageView() {
var mockFileSystem = new MockFileSystem('volumeId'); var mockFileSystem = new MockFileSystem('volumeId');
var mockEntry = new MockEntry(mockFileSystem, '/test.jpg'); var mockEntry = new MockFileEntry(mockFileSystem, '/test.jpg');
// Item has full size cache. // Item has full size cache.
var itemWithFullCache = new GalleryItem(mockEntry, null, {}, null, false); var itemWithFullCache = new MockGalleryItem(mockEntry, null, {});
itemWithFullCache.contentImage = document.createElement('canvas'); itemWithFullCache.contentImage =
assertInstanceof(document.createElement('canvas'), HTMLCanvasElement);
assertEquals( assertEquals(
ImageView.LoadTarget.CACHED_MAIN_IMAGE, ImageView.LoadTarget.CACHED_MAIN_IMAGE,
ImageView.getLoadTarget(itemWithFullCache, new ImageView.Effect.None())); ImageView.getLoadTarget(itemWithFullCache, new ImageView.Effect.None()));
// Item with content thumbnail. // Item with content thumbnail.
var itemWithContentThumbnail = new GalleryItem( var itemWithContentThumbnail =
mockEntry, null, {thumbnail: {url: 'url'}}, null, false); new MockGalleryItem(mockEntry, null, {thumbnail: {url: 'url'}});
assertEquals( assertEquals(
ImageView.LoadTarget.THUMBNAIL, ImageView.LoadTarget.THUMBNAIL,
ImageView.getLoadTarget( ImageView.getLoadTarget(
itemWithContentThumbnail, new ImageView.Effect.None())); itemWithContentThumbnail, new ImageView.Effect.None()));
// Item with external thumbnail. // Item with external thumbnail.
var itemWithExternalThumbnail = new GalleryItem( var itemWithExternalThumbnail =
mockEntry, null, {external: {thumbnailUrl: 'url'}}, null, false); new MockGalleryItem(mockEntry, null, {external: {thumbnailUrl: 'url'}});
assertEquals( assertEquals(
ImageView.LoadTarget.THUMBNAIL, ImageView.LoadTarget.THUMBNAIL,
ImageView.getLoadTarget( ImageView.getLoadTarget(
itemWithExternalThumbnail, new ImageView.Effect.None())); itemWithExternalThumbnail, new ImageView.Effect.None()));
// Item with external thumbnail but present localy. // Item with external thumbnail but present locally.
var itemWithExternalThumbnailPresent = new GalleryItem( var itemWithExternalThumbnailPresent = new MockGalleryItem(
mockEntry, null, {external: {thumbnailUrl: 'url', present: true}}, null, mockEntry, null, {external: {thumbnailUrl: 'url', present: true}});
false);
assertEquals( assertEquals(
ImageView.LoadTarget.MAIN_IMAGE, ImageView.LoadTarget.MAIN_IMAGE,
ImageView.getLoadTarget( ImageView.getLoadTarget(
itemWithExternalThumbnailPresent, new ImageView.Effect.None())); itemWithExternalThumbnailPresent, new ImageView.Effect.None()));
// Item with external thumbnail shown by slide effect. // Item with external thumbnail shown by slide effect.
var itemWithExternalThumbnailSlide = new GalleryItem( var itemWithExternalThumbnailSlide =
mockEntry, null, {external: {thumbnailUrl: 'url'}}, null, false); new MockGalleryItem(mockEntry, null, {external: {thumbnailUrl: 'url'}});
assertEquals( assertEquals(
ImageView.LoadTarget.THUMBNAIL, ImageView.LoadTarget.THUMBNAIL,
ImageView.getLoadTarget( ImageView.getLoadTarget(
itemWithExternalThumbnailSlide, new ImageView.Effect.Slide(1))); itemWithExternalThumbnailSlide, new ImageView.Effect.Slide(1)));
// Item with external thumbnail shown by zoom to screen effect. // Item with external thumbnail shown by zoom to screen effect.
var itemWithExternalThumbnailZoomToScreen = new GalleryItem( var itemWithExternalThumbnailZoomToScreen =
mockEntry, null, {external: {thumbnailUrl: 'url'}}, null, false); new MockGalleryItem(mockEntry, null, {external: {thumbnailUrl: 'url'}});
assertEquals( assertEquals(
ImageView.LoadTarget.THUMBNAIL, ImageView.LoadTarget.THUMBNAIL,
ImageView.getLoadTarget( ImageView.getLoadTarget(
...@@ -56,17 +80,16 @@ function testImageView() { ...@@ -56,17 +80,16 @@ function testImageView() {
new ImageView.Effect.ZoomToScreen(new ImageRect(0, 0, 100, 100)))); new ImageView.Effect.ZoomToScreen(new ImageRect(0, 0, 100, 100))));
// Item with external thumbnail shown by zoom effect. // Item with external thumbnail shown by zoom effect.
var itemWithExternalThumbnailZoom = new GalleryItem( var itemWithExternalThumbnailZoom =
mockEntry, null, {external: {thumbnailUrl: 'url'}}, null, false); new MockGalleryItem(mockEntry, null, {external: {thumbnailUrl: 'url'}});
assertEquals( assertEquals(
ImageView.LoadTarget.MAIN_IMAGE, ImageView.LoadTarget.MAIN_IMAGE,
ImageView.getLoadTarget( ImageView.getLoadTarget(
itemWithExternalThumbnailZoom, itemWithExternalThumbnailZoom,
new ImageView.Effect.Zoom(0, 0, null))); new ImageView.Effect.Zoom(0, 0, new ImageRect(0, 0, 1, 1))));
// Item without cache/thumbnail. // Item without cache/thumbnail.
var itemWithoutCacheOrThumbnail = new GalleryItem( var itemWithoutCacheOrThumbnail = new MockGalleryItem(mockEntry, null, {});
mockEntry, null, {}, null, false);
assertEquals( assertEquals(
ImageView.LoadTarget.MAIN_IMAGE, ImageView.LoadTarget.MAIN_IMAGE,
ImageView.getLoadTarget( ImageView.getLoadTarget(
...@@ -74,27 +97,31 @@ function testImageView() { ...@@ -74,27 +97,31 @@ function testImageView() {
} }
function testLoadVideo(callback) { function testLoadVideo(callback) {
var container = document.createElement('div'); var container = assertInstanceof(document.createElement('div'), HTMLElement);
// We re-use the image-container for video, it starts with this class. // We re-use the image-container for video, it starts with this class.
container.classList.add('image-container'); container.classList.add('image-container');
var viewport = new Viewport(window); var viewport = new Viewport(window);
// Mock volume manager. /**
var volumeManager = { * Mock volume manager.
getVolumeInfo: function(entry) { * @constructor
return {volumeType: VolumeManagerCommon.VolumeType.DOWNLOADS}; * @extends MetadataProvider
} */
}; function MockDownloadVolumeManager() {
this.__proto__.getVolumeInfo =
function(entry) {
return {volumeType: VolumeManagerCommon.VolumeType.DOWNLOADS};
};
}
var metadataModel = new MetadataModel(volumeManager); var metadataModel = new MetadataModel(new MockDownloadVolumeManager());
var imageView = new ImageView(container, viewport, metadataModel); var imageView = new ImageView(container, viewport, metadataModel);
var downloads = new MockFileSystem('file:///downloads'); var downloads = new MockFileSystem('file:///downloads');
var getGalleryItem = function(path) { var getGalleryItem = function(path) {
return new GalleryItem( return new MockGalleryItem(
new MockEntry(downloads, path), {isReadOnly: false}, {size: 100}, {}, new MockFileEntry(downloads, path), null, {size: 100});
true /* original */);
}; };
var item = getGalleryItem('/test.webm'); var item = getGalleryItem('/test.webm');
var effect = new ImageView.Effect.None(); var effect = new ImageView.Effect.None();
......
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