Flutter Engine
The Flutter Engine
WindowManagerHandlerTest.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.plugin.platform;
6
7import static io.flutter.Build.API_LEVELS;
8import static org.mockito.Mockito.mock;
9import static org.mockito.Mockito.verify;
10import static org.mockito.Mockito.verifyNoInteractions;
11
12import android.annotation.TargetApi;
13import android.view.View;
14import android.view.ViewGroup;
15import android.view.WindowManager;
16import androidx.test.ext.junit.runners.AndroidJUnit4;
17import java.util.concurrent.Executor;
18import java.util.function.Consumer;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.robolectric.annotation.Config;
22
23@Config(manifest = Config.NONE)
24@RunWith(AndroidJUnit4.class)
25@TargetApi(API_LEVELS.API_28)
27 @Test
28 @Config(minSdk = API_LEVELS.API_30)
29 public void windowManagerHandler_passesCorrectlyToFakeWindowViewGroup() {
30 // Mock the WindowManager and FakeWindowViewGroup that get used by the WindowManagerHandler.
31 WindowManager mockWindowManager = mock(WindowManager.class);
32 SingleViewFakeWindowViewGroup mockSingleViewFakeWindowViewGroup =
34
35 View mockView = mock(View.class);
36 ViewGroup.LayoutParams mockLayoutParams = mock(ViewGroup.LayoutParams.class);
37
38 WindowManagerHandler windowManagerHandler =
39 new WindowManagerHandler(mockWindowManager, mockSingleViewFakeWindowViewGroup);
40
41 // removeViewImmediate
42 windowManagerHandler.removeViewImmediate(mockView);
43 verify(mockView).clearAnimation();
44 verify(mockSingleViewFakeWindowViewGroup).removeView(mockView);
45 verifyNoInteractions(mockWindowManager);
46
47 // addView
48 windowManagerHandler.addView(mockView, mockLayoutParams);
49 verify(mockSingleViewFakeWindowViewGroup).addView(mockView, mockLayoutParams);
50 verifyNoInteractions(mockWindowManager);
51
52 // updateViewLayout
53 windowManagerHandler.updateViewLayout(mockView, mockLayoutParams);
54 verify(mockSingleViewFakeWindowViewGroup).updateViewLayout(mockView, mockLayoutParams);
55 verifyNoInteractions(mockWindowManager);
56
57 // removeView
58 windowManagerHandler.updateViewLayout(mockView, mockLayoutParams);
59 verify(mockSingleViewFakeWindowViewGroup).removeView(mockView);
60 verifyNoInteractions(mockWindowManager);
61 }
62
63 @Test
64 @Config(minSdk = API_LEVELS.API_30)
65 public void windowManagerHandler_logAndReturnEarly_whenFakeWindowViewGroupIsNull() {
66 // Mock the WindowManager and FakeWindowViewGroup that get used by the WindowManagerHandler.
67 WindowManager mockWindowManager = mock(WindowManager.class);
68
69 View mockView = mock(View.class);
70 ViewGroup.LayoutParams mockLayoutParams = mock(ViewGroup.LayoutParams.class);
71
72 WindowManagerHandler windowManagerHandler = new WindowManagerHandler(mockWindowManager, null);
73
74 // removeViewImmediate
75 windowManagerHandler.removeViewImmediate(mockView);
76 verifyNoInteractions(mockView);
77 verifyNoInteractions(mockWindowManager);
78
79 // addView
80 windowManagerHandler.addView(mockView, mockLayoutParams);
81 verifyNoInteractions(mockWindowManager);
82
83 // updateViewLayout
84 windowManagerHandler.updateViewLayout(mockView, mockLayoutParams);
85 verifyNoInteractions(mockWindowManager);
86
87 // removeView
88 windowManagerHandler.updateViewLayout(mockView, mockLayoutParams);
89 verifyNoInteractions(mockWindowManager);
90 }
91
92 // This section tests that WindowManagerHandler forwards all of the non-special case calls to the
93 // delegate WindowManager. Because this must include some deprecated WindowManager method calls
94 // (because the proxy overrides every method), we suppress deprecation warnings here.
95 @Test
96 @Config(minSdk = API_LEVELS.API_31)
97 @SuppressWarnings("deprecation")
98 public void windowManagerHandler_forwardsAllOtherCallsToDelegate() {
99 // Mock the WindowManager and FakeWindowViewGroup that get used by the WindowManagerHandler.
100 WindowManager mockWindowManager = mock(WindowManager.class);
101 SingleViewFakeWindowViewGroup mockSingleViewFakeWindowViewGroup =
103
104 WindowManagerHandler windowManagerHandler =
105 new WindowManagerHandler(mockWindowManager, mockSingleViewFakeWindowViewGroup);
106
107 // Verify that all other calls get forwarded to the delegate.
108 Executor mockExecutor = mock(Executor.class);
109 @SuppressWarnings("Unchecked cast")
110 Consumer<Boolean> mockListener = (Consumer<Boolean>) mock(Consumer.class);
111
112 windowManagerHandler.getDefaultDisplay();
113 verify(mockWindowManager).getDefaultDisplay();
114
115 windowManagerHandler.getCurrentWindowMetrics();
116 verify(mockWindowManager).getCurrentWindowMetrics();
117
118 windowManagerHandler.getMaximumWindowMetrics();
119 verify(mockWindowManager).getMaximumWindowMetrics();
120
121 windowManagerHandler.isCrossWindowBlurEnabled();
122 verify(mockWindowManager).isCrossWindowBlurEnabled();
123
124 windowManagerHandler.addCrossWindowBlurEnabledListener(mockListener);
125 verify(mockWindowManager).addCrossWindowBlurEnabledListener(mockListener);
126
127 windowManagerHandler.addCrossWindowBlurEnabledListener(mockExecutor, mockListener);
128 verify(mockWindowManager).addCrossWindowBlurEnabledListener(mockExecutor, mockListener);
129
130 windowManagerHandler.removeCrossWindowBlurEnabledListener(mockListener);
131 verify(mockWindowManager).removeCrossWindowBlurEnabledListener(mockListener);
132 }
133}
static final int API_31
Definition: Build.java:21
static final int API_30
Definition: Build.java:20
void addCrossWindowBlurEnabledListener(@NonNull Consumer< Boolean > listener)
void removeCrossWindowBlurEnabledListener(@NonNull Consumer< Boolean > listener)
void updateViewLayout(View view, ViewGroup.LayoutParams params)
void addView(View view, ViewGroup.LayoutParams params)