Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions tests/app/ui/image-cache/image-cache-tests.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import * as imageCacheModule from "@nativescript/core/ui/image-cache";
import { ImageSource } from "@nativescript/core/image-source";
import * as types from "@nativescript/core/utils/types";
import { isAndroid, device } from "@nativescript/core/platform";
import lazy from "@nativescript/core/utils/lazy";

import * as TKUnit from "../../tk-unit";

const sdkVersion = lazy(() => parseInt(device.sdkVersion));

export const test_ImageCache_ValidUrl = function () {
export const test_ImageCache_ValidUrl = function (done: (err: Error, res?: string) => void) {
// see https://github.com/NativeScript/NativeScript/issues/6643
if (isAndroid && sdkVersion() < 20) {
return;
Expand All @@ -17,15 +14,14 @@ export const test_ImageCache_ValidUrl = function () {
const cache = new imageCacheModule.Cache();
cache.maxRequests = 5;

let validKey: string;

let imgSource: ImageSource;
const url = "https://github.com/NativeScript.png";
// Try to read the image from the cache
const image = cache.get(url);
if (image) {
// If present -- use it.
imgSource = new ImageSource(image);
done(new Error("The image was found in the cache"));
}
else {
// If not present -- request its download.
Expand All @@ -35,31 +31,26 @@ export const test_ImageCache_ValidUrl = function () {
completed: (image: any, key: string) => {
if (url === key) {
imgSource = new ImageSource(image);
validKey = key;
console.log("Valid url: ", key);
done(null);
}
}
});
}

TKUnit.waitUntilReady(() => types.isDefined(validKey), 8);
TKUnit.assertEqual(validKey, url, "Key should equal the provided url");
};

export const test_ImageCache_NothingAtProvidedUrl = function () {
export const test_ImageCache_NothingAtProvidedUrl = function (done: (err: Error, res?: string) => void) {
const cache = new imageCacheModule.Cache();
cache.maxRequests = 5;

let errorCaught = false;
let errorMessage: string;

let imgSource: ImageSource;
const url = "https://github.com/NativeScript-NoImage.png";
// Try to read the image from the cache
const image = cache.get(url);
if (image) {
// If present -- use it.
imgSource = new ImageSource(image);
done(new Error("The image was found in the cache"));
}
else {
// If not present -- request its download.
Expand All @@ -70,15 +61,12 @@ export const test_ImageCache_NothingAtProvidedUrl = function () {
if (url === key) {
imgSource = new ImageSource(image);
}
done(new Error("The completed callback was not expected to be called"));
},
error: (key: string) => {
console.log("No image for key: ", key);
errorMessage = `No image for key: ${key}`;
errorCaught = true;
done(null);
}
});
}

TKUnit.waitUntilReady(() => errorCaught);
TKUnit.assertEqual(`No image for key: ${url}`, errorMessage);
};