6
6
7
7
import static org .mockito .ArgumentMatchers .any ;
8
8
import static org .mockito .ArgumentMatchers .anyBoolean ;
9
+ import static org .mockito .ArgumentMatchers .eq ;
10
+ import static org .mockito .ArgumentMatchers .isNull ;
11
+ import static org .mockito .Mockito .doAnswer ;
9
12
import static org .mockito .Mockito .mock ;
13
+ import static org .mockito .Mockito .mockStatic ;
10
14
import static org .mockito .Mockito .times ;
11
15
import static org .mockito .Mockito .verify ;
12
16
import static org .mockito .Mockito .when ;
13
17
18
+ import android .content .Context ;
14
19
import android .webkit .DownloadListener ;
15
20
import android .webkit .WebChromeClient ;
16
21
import android .webkit .WebView ;
22
+ import io .flutter .plugin .common .MethodCall ;
23
+ import io .flutter .plugin .common .MethodChannel ;
17
24
import java .util .HashMap ;
18
25
import java .util .Map ;
19
26
import org .junit .Before ;
20
27
import org .junit .Test ;
28
+ import org .mockito .MockedStatic ;
21
29
22
30
public class FlutterWebViewTest {
23
31
private WebChromeClient mockWebChromeClient ;
24
32
private DownloadListener mockDownloadListener ;
25
33
private WebViewBuilder mockWebViewBuilder ;
26
34
private WebView mockWebView ;
35
+ private MethodChannel .Result mockResult ;
36
+ private Context mockContext ;
37
+ private MethodChannel mockMethodChannel ;
27
38
28
39
@ Before
29
40
public void before () {
41
+
30
42
mockWebChromeClient = mock (WebChromeClient .class );
31
43
mockWebViewBuilder = mock (WebViewBuilder .class );
32
44
mockWebView = mock (WebView .class );
33
45
mockDownloadListener = mock (DownloadListener .class );
46
+ mockResult = mock (MethodChannel .Result .class );
47
+ mockContext = mock (Context .class );
48
+ mockMethodChannel = mock (MethodChannel .class );
34
49
35
50
when (mockWebViewBuilder .setDomStorageEnabled (anyBoolean ())).thenReturn (mockWebViewBuilder );
36
51
when (mockWebViewBuilder .setJavaScriptCanOpenWindowsAutomatically (anyBoolean ()))
@@ -42,12 +57,11 @@ public void before() {
42
57
.thenReturn (mockWebViewBuilder );
43
58
when (mockWebViewBuilder .setDownloadListener (any (DownloadListener .class )))
44
59
.thenReturn (mockWebViewBuilder );
45
-
46
60
when (mockWebViewBuilder .build ()).thenReturn (mockWebView );
47
61
}
48
62
49
63
@ Test
50
- public void createWebView_should_create_webview_with_default_configuration () {
64
+ public void createWebView_shouldCreateWebViewWithDefaultConfiguration () {
51
65
FlutterWebView .createWebView (
52
66
mockWebViewBuilder , createParameterMap (false ), mockWebChromeClient , mockDownloadListener );
53
67
@@ -59,6 +73,97 @@ public void createWebView_should_create_webview_with_default_configuration() {
59
73
verify (mockWebViewBuilder , times (1 )).setZoomControlsEnabled (true );
60
74
}
61
75
76
+ @ Test (expected = UnsupportedOperationException .class )
77
+ public void evaluateJavaScript_shouldThrowForNullString () {
78
+ try (MockedStatic <FlutterWebView > mockedFlutterWebView = mockStatic (FlutterWebView .class )) {
79
+ // Setup
80
+ mockedFlutterWebView
81
+ .when (
82
+ new MockedStatic .Verification () {
83
+ @ Override
84
+ public void apply () throws Throwable {
85
+ FlutterWebView .createWebView (
86
+ (WebViewBuilder ) any (),
87
+ (Map <String , Object >) any (),
88
+ (WebChromeClient ) any (),
89
+ (DownloadListener ) any ());
90
+ }
91
+ })
92
+ .thenReturn (mockWebView );
93
+ FlutterWebView flutterWebView =
94
+ new FlutterWebView (mockContext , mockMethodChannel , new HashMap <String , Object >(), null );
95
+
96
+ // Run
97
+ flutterWebView .onMethodCall (new MethodCall ("runJavascript" , null ), mockResult );
98
+ }
99
+ }
100
+
101
+ @ Test
102
+ public void evaluateJavaScript_shouldReturnValueOnSuccessForReturnValue () {
103
+ try (MockedStatic <FlutterWebView > mockedFlutterWebView = mockStatic (FlutterWebView .class )) {
104
+ // Setup
105
+ mockedFlutterWebView
106
+ .when (
107
+ () ->
108
+ FlutterWebView .createWebView (
109
+ (WebViewBuilder ) any (),
110
+ (Map <String , Object >) any (),
111
+ (WebChromeClient ) any (),
112
+ (DownloadListener ) any ()))
113
+ .thenReturn (mockWebView );
114
+ doAnswer (
115
+ invocation -> {
116
+ android .webkit .ValueCallback <String > callback = invocation .getArgument (1 );
117
+ callback .onReceiveValue ("Test JavaScript Result" );
118
+ return null ;
119
+ })
120
+ .when (mockWebView )
121
+ .evaluateJavascript (eq ("Test JavaScript String" ), any ());
122
+ FlutterWebView flutterWebView =
123
+ new FlutterWebView (mockContext , mockMethodChannel , new HashMap <String , Object >(), null );
124
+
125
+ // Run
126
+ flutterWebView .onMethodCall (
127
+ new MethodCall ("runJavascriptReturningResult" , "Test JavaScript String" ), mockResult );
128
+
129
+ // Verify
130
+ verify (mockResult , times (1 )).success ("Test JavaScript Result" );
131
+ }
132
+ }
133
+
134
+ @ Test
135
+ public void evaluateJavaScript_shouldReturnNilOnSuccessForNoReturnValue () {
136
+ try (MockedStatic <FlutterWebView > mockedFlutterWebView = mockStatic (FlutterWebView .class )) {
137
+ // Setup
138
+ mockedFlutterWebView
139
+ .when (
140
+ () ->
141
+ FlutterWebView .createWebView (
142
+ (WebViewBuilder ) any (),
143
+ (Map <String , Object >) any (),
144
+ (WebChromeClient ) any (),
145
+ (DownloadListener ) any ()))
146
+ .thenReturn (mockWebView );
147
+ doAnswer (
148
+ invocation -> {
149
+ android .webkit .ValueCallback <String > callback = invocation .getArgument (1 );
150
+ callback .onReceiveValue ("Test JavaScript Result" );
151
+ return null ;
152
+ })
153
+ .when (mockWebView )
154
+ .evaluateJavascript (eq ("Test JavaScript String" ), any ());
155
+ FlutterWebView flutterWebView =
156
+ new FlutterWebView (mockContext , mockMethodChannel , new HashMap <String , Object >(), null );
157
+
158
+ // Run
159
+ flutterWebView .onMethodCall (
160
+ new MethodCall ("runJavascript" , "Test JavaScript String" ), mockResult );
161
+
162
+ // Verify
163
+ verify (mockResult , times (1 )).success (isNull ());
164
+ }
165
+ }
166
+
62
167
private Map <String , Object > createParameterMap (boolean usesHybridComposition ) {
63
168
Map <String , Object > params = new HashMap <>();
64
169
params .put ("usesHybridComposition" , usesHybridComposition );
0 commit comments