1 /* <lambda>null2 * Copyright (C) 2023 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.app.viewcapture 18 19 import android.Manifest 20 import android.content.Context 21 import android.content.Intent 22 import android.media.permission.SafeCloseable 23 import android.provider.Settings 24 import android.testing.AndroidTestingRunner 25 import android.view.Choreographer 26 import android.view.View 27 import androidx.test.ext.junit.rules.ActivityScenarioRule 28 import androidx.test.filters.SmallTest 29 import androidx.test.platform.app.InstrumentationRegistry 30 import androidx.test.rule.GrantPermissionRule 31 import com.android.app.viewcapture.SettingsAwareViewCapture.Companion.VIEW_CAPTURE_ENABLED 32 import com.android.app.viewcapture.ViewCapture.MAIN_EXECUTOR 33 import junit.framework.Assert.assertEquals 34 import org.junit.Rule 35 import org.junit.Test 36 import org.junit.runner.RunWith 37 38 @SmallTest 39 @RunWith(AndroidTestingRunner::class) 40 class SettingsAwareViewCaptureTest { 41 private val context: Context = InstrumentationRegistry.getInstrumentation().context 42 private val activityIntent = Intent(context, TestActivity::class.java) 43 44 @get:Rule val activityScenarioRule = ActivityScenarioRule<TestActivity>(activityIntent) 45 @get:Rule val grantPermissionRule = 46 GrantPermissionRule.grant(Manifest.permission.WRITE_SECURE_SETTINGS) 47 48 @Test 49 fun do_not_capture_view_hierarchies_if_setting_is_disabled() { 50 Settings.Global.putInt(context.contentResolver, VIEW_CAPTURE_ENABLED, 0) 51 52 activityScenarioRule.scenario.onActivity { activity -> 53 val viewCapture: ViewCapture = SettingsAwareViewCapture(context, MAIN_EXECUTOR) 54 val rootView: View = activity.requireViewById(android.R.id.content) 55 56 val closeable: SafeCloseable = viewCapture.startCapture(rootView, "rootViewId") 57 Choreographer.getInstance().postFrameCallback { 58 rootView.viewTreeObserver.dispatchOnDraw() 59 60 assertEquals( 61 0, 62 viewCapture 63 .getDumpTask(activity.requireViewById(android.R.id.content)) 64 .get() 65 .get() 66 .frameDataList 67 .size 68 ) 69 closeable.close() 70 } 71 } 72 } 73 74 @Test 75 fun capture_view_hierarchies_if_setting_is_enabled() { 76 Settings.Global.putInt(context.contentResolver, VIEW_CAPTURE_ENABLED, 1) 77 78 activityScenarioRule.scenario.onActivity { activity -> 79 val viewCapture: ViewCapture = SettingsAwareViewCapture(context, MAIN_EXECUTOR) 80 val rootView: View = activity.requireViewById(android.R.id.content) 81 82 val closeable: SafeCloseable = viewCapture.startCapture(rootView, "rootViewId") 83 Choreographer.getInstance().postFrameCallback { 84 rootView.viewTreeObserver.dispatchOnDraw() 85 86 assertEquals( 87 1, 88 viewCapture 89 .getDumpTask(activity.requireViewById(android.R.id.content)) 90 .get() 91 .get() 92 .frameDataList 93 .size 94 ) 95 96 closeable.close() 97 } 98 } 99 } 100 } 101