TestWindowManagerPolicy.java revision 828ff7e3ef032f3c3b149be9961fa39a979d2fd2
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.WindowManagerPolicy;
51import android.view.animation.Animation;
52import android.os.PowerManagerInternal;
53
54import com.android.internal.policy.IKeyguardDismissCallback;
55import com.android.internal.policy.IShortcutService;
56import com.android.server.input.InputManagerService;
57import com.android.server.LocalServices;
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    }
421
422    @Override
423    public boolean isKeyguardDrawnLw() {
424        return false;
425    }
426
427    @Override
428    public boolean isShowingDreamLw() {
429        return false;
430    }
431
432    @Override
433    public void onKeyguardOccludedChangedLw(boolean occluded) {
434    }
435
436    @Override
437    public int rotationForOrientationLw(int orientation,
438            int lastRotation) {
439        return rotationToReport;
440    }
441
442    @Override
443    public boolean rotationHasCompatibleMetricsLw(int orientation, int rotation) {
444        return true;
445    }
446
447    @Override
448    public void setRotationLw(int rotation) {
449
450    }
451
452    @Override
453    public void setSafeMode(boolean safeMode) {
454
455    }
456
457    @Override
458    public void systemReady() {
459
460    }
461
462    @Override
463    public void systemBooted() {
464
465    }
466
467    @Override
468    public void showBootMessage(CharSequence msg, boolean always) {
469
470    }
471
472    @Override
473    public void hideBootMessages() {
474
475    }
476
477    @Override
478    public void userActivity() {
479
480    }
481
482    @Override
483    public void enableScreenAfterBoot() {
484
485    }
486
487    @Override
488    public void setCurrentOrientationLw(int newOrientation) {
489
490    }
491
492    @Override
493    public boolean performHapticFeedbackLw(WindowState win, int effectId,
494            boolean always) {
495        return false;
496    }
497
498    @Override
499    public void keepScreenOnStartedLw() {
500
501    }
502
503    @Override
504    public void keepScreenOnStoppedLw() {
505
506    }
507
508    @Override
509    public int getUserRotationMode() {
510        return 0;
511    }
512
513    @Override
514    public void setUserRotationMode(int mode,
515            int rotation) {
516
517    }
518
519    @Override
520    public int adjustSystemUiVisibilityLw(int visibility) {
521        return 0;
522    }
523
524    @Override
525    public boolean hasNavigationBar() {
526        return false;
527    }
528
529    @Override
530    public void lockNow(Bundle options) {
531
532    }
533
534    @Override
535    public void setLastInputMethodWindowLw(WindowState ime,
536            WindowState target) {
537
538    }
539
540    @Override
541    public void showRecentApps(boolean fromHome) {
542
543    }
544
545    @Override
546    public void showGlobalActions() {
547
548    }
549
550    @Override
551    public void setCurrentUserLw(int newUserId) {
552
553    }
554
555    @Override
556    public void setSwitchingUser(boolean switching) {
557
558    }
559
560    @Override
561    public void writeToProto(ProtoOutputStream proto, long fieldId) {
562
563    }
564
565    @Override
566    public void dump(String prefix, PrintWriter writer, String[] args) {
567
568    }
569
570    @Override
571    public boolean canMagnifyWindow(int windowType) {
572        return false;
573    }
574
575    @Override
576    public boolean isTopLevelWindow(int windowType) {
577        return false;
578    }
579
580    @Override
581    public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
582
583    }
584
585    @Override
586    public void getStableInsetsLw(int displayRotation, int displayWidth, int displayHeight,
587            Rect outInsets) {
588
589    }
590
591    @Override
592    public boolean isNavBarForcedShownLw(WindowState win) {
593        return false;
594    }
595
596    @Override
597    public int getNavBarPosition() {
598        return NAV_BAR_BOTTOM;
599    }
600
601    @Override
602    public void getNonDecorInsetsLw(int displayRotation, int displayWidth, int displayHeight,
603            Rect outInsets) {
604
605    }
606
607    @Override
608    public boolean isDockSideAllowed(int dockSide) {
609        return false;
610    }
611
612    @Override
613    public void onConfigurationChanged() {
614
615    }
616
617    @Override
618    public boolean shouldRotateSeamlessly(int oldRotation, int newRotation) {
619        return false;
620    }
621
622    @Override
623    public void setPipVisibilityLw(boolean visible) {
624
625    }
626
627    @Override
628    public void setRecentsVisibilityLw(boolean visible) {
629
630    }
631
632    @Override
633    public void onSystemUiStarted() {
634    }
635
636    @Override
637    public boolean canDismissBootAnimation() {
638        return true;
639    }
640}
641