Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 15 | package android.testing; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 16 | |
Jason Monk | 9ec0f1e | 2017-02-23 11:33:54 -0500 | [diff] [blame] | 17 | import android.content.pm.ApplicationInfo; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 18 | import android.graphics.PixelFormat; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 19 | import android.view.View; |
| 20 | import android.view.WindowManager; |
| 21 | import android.view.WindowManager.LayoutParams; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 22 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 23 | /** |
| 24 | * Utilities to make testing views easier. |
| 25 | */ |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 26 | public class ViewUtils { |
| 27 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 28 | /** |
| 29 | * Causes the view (and its children) to have {@link View#onAttachedToWindow()} called. |
| 30 | * |
| 31 | * This is currently done by adding the view to a window. |
| 32 | */ |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 33 | public static void attachView(View view) { |
Jason Monk | 9ec0f1e | 2017-02-23 11:33:54 -0500 | [diff] [blame] | 34 | // Make sure hardware acceleration isn't turned on. |
| 35 | view.getContext().getApplicationInfo().flags &= |
| 36 | ~(ApplicationInfo.FLAG_HARDWARE_ACCELERATED); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 37 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 38 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, |
Jason Monk | 7d6fa83 | 2017-02-03 15:16:21 -0500 | [diff] [blame] | 39 | LayoutParams.TYPE_APPLICATION_OVERLAY, |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 40 | 0, PixelFormat.TRANSLUCENT); |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 41 | view.getContext().getSystemService(WindowManager.class).addView(view, lp); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 44 | /** |
| 45 | * Causes the view (and its children) to have {@link View#onDetachedFromWindow()} called. |
| 46 | * |
| 47 | * This is currently done by removing the view from a window. |
| 48 | */ |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 49 | public static void detachView(View view) { |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 50 | view.getContext().getSystemService(WindowManager.class).removeViewImmediate(view); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 51 | } |
| 52 | } |