Flutter Engine
The Flutter Engine
LifecycleChannelTest.java
Go to the documentation of this file.
1package io.flutter.embedding.engine.systemchannels;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.ArgumentMatchers.any;
5import static org.mockito.Mockito.mock;
6import static org.mockito.Mockito.times;
7import static org.mockito.Mockito.verify;
8
9import androidx.test.ext.junit.runners.AndroidJUnit4;
10import io.flutter.plugin.common.BasicMessageChannel;
11import org.junit.Before;
12import org.junit.Test;
13import org.junit.runner.RunWith;
14import org.mockito.ArgumentCaptor;
15import org.robolectric.annotation.Config;
16
17@Config(manifest = Config.NONE)
18@RunWith(AndroidJUnit4.class)
22
23 @Before
24 public void setUp() {
25 mockChannel = mock(BasicMessageChannel.class);
26 lifecycleChannel = new LifecycleChannel(mockChannel);
27 }
28
29 @Test
31 lifecycleChannel.appIsResumed();
32 ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
33 verify(mockChannel, times(1)).send(stringArgumentCaptor.capture());
34 assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());
35
36 lifecycleChannel.noWindowsAreFocused();
37 stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
38 verify(mockChannel, times(2)).send(stringArgumentCaptor.capture());
39 assertEquals("AppLifecycleState.inactive", stringArgumentCaptor.getValue());
40
41 lifecycleChannel.aWindowIsFocused();
42 stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
43 verify(mockChannel, times(3)).send(stringArgumentCaptor.capture());
44 assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());
45
46 // Stays inactive, so no event is sent.
47 lifecycleChannel.appIsInactive();
48 verify(mockChannel, times(4)).send(any(String.class));
49
50 // Stays inactive, so no event is sent.
51 lifecycleChannel.appIsResumed();
52 verify(mockChannel, times(5)).send(any(String.class));
53
54 lifecycleChannel.aWindowIsFocused();
55 stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
56 verify(mockChannel, times(5)).send(stringArgumentCaptor.capture());
57 assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());
58 }
59
60 @Test
62 lifecycleChannel.appIsInactive();
63 ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
64 verify(mockChannel, times(1)).send(stringArgumentCaptor.capture());
65 assertEquals("AppLifecycleState.inactive", stringArgumentCaptor.getValue());
66
67 // Stays inactive, so no event is sent.
68 lifecycleChannel.aWindowIsFocused();
69 verify(mockChannel, times(1)).send(any(String.class));
70
71 // Stays inactive, so no event is sent.
72 lifecycleChannel.noWindowsAreFocused();
73 verify(mockChannel, times(1)).send(any(String.class));
74
75 lifecycleChannel.appIsResumed();
76 lifecycleChannel.aWindowIsFocused();
77 stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
78 verify(mockChannel, times(2)).send(stringArgumentCaptor.capture());
79 assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());
80 }
81
82 @Test
84 // Stays inactive, so no event is sent.
85 lifecycleChannel.appIsPaused();
86 ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
87 verify(mockChannel, times(1)).send(stringArgumentCaptor.capture());
88 assertEquals("AppLifecycleState.paused", stringArgumentCaptor.getValue());
89
90 // Stays paused, so no event is sent.
91 lifecycleChannel.aWindowIsFocused();
92 verify(mockChannel, times(1)).send(any(String.class));
93
94 lifecycleChannel.noWindowsAreFocused();
95 verify(mockChannel, times(1)).send(any(String.class));
96
97 lifecycleChannel.appIsResumed();
98 stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
99 verify(mockChannel, times(2)).send(stringArgumentCaptor.capture());
100 assertEquals("AppLifecycleState.inactive", stringArgumentCaptor.getValue());
101
102 lifecycleChannel.aWindowIsFocused();
103 stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
104 verify(mockChannel, times(3)).send(stringArgumentCaptor.capture());
105 assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());
106 }
107
108 @Test
110 // Stays inactive, so no event is sent.
111 lifecycleChannel.appIsDetached();
112 ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
113 verify(mockChannel, times(1)).send(stringArgumentCaptor.capture());
114 assertEquals("AppLifecycleState.detached", stringArgumentCaptor.getValue());
115
116 // Stays paused, so no event is sent.
117 lifecycleChannel.aWindowIsFocused();
118 verify(mockChannel, times(1)).send(any(String.class));
119
120 lifecycleChannel.noWindowsAreFocused();
121 verify(mockChannel, times(1)).send(any(String.class));
122
123 lifecycleChannel.appIsResumed();
124 stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
125 verify(mockChannel, times(2)).send(stringArgumentCaptor.capture());
126 assertEquals("AppLifecycleState.inactive", stringArgumentCaptor.getValue());
127
128 lifecycleChannel.aWindowIsFocused();
129 stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
130 verify(mockChannel, times(3)).send(stringArgumentCaptor.capture());
131 assertEquals("AppLifecycleState.resumed", stringArgumentCaptor.getValue());
132 }
133}
static SkISize times(const SkISize &size, float factor)
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530