Flutter Engine
The Flutter Engine
RestorationChannelTest.java
Go to the documentation of this file.
1package io.flutter.embedding.engine.systemchannels;
2
3import static io.flutter.Build.API_LEVELS;
4import static org.junit.Assert.assertEquals;
5import static org.mockito.Mockito.any;
6import static org.mockito.Mockito.eq;
7import static org.mockito.Mockito.mock;
8import static org.mockito.Mockito.times;
9import static org.mockito.Mockito.verify;
10import static org.mockito.Mockito.verifyNoInteractions;
11
12import android.annotation.TargetApi;
13import androidx.test.ext.junit.runners.AndroidJUnit4;
14import io.flutter.plugin.common.MethodCall;
15import io.flutter.plugin.common.MethodChannel;
16import java.util.HashMap;
17import java.util.Map;
18import org.json.JSONException;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.mockito.ArgumentCaptor;
22import org.robolectric.annotation.Config;
23
24@Config(
25 manifest = Config.NONE,
26 shadows = {})
27@RunWith(AndroidJUnit4.class)
28@TargetApi(API_LEVELS.API_24)
30 @Test
32 throws JSONException {
33 MethodChannel rawChannel = mock(MethodChannel.class);
34 RestorationChannel restorationChannel =
35 new RestorationChannel(rawChannel, /*waitForRestorationData=*/ false);
36 restorationChannel.setRestorationData("Any String you want".getBytes());
37 verify(rawChannel, times(0)).invokeMethod(any(), any());
38 }
39
40 @Test
41 public void itSendsDataOverWhenRequestIsPending() throws JSONException {
42 byte[] data = "Any String you want".getBytes();
43
44 MethodChannel rawChannel = mock(MethodChannel.class);
45 RestorationChannel restorationChannel =
46 new RestorationChannel(rawChannel, /*waitForRestorationData=*/ true);
47 ArgumentCaptor<MethodChannel.MethodCallHandler> argumentCaptor =
48 ArgumentCaptor.forClass(MethodChannel.MethodCallHandler.class);
49 verify(rawChannel).setMethodCallHandler(argumentCaptor.capture());
50
51 MethodChannel.Result result = mock(MethodChannel.Result.class);
52 argumentCaptor.getValue().onMethodCall(new MethodCall("get", null), result);
53 verifyNoInteractions(result);
54
55 restorationChannel.setRestorationData(data);
56 verify(rawChannel, times(0)).invokeMethod(any(), any());
57 Map<String, Object> expected = new HashMap<>();
58 expected.put("enabled", true);
59 expected.put("data", data);
60 verify(result).success(expected);
61
62 // Next get request is answered right away.
63 MethodChannel.Result result2 = mock(MethodChannel.Result.class);
64 argumentCaptor.getValue().onMethodCall(new MethodCall("get", null), result2);
65 verify(result2).success(expected);
66 }
67
68 @Test
69 public void itPushesNewData() throws JSONException {
70 byte[] data = "Any String you want".getBytes();
71
72 MethodChannel rawChannel = mock(MethodChannel.class);
73 RestorationChannel restorationChannel =
74 new RestorationChannel(rawChannel, /*waitForRestorationData=*/ false);
75 ArgumentCaptor<MethodChannel.MethodCallHandler> argumentCaptor =
76 ArgumentCaptor.forClass(MethodChannel.MethodCallHandler.class);
77 verify(rawChannel).setMethodCallHandler(argumentCaptor.capture());
78
79 MethodChannel.Result result = mock(MethodChannel.Result.class);
80 argumentCaptor.getValue().onMethodCall(new MethodCall("get", null), result);
81 Map<String, Object> expected = new HashMap<>();
82 expected.put("enabled", true);
83 expected.put("data", null);
84 verify(result).success(expected);
85
86 restorationChannel.setRestorationData(data);
87 assertEquals(restorationChannel.getRestorationData(), null);
88
89 ArgumentCaptor<MethodChannel.Result> resultCapture =
90 ArgumentCaptor.forClass(MethodChannel.Result.class);
91 Map<String, Object> expected2 = new HashMap<>();
92 expected2.put("enabled", true);
93 expected2.put("data", data);
94 verify(rawChannel).invokeMethod(eq("push"), eq(expected2), resultCapture.capture());
95 resultCapture.getValue().success(null);
96 assertEquals(restorationChannel.getRestorationData(), data);
97 }
98
99 @Test
100 public void itHoldsOnToDataFromFramework() throws JSONException {
101 byte[] data = "Any String you want".getBytes();
102
103 MethodChannel rawChannel = mock(MethodChannel.class);
104 RestorationChannel restorationChannel =
105 new RestorationChannel(rawChannel, /*waitForRestorationData=*/ false);
106 ArgumentCaptor<MethodChannel.MethodCallHandler> argumentCaptor =
107 ArgumentCaptor.forClass(MethodChannel.MethodCallHandler.class);
108 verify(rawChannel).setMethodCallHandler(argumentCaptor.capture());
109
110 MethodChannel.Result result = mock(MethodChannel.Result.class);
111 argumentCaptor.getValue().onMethodCall(new MethodCall("put", data), result);
112 assertEquals(restorationChannel.getRestorationData(), data);
113 }
114}
static SkISize times(const SkISize &size, float factor)
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
Definition: M44Test.cpp:18
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)
GAsyncResult * result
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63