Flutter Engine
The Flutter Engine
VsyncWaiterTest.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.view;
6
7import static org.mockito.ArgumentMatchers.any;
8import static org.mockito.ArgumentMatchers.anyLong;
9import static org.mockito.ArgumentMatchers.eq;
10import static org.mockito.ArgumentMatchers.isNull;
11import static org.mockito.Mockito.mock;
12import static org.mockito.Mockito.times;
13import static org.mockito.Mockito.verify;
14import static org.mockito.Mockito.when;
15import static org.robolectric.Shadows.shadowOf;
16
17import android.hardware.display.DisplayManager;
18import android.os.Looper;
19import android.view.Display;
20import androidx.test.ext.junit.runners.AndroidJUnit4;
21import io.flutter.embedding.engine.FlutterJNI;
22import org.junit.Before;
23import org.junit.Test;
24import org.junit.runner.RunWith;
25import org.mockito.ArgumentCaptor;
26import org.robolectric.annotation.Config;
27
28@Config(manifest = Config.NONE)
29@RunWith(AndroidJUnit4.class)
30public class VsyncWaiterTest {
31 @Before
32 public void setUp() {
34 }
35
36 @Test
37 public void itSetsFpsBelowApi17() {
38 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
39 VsyncWaiter waiter = VsyncWaiter.getInstance(10.0f, mockFlutterJNI);
40 verify(mockFlutterJNI, times(1)).setRefreshRateFPS(10.0f);
41
42 waiter.init();
43
44 ArgumentCaptor<FlutterJNI.AsyncWaitForVsyncDelegate> delegateCaptor =
45 ArgumentCaptor.forClass(FlutterJNI.AsyncWaitForVsyncDelegate.class);
46 verify(mockFlutterJNI, times(1)).setAsyncWaitForVsyncDelegate(delegateCaptor.capture());
47 delegateCaptor.getValue().asyncWaitForVsync(1);
48 shadowOf(Looper.getMainLooper()).idle();
49 verify(mockFlutterJNI, times(1)).onVsync(anyLong(), eq(1000000000l / 10l), eq(1l));
50 }
51
52 @Test
54 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
55 DisplayManager mockDisplayManager = mock(DisplayManager.class);
56 Display mockDisplay = mock(Display.class);
57 ArgumentCaptor<VsyncWaiter.DisplayListener> displayListenerCaptor =
58 ArgumentCaptor.forClass(VsyncWaiter.DisplayListener.class);
59 when(mockDisplayManager.getDisplay(Display.DEFAULT_DISPLAY)).thenReturn(mockDisplay);
60
61 VsyncWaiter waiter = VsyncWaiter.getInstance(mockDisplayManager, mockFlutterJNI);
62 verify(mockDisplayManager, times(1))
63 .registerDisplayListener(displayListenerCaptor.capture(), isNull());
64
65 when(mockDisplay.getRefreshRate()).thenReturn(90.0f);
66 displayListenerCaptor.getValue().onDisplayChanged(Display.DEFAULT_DISPLAY);
67 verify(mockFlutterJNI, times(1)).setRefreshRateFPS(90.0f);
68
69 waiter.init();
70
71 ArgumentCaptor<FlutterJNI.AsyncWaitForVsyncDelegate> delegateCaptor =
72 ArgumentCaptor.forClass(FlutterJNI.AsyncWaitForVsyncDelegate.class);
73 verify(mockFlutterJNI, times(1)).setAsyncWaitForVsyncDelegate(delegateCaptor.capture());
74 delegateCaptor.getValue().asyncWaitForVsync(1);
75 shadowOf(Looper.getMainLooper()).idle();
76 verify(mockFlutterJNI, times(1)).onVsync(anyLong(), eq(1000000000l / 90l), eq(1l));
77
78 when(mockDisplay.getRefreshRate()).thenReturn(60.0f);
79 displayListenerCaptor.getValue().onDisplayChanged(Display.DEFAULT_DISPLAY);
80 verify(mockFlutterJNI, times(1)).setRefreshRateFPS(60.0f);
81
82 delegateCaptor.getValue().asyncWaitForVsync(1);
83 shadowOf(Looper.getMainLooper()).idle();
84 verify(mockFlutterJNI, times(1)).onVsync(anyLong(), eq(1000000000l / 60l), eq(1l));
85 }
86
87 @Test
89 FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
90 DisplayManager mockDisplayManager = mock(DisplayManager.class);
91 Display mockDisplay = mock(Display.class);
92 when(mockDisplayManager.getDisplay(Display.DEFAULT_DISPLAY)).thenReturn(mockDisplay);
93 when(mockDisplay.getRefreshRate()).thenReturn(90.0f);
94
95 VsyncWaiter waiter = VsyncWaiter.getInstance(mockDisplayManager, mockFlutterJNI);
96 verify(mockDisplayManager, times(1)).registerDisplayListener(any(), isNull());
97
98 verify(mockFlutterJNI, times(1)).setRefreshRateFPS(90.0f);
99 }
100}
static SkISize times(const SkISize &size, float factor)
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
Definition: M44Test.cpp:18
static VsyncWaiter getInstance(float fps, @NonNull FlutterJNI flutterJNI)
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530