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" ;
32import * 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}
0 commit comments