Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ViewUtilsTest.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.util;
6
7import static org.junit.Assert.assertEquals;
8import static org.junit.Assert.assertFalse;
9import static org.junit.Assert.assertTrue;
10import static org.mockito.Mockito.mock;
11import static org.mockito.Mockito.when;
12
13import android.app.Activity;
14import android.content.Context;
15import android.content.ContextWrapper;
16import android.view.View;
17import android.view.ViewGroup;
18import androidx.test.ext.junit.runners.AndroidJUnit4;
19import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.robolectric.annotation.Config;
22
23@Config(manifest = Config.NONE)
24@RunWith(AndroidJUnit4.class)
25public class ViewUtilsTest {
26 @Test
27 public void canGetActivity() {
28 // Non activity context returns null
29 Context nonActivityContext = mock(Context.class);
30 assertEquals(null, ViewUtils.getActivity(nonActivityContext));
31
32 Activity activity = mock(Activity.class);
33 assertEquals(activity, ViewUtils.getActivity(activity));
34
35 ContextWrapper wrapper = new ContextWrapper(new ContextWrapper(activity));
36 assertEquals(activity, ViewUtils.getActivity(wrapper));
37 }
38
39 @Test
41 final View rootView = mock(View.class);
42 when(rootView.hasFocus()).thenReturn(true);
43 assertTrue(ViewUtils.childHasFocus(rootView));
44 }
45
46 @Test
48 final View rootView = mock(View.class);
49 when(rootView.hasFocus()).thenReturn(false);
50 assertFalse(ViewUtils.childHasFocus(rootView));
51 }
52
53 @Test
55 assertFalse(ViewUtils.childHasFocus(null));
56 }
57
58 @Test
60 final View childView = mock(View.class);
61 when(childView.hasFocus()).thenReturn(true);
62
63 final ViewGroup rootView = mock(ViewGroup.class);
64 when(rootView.getChildCount()).thenReturn(1);
65 when(rootView.getChildAt(0)).thenReturn(childView);
66
67 assertTrue(ViewUtils.childHasFocus(rootView));
68 }
69
70 @Test
72 final View childView = mock(View.class);
73 when(childView.hasFocus()).thenReturn(false);
74
75 final ViewGroup rootView = mock(ViewGroup.class);
76 when(rootView.getChildCount()).thenReturn(1);
77 when(rootView.getChildAt(0)).thenReturn(childView);
78
79 assertFalse(ViewUtils.childHasFocus(rootView));
80 }
81}
static Activity getActivity(@Nullable Context context)
static boolean childHasFocus(@Nullable View root)