11import * as imageCacheModule from "@nativescript/core/ui/image-cache" ;
22import { ImageSource } from "@nativescript/core/image-source" ;
3- import * as types from "@nativescript/core/utils/types" ;
43import { isAndroid , device } from "@nativescript/core/platform" ;
54import lazy from "@nativescript/core/utils/lazy" ;
65
7- import * as TKUnit from "../../tk-unit" ;
8-
96const sdkVersion = lazy ( ( ) => parseInt ( device . sdkVersion ) ) ;
107
11- export const test_ImageCache_ValidUrl = function ( ) {
8+ export const test_ImageCache_ValidUrl = function ( done : ( err : Error , res ?: string ) => void ) {
129 // see https://github.com/NativeScript/NativeScript/issues/6643
1310 if ( isAndroid && sdkVersion ( ) < 20 ) {
1411 return ;
@@ -17,15 +14,14 @@ export const test_ImageCache_ValidUrl = function () {
1714 const cache = new imageCacheModule . Cache ( ) ;
1815 cache . maxRequests = 5 ;
1916
20- let validKey : string ;
21-
2217 let imgSource : ImageSource ;
2318 const url = "https://github.com/NativeScript.png" ;
2419 // Try to read the image from the cache
2520 const image = cache . get ( url ) ;
2621 if ( image ) {
2722 // If present -- use it.
2823 imgSource = new ImageSource ( image ) ;
24+ done ( new Error ( "The image was found in the cache" ) ) ;
2925 }
3026 else {
3127 // If not present -- request its download.
@@ -35,31 +31,26 @@ export const test_ImageCache_ValidUrl = function () {
3531 completed : ( image : any , key : string ) => {
3632 if ( url === key ) {
3733 imgSource = new ImageSource ( image ) ;
38- validKey = key ;
3934 console . log ( "Valid url: " , key ) ;
35+ done ( null ) ;
4036 }
4137 }
4238 } ) ;
4339 }
44-
45- TKUnit . waitUntilReady ( ( ) => types . isDefined ( validKey ) , 8 ) ;
46- TKUnit . assertEqual ( validKey , url , "Key should equal the provided url" ) ;
4740} ;
4841
49- export const test_ImageCache_NothingAtProvidedUrl = function ( ) {
42+ export const test_ImageCache_NothingAtProvidedUrl = function ( done : ( err : Error , res ?: string ) => void ) {
5043 const cache = new imageCacheModule . Cache ( ) ;
5144 cache . maxRequests = 5 ;
5245
53- let errorCaught = false ;
54- let errorMessage : string ;
55-
5646 let imgSource : ImageSource ;
5747 const url = "https://github.com/NativeScript-NoImage.png" ;
5848 // Try to read the image from the cache
5949 const image = cache . get ( url ) ;
6050 if ( image ) {
6151 // If present -- use it.
6252 imgSource = new ImageSource ( image ) ;
53+ done ( new Error ( "The image was found in the cache" ) ) ;
6354 }
6455 else {
6556 // If not present -- request its download.
@@ -70,15 +61,12 @@ export const test_ImageCache_NothingAtProvidedUrl = function () {
7061 if ( url === key ) {
7162 imgSource = new ImageSource ( image ) ;
7263 }
64+ done ( new Error ( "The completed callback was not expected to be called" ) ) ;
7365 } ,
7466 error : ( key : string ) => {
7567 console . log ( "No image for key: " , key ) ;
76- errorMessage = `No image for key: ${ key } ` ;
77- errorCaught = true ;
68+ done ( null ) ;
7869 }
7970 } ) ;
8071 }
81-
82- TKUnit . waitUntilReady ( ( ) => errorCaught ) ;
83- TKUnit . assertEqual ( `No image for key: ${ url } ` , errorMessage ) ;
8472} ;
0 commit comments