15
15
*/
16
16
package com .squareup .picasso ;
17
17
18
+ import android .content .Context ;
19
+ import android .content .res .Resources ;
18
20
import android .graphics .Bitmap ;
19
21
import android .graphics .drawable .Drawable ;
20
22
import org .junit .Test ;
23
25
import org .robolectric .annotation .Config ;
24
26
25
27
import static com .squareup .picasso .Picasso .LoadedFrom .MEMORY ;
28
+ import static com .squareup .picasso .Picasso .RequestTransformer .IDENTITY ;
26
29
import static com .squareup .picasso .TestUtils .BITMAP_1 ;
27
30
import static com .squareup .picasso .TestUtils .BITMAP_3 ;
31
+ import static com .squareup .picasso .TestUtils .RESOURCE_ID_1 ;
28
32
import static com .squareup .picasso .TestUtils .URI_KEY_1 ;
29
33
import static com .squareup .picasso .TestUtils .mockTarget ;
30
34
import static org .junit .Assert .fail ;
31
35
import static org .mockito .Mockito .mock ;
32
36
import static org .mockito .Mockito .verify ;
37
+ import static org .mockito .Mockito .when ;
33
38
34
39
@ RunWith (RobolectricTestRunner .class )
35
40
@ Config (manifest = Config .NONE )
@@ -38,24 +43,42 @@ public class TargetActionTest {
38
43
@ Test (expected = AssertionError .class )
39
44
public void throwsErrorWithNullResult () throws Exception {
40
45
TargetAction request =
41
- new TargetAction (mock (Picasso .class ), mockTarget (), null , false , URI_KEY_1 );
46
+ new TargetAction (mock (Picasso .class ), mockTarget (), null , false , 0 , null , URI_KEY_1 );
42
47
request .complete (null , MEMORY );
43
48
}
44
49
45
50
@ Test
46
51
public void invokesSuccessIfTargetIsNotNull () throws Exception {
47
52
Target target = mockTarget ();
48
- TargetAction request = new TargetAction (mock (Picasso .class ), target , null , false , URI_KEY_1 );
53
+ TargetAction request = new TargetAction (mock (Picasso .class ), target , null , false , 0 , null , URI_KEY_1 );
49
54
request .complete (BITMAP_3 , MEMORY );
50
55
verify (target ).onBitmapLoaded (BITMAP_3 , MEMORY );
51
56
}
52
57
53
58
@ Test
54
- public void invokesErrorIfTargetIsNotNull () throws Exception {
59
+ public void invokesOnBitmapFailedIfTargetIsNotNullWithErrorDrawable () throws Exception {
60
+ Drawable errorDrawable = mock (Drawable .class );
55
61
Target target = mockTarget ();
56
- TargetAction request = new TargetAction (mock (Picasso .class ), target , null , false , URI_KEY_1 );
62
+ TargetAction request = new TargetAction (mock (Picasso .class ), target , null , false , 0 , errorDrawable , URI_KEY_1 );
57
63
request .error ();
58
- verify (target ).onBitmapFailed (null );
64
+ verify (target ).onBitmapFailed (errorDrawable );
65
+ }
66
+
67
+ @ Test
68
+ public void invokesOnBitmapFailedIfTargetIsNotNullWithErrorResourceId () throws Exception {
69
+ Drawable errorDrawable = mock (Drawable .class );
70
+ Target target = mockTarget ();
71
+ Context context = mock (Context .class );
72
+ Picasso picasso =
73
+ new Picasso (context , mock (Dispatcher .class ), Cache .NONE , null , IDENTITY ,
74
+ mock (Stats .class ), true );
75
+ Resources res = mock (Resources .class );
76
+ TargetAction request = new TargetAction (picasso , target , null , false , RESOURCE_ID_1 , null , URI_KEY_1 );
77
+
78
+ when (context .getResources ()).thenReturn (res );
79
+ when (res .getDrawable (RESOURCE_ID_1 )).thenReturn (errorDrawable );
80
+ request .error ();
81
+ verify (target ).onBitmapFailed (errorDrawable );
59
82
}
60
83
61
84
@ Test public void recyclingInSuccessThrowsException () {
@@ -74,7 +97,7 @@ public void invokesErrorIfTargetIsNotNull() throws Exception {
74
97
};
75
98
Picasso picasso = mock (Picasso .class );
76
99
77
- TargetAction tr = new TargetAction (picasso , bad , null , false , URI_KEY_1 );
100
+ TargetAction tr = new TargetAction (picasso , bad , null , false , 0 , null , URI_KEY_1 );
78
101
try {
79
102
tr .complete (BITMAP_1 , MEMORY );
80
103
fail ();
0 commit comments