blob: 80c2e8ddd907cbaf83ecf95e70f2ac44e09ea4ff [file] [log] [blame]
Jason Monkde850bb2017-02-01 19:26:30 -05001/*
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 Monk340b0e52017-03-08 14:57:56 -050015package android.testing;
Jason Monkde850bb2017-02-01 19:26:30 -050016
Jason Monk9ec0f1e2017-02-23 11:33:54 -050017import android.content.pm.ApplicationInfo;
Jason Monkde850bb2017-02-01 19:26:30 -050018import android.graphics.PixelFormat;
Jason Monkde850bb2017-02-01 19:26:30 -050019import android.view.View;
20import android.view.WindowManager;
21import android.view.WindowManager.LayoutParams;
Jason Monkde850bb2017-02-01 19:26:30 -050022
Jason Monk0c408002017-05-03 15:43:52 -040023/**
24 * Utilities to make testing views easier.
25 */
Jason Monkde850bb2017-02-01 19:26:30 -050026public class ViewUtils {
27
Jason Monk0c408002017-05-03 15:43:52 -040028 /**
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 Monkde850bb2017-02-01 19:26:30 -050033 public static void attachView(View view) {
Jason Monk9ec0f1e2017-02-23 11:33:54 -050034 // Make sure hardware acceleration isn't turned on.
35 view.getContext().getApplicationInfo().flags &=
36 ~(ApplicationInfo.FLAG_HARDWARE_ACCELERATED);
Jason Monkde850bb2017-02-01 19:26:30 -050037 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
38 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
Jason Monk7d6fa832017-02-03 15:16:21 -050039 LayoutParams.TYPE_APPLICATION_OVERLAY,
Jason Monkde850bb2017-02-01 19:26:30 -050040 0, PixelFormat.TRANSLUCENT);
Jason Monk893f0bd2017-06-01 11:21:14 -040041 view.getContext().getSystemService(WindowManager.class).addView(view, lp);
Jason Monkde850bb2017-02-01 19:26:30 -050042 }
43
Jason Monk0c408002017-05-03 15:43:52 -040044 /**
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 Monkde850bb2017-02-01 19:26:30 -050049 public static void detachView(View view) {
Jason Monk893f0bd2017-06-01 11:21:14 -040050 view.getContext().getSystemService(WindowManager.class).removeViewImmediate(view);
Jason Monkde850bb2017-02-01 19:26:30 -050051 }
52}