Skip to content

Commit 4f79d2e

Browse files
MCurran16MartoYankov
authored andcommitted
fix(iOS): Image cache module not longer sets null in NSCache (#7171)
* Image cache module fix * Cla verified | use const instead of let * Image cache module fix * Cla verified | use const instead of let * Improve tests
1 parent 2f3070c commit 4f79d2e

4 files changed

Lines changed: 64 additions & 22 deletions

File tree

‎AUTHORS‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ John Bristowe <john.bristowe@telerik.com>
1616
Joe D <joed@destefano.homelinux.com>
1717
Kamen Velikov <kamen.velikov@telerik.com>
1818
Lubomir Blagoev <lubomir.blagoev@telerik.com>
19+
Luke Curran <luke@pepper.me>
1920
Matt Donovan <unsecurepeax@gmail.com>
2021
Matthew Knight <anarchicknight@gmail.com>
2122
Mihail Slavchev <mihail.slavchev@telerik.com>
@@ -37,4 +38,4 @@ Vasil Chimev <Vasil.Chimev@telerik.com>
3738
Victor Nascimento <victormota15@gmail.com>
3839
Vladimir Enchev <vladimir.enchev@telerik.com>
3940
Wei Zhang <sagamail@gmail.com>
40-
Yavor Georgiev <yavor.georgiev@telerik.com>
41+
Yavor Georgiev <yavor.georgiev@telerik.com>

‎tests/app/testRunner.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ allTests["TAB-VIEW-NAVIGATION"] = tabViewNavigationTests;
189189
import * as imageTests from "./ui/image/image-tests";
190190
allTests["IMAGE"] = imageTests;
191191

192+
import * as imageCacheTests from "./ui/image-cache/image-cache-tests";
193+
allTests["IMAGE-CACHE"] = imageCacheTests;
194+
192195
import * as sliderTests from "./ui/slider/slider-tests";
193196
allTests["SLIDER"] = sliderTests;
194197

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
1-
// >> image-cache-require
2-
import * as imageCacheModule from "tns-core-modules/ui/image-cache";
1+
import * as imageCacheModule from "tns-core-modules/ui/image-cache";
32
import * as imageSource from "tns-core-modules/image-source";
4-
import * as fs from "tns-core-modules/file-system";
5-
// << image-cache-require
3+
import * as types from "tns-core-modules/utils/types";
4+
import * as TKUnit from "../../TKUnit";
65

7-
export function test_DummyTestForSnippetOnly() {
8-
// >> image-cache-request-images
9-
var cache = new imageCacheModule.Cache();
10-
cache.placeholder = imageSource.fromFile(fs.path.join(__dirname, "res/no-image.png"));
6+
export const test_ImageCache_ValidUrl = function() {
7+
const cache = new imageCacheModule.Cache();
118
cache.maxRequests = 5;
12-
13-
// Enable download while not scrolling
14-
cache.enableDownload();
15-
16-
var imgSouce: imageSource.ImageSource;
17-
var url = "https://github.com/NativeScript.png";
9+
10+
let validKey: string;
11+
12+
let imgSource: imageSource.ImageSource;
13+
const url = "https://github.com/NativeScript.png";
14+
// Try to read the image from the cache
15+
const image = cache.get(url);
16+
if (image) {
17+
// If present -- use it.
18+
imgSource = imageSource.fromNativeSource(image);
19+
}
20+
else {
21+
// If not present -- request its download.
22+
cache.push({
23+
key: url,
24+
url: url,
25+
completed: (image: any, key: string) => {
26+
if (url === key) {
27+
imgSource = imageSource.fromNativeSource(image);
28+
validKey = key;
29+
console.log("Valid url: ", key);
30+
}
31+
}
32+
});
33+
}
34+
35+
TKUnit.waitUntilReady(() => types.isDefined(validKey), 8);
36+
TKUnit.assertEqual(validKey, url, "Key should equal the provided url");
37+
}
38+
39+
export const test_ImageCache_NothingAtProvidedUrl = function() {
40+
const cache = new imageCacheModule.Cache();
41+
cache.maxRequests = 5;
42+
43+
let errorCaught = false;
44+
let errorMessage: string;
45+
46+
let imgSource: imageSource.ImageSource;
47+
const url = "https://github.com/NativeScript-NoImage.png";
1848
// Try to read the image from the cache
19-
var image = cache.get(url);
49+
const image = cache.get(url);
2050
if (image) {
2151
// If present -- use it.
22-
imgSouce = imageSource.fromNativeSource(image);
52+
imgSource = imageSource.fromNativeSource(image);
2353
}
2454
else {
2555
// If not present -- request its download.
@@ -28,13 +58,17 @@ export function test_DummyTestForSnippetOnly() {
2858
url: url,
2959
completed: (image: any, key: string) => {
3060
if (url === key) {
31-
imgSouce = imageSource.fromNativeSource(image);
61+
imgSource = imageSource.fromNativeSource(image);
3262
}
63+
},
64+
error: (key: string) => {
65+
console.log("No image for key: ", key);
66+
errorMessage = `No image for key: ${key}`;
67+
errorCaught = true;
3368
}
3469
});
3570
}
3671

37-
// Disable download while scrolling
38-
cache.disableDownload();
39-
// << image-cache-request-images
72+
TKUnit.waitUntilReady(() => errorCaught);
73+
TKUnit.assertEqual(`No image for key: ${url}`, errorMessage);
4074
}

‎tns-core-modules/ui/image-cache/image-cache.ios.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ export class Cache extends common.Cache {
7272
.then((response) => {
7373
try {
7474
const image = UIImage.alloc().initWithData(response.content.raw);
75-
this._onDownloadCompleted(request.key, image);
75+
if (image) {
76+
this._onDownloadCompleted(request.key, image);
77+
} else {
78+
this._onDownloadError(request.key, new Error("No result for provided url"));
79+
}
7680
} catch (err) {
7781
this._onDownloadError(request.key, err);
7882
}

0 commit comments

Comments
 (0)