Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame^] | 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
| 19 | import org.junit.Assert; |
| 20 | import org.junit.Before; |
| 21 | |
| 22 | import android.content.Context; |
| 23 | import android.os.IBinder; |
| 24 | import android.support.test.InstrumentationRegistry; |
| 25 | import android.view.Display; |
| 26 | import android.view.IWindow; |
| 27 | import android.view.WindowManager; |
| 28 | |
| 29 | import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID; |
| 30 | import static android.app.AppOpsManager.OP_NONE; |
| 31 | import static android.content.res.Configuration.EMPTY; |
| 32 | import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW; |
| 33 | import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW; |
| 34 | import static org.mockito.Mockito.mock; |
| 35 | |
| 36 | /** |
| 37 | * Common base class for window manager unit test classes. |
| 38 | */ |
| 39 | public class WindowTestsBase { |
| 40 | static WindowManagerService sWm = null; |
| 41 | private final IWindow mIWindow = new TestIWindow(); |
| 42 | private final Session mMockSession = mock(Session.class); |
| 43 | Display mDisplay; |
| 44 | private static int sNextStackId = FIRST_DYNAMIC_STACK_ID; |
| 45 | private static int sNextTaskId = 0; |
| 46 | |
| 47 | @Before |
| 48 | public void setUp() throws Exception { |
| 49 | final Context context = InstrumentationRegistry.getTargetContext(); |
| 50 | sWm = TestWindowManagerPolicy.getWindowManagerService(context); |
| 51 | mDisplay = context.getDisplay(); |
| 52 | } |
| 53 | |
| 54 | /** Asserts that the first entry is greater than the second entry. */ |
| 55 | void assertGreaterThan(int first, int second) throws Exception { |
| 56 | Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second); |
| 57 | } |
| 58 | |
| 59 | WindowToken createWindowToken(DisplayContent dc, int type) { |
| 60 | if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) { |
| 61 | return new WindowToken(sWm, mock(IBinder.class), type, false, dc); |
| 62 | } |
| 63 | |
| 64 | final int stackId = sNextStackId++; |
| 65 | dc.addStackToDisplay(stackId, true); |
| 66 | final TaskStack stack = sWm.mStackIdToStack.get(stackId); |
| 67 | final Task task = new Task(sNextTaskId++, stack, 0, sWm, null, EMPTY, false); |
| 68 | stack.addTask(task, true); |
| 69 | final AppWindowToken token = new AppWindowToken(sWm, null, false, dc); |
| 70 | task.addAppToken(0, token, 0, false); |
| 71 | return token; |
| 72 | } |
| 73 | |
| 74 | WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) { |
| 75 | final WindowToken token = createWindowToken(dc, type); |
| 76 | return createWindow(parent, type, token, name); |
| 77 | } |
| 78 | |
| 79 | WindowState createWindow(WindowState parent, int type, WindowToken token, String name) { |
| 80 | final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type); |
| 81 | attrs.setTitle(name); |
| 82 | |
| 83 | final WindowState w = new WindowState(sWm, mMockSession, mIWindow, token, parent, OP_NONE, |
| 84 | 0, attrs, 0, 0); |
| 85 | // TODO: Probably better to make this call in the WindowState ctor to avoid errors with |
| 86 | // adding it to the token... |
| 87 | token.addWindow(w); |
| 88 | return w; |
| 89 | } |
| 90 | } |