TestWindowManagerPolicy.java revision dff7a73979eea381c704e9165ec62335fae1c6c7
1/*
2 * Copyright (C) 2016 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.server.wm;
18
19import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
20import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
21
22import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
23
24import static org.mockito.ArgumentMatchers.anyString;
25import static org.mockito.Matchers.anyInt;
26import static org.mockito.Mockito.doReturn;
27import static org.mockito.Mockito.mock;
28import static org.mockito.Mockito.any;
29import static org.mockito.Mockito.doAnswer;
30
31import android.os.PowerSaveState;
32import android.util.proto.ProtoOutputStream;
33import org.mockito.invocation.InvocationOnMock;
34
35import android.annotation.Nullable;
36import android.app.ActivityManagerInternal;
37import android.content.Context;
38import android.content.res.CompatibilityInfo;
39import android.content.res.Configuration;
40import android.graphics.Rect;
41import android.hardware.display.DisplayManagerInternal;
42import android.os.Bundle;
43import android.os.IBinder;
44import android.os.RemoteException;
45import android.view.Display;
46import android.view.IWindowManager;
47import android.view.InputChannel;
48import android.view.KeyEvent;
49import android.view.WindowManager;
50import android.view.animation.Animation;
51import android.os.PowerManagerInternal;
52
53import com.android.internal.policy.IKeyguardDismissCallback;
54import com.android.internal.policy.IShortcutService;
55import com.android.server.input.InputManagerService;
56import com.android.server.LocalServices;
57import com.android.server.policy.WindowManagerPolicy;
58
59import java.io.PrintWriter;
60
61class TestWindowManagerPolicy implements WindowManagerPolicy {
62    private static final String TAG = "TestWindowManagerPolicy";
63
64    private static WindowManagerService sWm = null;
65
66    int rotationToReport = 0;
67
68    private Runnable mRunnableWhenAddingSplashScreen;
69
70    static synchronized WindowManagerService getWindowManagerService(Context context) {
71        if (sWm == null) {
72            // We only want to do this once for the test process as we don't want WM to try to
73            // register a bunch of local services again.
74            if (LocalServices.getService(DisplayManagerInternal.class) == null) {
75                LocalServices.addService(DisplayManagerInternal.class,
76                        mock(DisplayManagerInternal.class));
77            }
78            if (LocalServices.getService(PowerManagerInternal.class) == null) {
79                LocalServices.addService(PowerManagerInternal.class,
80                        mock(PowerManagerInternal.class));
81                final PowerManagerInternal pm =
82                        LocalServices.getService(PowerManagerInternal.class);
83                PowerSaveState state = new PowerSaveState.Builder().build();
84                doReturn(state).when(pm).getLowPowerState(anyInt());
85            }
86            if (LocalServices.getService(ActivityManagerInternal.class) == null) {
87                LocalServices.addService(ActivityManagerInternal.class,
88                        mock(ActivityManagerInternal.class));
89                final ActivityManagerInternal am =
90                        LocalServices.getService(ActivityManagerInternal.class);
91                doAnswer((InvocationOnMock invocationOnMock) -> {
92                    final Runnable runnable = invocationOnMock.<Runnable>getArgument(0);
93                    if (runnable != null) {
94                        runnable.run();
95                    }
96                    return null;
97                }).when(am).notifyKeyguardFlagsChanged(any());
98            }
99
100            InputManagerService ims = mock(InputManagerService.class);
101            // InputChannel is final and can't be mocked.
102            InputChannel[] input = InputChannel.openInputChannelPair(TAG_WM);
103            if (input != null && input.length > 1) {
104                doReturn(input[1]).when(ims).monitorInput(anyString());
105            }
106
107            sWm = WindowManagerService.main(context, ims, true, false,
108                    false, new TestWindowManagerPolicy());
109        }
110        return sWm;
111    }
112
113    @Override
114    public void registerShortcutKey(long shortcutCode, IShortcutService shortcutKeyReceiver)
115            throws RemoteException {
116
117    }
118
119    @Override
120    public void init(Context context, IWindowManager windowManager,
121            WindowManagerFuncs windowManagerFuncs) {
122
123    }
124
125    @Override
126    public boolean isDefaultOrientationForced() {
127        return false;
128    }
129
130    @Override
131    public void setInitialDisplaySize(Display display, int width, int height, int density) {
132
133    }
134
135    @Override
136    public int checkAddPermission(WindowManager.LayoutParams attrs, int[] outAppOp) {
137        return 0;
138    }
139
140    @Override
141    public boolean checkShowToOwnerOnly(WindowManager.LayoutParams attrs) {
142        return false;
143    }
144
145    @Override
146    public void adjustWindowParamsLw(WindowState win, WindowManager.LayoutParams attrs,
147            boolean hasStatusBarServicePermission) {
148    }
149
150    @Override
151    public void adjustConfigurationLw(Configuration config, int keyboardPresence,
152            int navigationPresence) {
153
154    }
155
156    @Override
157    public int getMaxWallpaperLayer() {
158        return 0;
159    }
160
161    @Override
162    public int getNonDecorDisplayWidth(int fullWidth, int fullHeight, int rotation, int uiMode,
163            int displayId) {
164        return 0;
165    }
166
167    @Override
168    public int getNonDecorDisplayHeight(int fullWidth, int fullHeight, int rotation, int uiMode,
169            int displayId) {
170        return 0;
171    }
172
173    @Override
174    public int getConfigDisplayWidth(int fullWidth, int fullHeight, int rotation, int uiMode,
175            int displayId) {
176        return 0;
177    }
178
179    @Override
180    public int getConfigDisplayHeight(int fullWidth, int fullHeight, int rotation, int uiMode,
181            int displayId) {
182        return 0;
183    }
184
185    @Override
186    public boolean isKeyguardHostWindow(WindowManager.LayoutParams attrs) {
187        return attrs.type == TYPE_STATUS_BAR;
188    }
189
190    @Override
191    public boolean canBeHiddenByKeyguardLw(WindowState win) {
192        return false;
193    }
194
195    /**
196     * Sets a runnable to run when adding a splash screen which gets executed after the window has
197     * been added but before returning the surface.
198     */
199    void setRunnableWhenAddingSplashScreen(Runnable r) {
200        mRunnableWhenAddingSplashScreen = r;
201    }
202
203    @Override
204    public StartingSurface addSplashScreen(IBinder appToken, String packageName, int theme,
205            CompatibilityInfo compatInfo, CharSequence nonLocalizedLabel, int labelRes, int icon,
206            int logo, int windowFlags, Configuration overrideConfig, int displayId) {
207        final com.android.server.wm.WindowState window;
208        final AppWindowToken atoken;
209        synchronized (sWm.mWindowMap) {
210            atoken = sWm.mRoot.getAppWindowToken(appToken);
211            window = WindowTestsBase.createWindow(null, TYPE_APPLICATION_STARTING, atoken,
212                    "Starting window");
213            atoken.startingWindow = window;
214        }
215        if (mRunnableWhenAddingSplashScreen != null) {
216            mRunnableWhenAddingSplashScreen.run();
217            mRunnableWhenAddingSplashScreen = null;
218        }
219        return () -> {
220            synchronized (sWm.mWindowMap) {
221                atoken.removeChild(window);
222                atoken.startingWindow = null;
223            }
224        };
225    }
226
227    @Override
228    public int prepareAddWindowLw(WindowState win,
229            WindowManager.LayoutParams attrs) {
230        return 0;
231    }
232
233    @Override
234    public void removeWindowLw(WindowState win) {
235
236    }
237
238    @Override
239    public int selectAnimationLw(WindowState win, int transit) {
240        return 0;
241    }
242
243    @Override
244    public void selectRotationAnimationLw(int[] anim) {
245
246    }
247
248    @Override
249    public boolean validateRotationAnimationLw(int exitAnimId, int enterAnimId,
250            boolean forceDefault) {
251        return false;
252    }
253
254    @Override
255    public Animation createHiddenByKeyguardExit(boolean onWallpaper,
256            boolean goingToNotificationShade) {
257        return null;
258    }
259
260    @Override
261    public Animation createKeyguardWallpaperExit(boolean goingToNotificationShade) {
262        return null;
263    }
264
265    @Override
266    public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
267        return 0;
268    }
269
270    @Override
271    public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
272        return 0;
273    }
274
275    @Override
276    public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event,
277            int policyFlags) {
278        return 0;
279    }
280
281    @Override
282    public KeyEvent dispatchUnhandledKey(WindowState win, KeyEvent event,
283            int policyFlags) {
284        return null;
285    }
286
287    @Override
288    public int getSystemDecorLayerLw() {
289        return 0;
290    }
291
292    @Override
293    public void beginPostLayoutPolicyLw(int displayWidth, int displayHeight) {
294
295    }
296
297    @Override
298    public void applyPostLayoutPolicyLw(WindowState win,
299            WindowManager.LayoutParams attrs, WindowState attached, WindowState imeTarget) {
300    }
301
302    @Override
303    public int finishPostLayoutPolicyLw() {
304        return 0;
305    }
306
307    @Override
308    public boolean allowAppAnimationsLw() {
309        return false;
310    }
311
312    @Override
313    public int focusChangedLw(WindowState lastFocus,
314            WindowState newFocus) {
315        return 0;
316    }
317
318    @Override
319    public void startedWakingUp() {
320
321    }
322
323    @Override
324    public void finishedWakingUp() {
325
326    }
327
328    @Override
329    public void startedGoingToSleep(int why) {
330
331    }
332
333    @Override
334    public void finishedGoingToSleep(int why) {
335
336    }
337
338    @Override
339    public void screenTurningOn(ScreenOnListener screenOnListener) {
340
341    }
342
343    @Override
344    public void screenTurnedOn() {
345
346    }
347
348    @Override
349    public void screenTurningOff(ScreenOffListener screenOffListener) {
350
351    }
352
353    @Override
354    public void screenTurnedOff() {
355
356    }
357
358    @Override
359    public boolean isScreenOn() {
360        return true;
361    }
362
363    @Override
364    public boolean okToAnimate() {
365        return true;
366    }
367
368    @Override
369    public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
370
371    }
372
373    @Override
374    public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) {
375
376    }
377
378    @Override
379    public void enableKeyguard(boolean enabled) {
380
381    }
382
383    @Override
384    public void exitKeyguardSecurely(OnKeyguardExitResult callback) {
385
386    }
387
388    @Override
389    public boolean isKeyguardLocked() {
390        return false;
391    }
392
393    @Override
394    public boolean isKeyguardSecure(int userId) {
395        return false;
396    }
397
398    @Override
399    public boolean isKeyguardOccluded() {
400        return false;
401    }
402
403    @Override
404    public boolean isKeyguardTrustedLw() {
405        return false;
406    }
407
408    @Override
409    public boolean isKeyguardShowingAndNotOccluded() {
410        return false;
411    }
412
413    @Override
414    public boolean inKeyguardRestrictedKeyInputMode() {
415        return false;
416    }
417
418    @Override
419    public void dismissKeyguardLw(@Nullable IKeyguardDismissCallback callback,
420            CharSequence message) {
421    }
422
423    @Override
424    public boolean isKeyguardDrawnLw() {
425        return false;
426    }
427
428    @Override
429    public boolean isShowingDreamLw() {
430        return false;
431    }
432
433    @Override
434    public void onKeyguardOccludedChangedLw(boolean occluded) {
435    }
436
437    @Override
438    public int rotationForOrientationLw(int orientation,
439            int lastRotation) {
440        return rotationToReport;
441    }
442
443    @Override
444    public boolean rotationHasCompatibleMetricsLw(int orientation, int rotation) {
445        return true;
446    }
447
448    @Override
449    public void setRotationLw(int rotation) {
450
451    }
452
453    @Override
454    public void setSafeMode(boolean safeMode) {
455
456    }
457
458    @Override
459    public void systemReady() {
460
461    }
462
463    @Override
464    public void systemBooted() {
465
466    }
467
468    @Override
469    public void showBootMessage(CharSequence msg, boolean always) {
470
471    }
472
473    @Override
474    public void hideBootMessages() {
475
476    }
477
478    @Override
479    public void userActivity() {
480
481    }
482
483    @Override
484    public void enableScreenAfterBoot() {
485
486    }
487
488    @Override
489    public void setCurrentOrientationLw(int newOrientation) {
490
491    }
492
493    @Override
494    public boolean performHapticFeedbackLw(WindowState win, int effectId,
495            boolean always) {
496        return false;
497    }
498
499    @Override
500    public void keepScreenOnStartedLw() {
501
502    }
503
504    @Override
505    public void keepScreenOnStoppedLw() {
506
507    }
508
509    @Override
510    public int getUserRotationMode() {
511        return 0;
512    }
513
514    @Override
515    public void setUserRotationMode(int mode,
516            int rotation) {
517
518    }
519
520    @Override
521    public int adjustSystemUiVisibilityLw(int visibility) {
522        return 0;
523    }
524
525    @Override
526    public boolean hasNavigationBar() {
527        return false;
528    }
529
530    @Override
531    public void lockNow(Bundle options) {
532
533    }
534
535    @Override
536    public void setLastInputMethodWindowLw(WindowState ime,
537            WindowState target) {
538
539    }
540
541    @Override
542    public void showRecentApps() {
543
544    }
545
546    @Override
547    public void showGlobalActions() {
548
549    }
550
551    @Override
552    public void setCurrentUserLw(int newUserId) {
553
554    }
555
556    @Override
557    public void setSwitchingUser(boolean switching) {
558
559    }
560
561    @Override
562    public void writeToProto(ProtoOutputStream proto, long fieldId) {
563
564    }
565
566    @Override
567    public void dump(String prefix, PrintWriter writer, String[] args) {
568
569    }
570
571    @Override
572    public boolean isTopLevelWindow(int windowType) {
573        return false;
574    }
575
576    @Override
577    public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
578
579    }
580
581    @Override
582    public void getStableInsetsLw(int displayRotation, int displayWidth, int displayHeight,
583            Rect outInsets) {
584
585    }
586
587    @Override
588    public boolean isNavBarForcedShownLw(WindowState win) {
589        return false;
590    }
591
592    @NavigationBarPosition
593    @Override
594    public int getNavBarPosition() {
595        return NAV_BAR_BOTTOM;
596    }
597
598    @Override
599    public void getNonDecorInsetsLw(int displayRotation, int displayWidth, int displayHeight,
600            Rect outInsets) {
601
602    }
603
604    @Override
605    public boolean isDockSideAllowed(int dockSide) {
606        return false;
607    }
608
609    @Override
610    public void onConfigurationChanged() {
611
612    }
613
614    @Override
615    public boolean shouldRotateSeamlessly(int oldRotation, int newRotation) {
616        return false;
617    }
618
619    @Override
620    public void setPipVisibilityLw(boolean visible) {
621
622    }
623
624    @Override
625    public void setRecentsVisibilityLw(boolean visible) {
626
627    }
628
629    @Override
630    public void onSystemUiStarted() {
631    }
632
633    @Override
634    public boolean canDismissBootAnimation() {
635        return true;
636    }
637}
638