blob: fff7e4a04cb41fbe0d7fb5f5c262931bcaa30268 [file] [log] [blame]
ryanlwlin7bc9d862019-12-18 21:24:30 +08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.accessibility;
18
19import static android.view.WindowManager.LayoutParams;
20
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertTrue;
23import static org.mockito.ArgumentMatchers.any;
24import static org.mockito.ArgumentMatchers.eq;
25import static org.mockito.Mockito.doAnswer;
26import static org.mockito.Mockito.verify;
27
28import android.content.Context;
29import android.graphics.Point;
30import android.os.IBinder;
31import android.testing.AndroidTestingRunner;
32import android.view.LayoutInflater;
33import android.view.View;
34import android.view.ViewGroup;
35import android.view.WindowManager;
36
37import androidx.test.filters.SmallTest;
38
39import com.android.systemui.SysuiTestCase;
40
41import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44import org.mockito.ArgumentCaptor;
45import org.mockito.Mock;
46import org.mockito.MockitoAnnotations;
47
48@SmallTest
49@RunWith(AndroidTestingRunner.class)
50public class MirrorWindowControlTest extends SysuiTestCase {
51
52 @Mock WindowManager mWindowManager;
53 @Mock IBinder mIBinder;
54 View mView;
55 int mViewWidth;
56 int mViewHeight;
57
58 StubMirrorWindowControl mStubMirrorWindowControl;
59
60 @Before
61 public void setUp() throws Exception {
62 MockitoAnnotations.initMocks(this);
63 mView = new View(getContext());
64 mViewWidth = 10;
65 mViewHeight = 20;
66 getContext().addMockSystemService(Context.WINDOW_SERVICE, mWindowManager);
67 doAnswer(invocation -> {
68 View view = invocation.getArgument(0);
69 LayoutParams lp = invocation.getArgument(1);
70 view.setLayoutParams(lp);
71 return null;
72 }).when(mWindowManager).addView(any(View.class), any(LayoutParams.class));
73
74 mStubMirrorWindowControl = new StubMirrorWindowControl(getContext(), mView, mViewWidth,
75 mViewHeight);
76 }
77
78 @Test
79 public void showControl_createViewAndAddView() {
80 mStubMirrorWindowControl.showControl(mIBinder);
81
82 assertTrue(mStubMirrorWindowControl.mInvokeOnCreateView);
83 ArgumentCaptor<ViewGroup.LayoutParams> lpCaptor = ArgumentCaptor.forClass(
84 ViewGroup.LayoutParams.class);
85 verify(mWindowManager).addView(any(), lpCaptor.capture());
86 assertTrue(lpCaptor.getValue().width == mViewWidth);
87 assertTrue(lpCaptor.getValue().height == mViewHeight);
88 }
89
90 @Test
91 public void destroyControl_removeView() {
92 mStubMirrorWindowControl.showControl(mIBinder);
93 ArgumentCaptor<View> captor = ArgumentCaptor.forClass(View.class);
94 verify(mWindowManager).addView(captor.capture(), any(LayoutParams.class));
95
96 mStubMirrorWindowControl.destroyControl();
97
98 verify(mWindowManager).removeView(eq(captor.getValue()));
99 }
100
101 @Test
102 public void move_offsetIsCorrect() {
103 ArgumentCaptor<ViewGroup.LayoutParams> lpCaptor = ArgumentCaptor.forClass(
104 ViewGroup.LayoutParams.class);
105 mStubMirrorWindowControl.showControl(mIBinder);
106 verify(mWindowManager).addView(any(), lpCaptor.capture());
107 LayoutParams lp = (LayoutParams) lpCaptor.getValue();
108 Point startPosition = new Point(lp.x, lp.y);
109
110 mStubMirrorWindowControl.move(-10, -20);
111
112 verify(mWindowManager).updateViewLayout(eq(mView), lpCaptor.capture());
113 assertTrue(lpCaptor.getAllValues().size() == 2);
114 lp = (LayoutParams) lpCaptor.getValue();
115 Point currentPosition = new Point(lp.x, lp.y);
116 assertEquals(-10, currentPosition.x - startPosition.x);
117 assertEquals(-20, currentPosition.y - startPosition.y);
118 }
119
120 private static class StubMirrorWindowControl extends MirrorWindowControl {
121 private final int mWidth;
122 private final int mHeight;
123 private final View mView;
124
125 boolean mInvokeOnCreateView = false;
126
127 StubMirrorWindowControl(Context context, View view, int width, int height) {
128 super(context);
129 mView = view;
130 mWidth = width;
131 mHeight = height;
132 }
133
134 @Override
135 public String getWindowTitle() {
136 return "StubMirrorWindowControl";
137 }
138
139 @Override
140 View onCreateView(LayoutInflater inflater, Point viewSize) {
141 mInvokeOnCreateView = true;
142 viewSize.x = mWidth;
143 viewSize.y = mHeight;
144 return mView;
145 }
146
147 }
148}