1 /* 2 * Copyright (C) 2024 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 package android.appwidget.cts 17 18 import android.cts.statsdatom.lib.AtomTestUtils 19 import android.cts.statsdatom.lib.ConfigUtils 20 import android.cts.statsdatom.lib.DeviceUtils 21 import android.cts.statsdatom.lib.ReportUtils 22 import com.android.os.framework.FrameworkExtensionAtoms 23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner 24 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test 25 import com.android.tradefed.util.RunUtil 26 import com.google.common.truth.Truth.assertThat 27 import com.google.protobuf.ExtensionRegistry 28 import org.junit.After 29 import org.junit.Before 30 import org.junit.Test 31 import org.junit.runner.RunWith 32 33 @RunWith(DeviceJUnit4ClassRunner::class) 34 class StatsTest : BaseHostJUnit4Test() { 35 private companion object { 36 private const val PACKAGE = "android.appwidget.cts.app" 37 private const val TEST_CLASS = "android.appwidget.cts.app.StatsDeviceTest" 38 private const val BIND_WIDGET = "bindWidget" 39 } 40 41 var user = 0 42 43 @Before beforenull44 fun before() { 45 // Clear package and start user 46 val pmResult = device.executeShellV2Command("pm clear $PACKAGE") 47 assertThat(pmResult.exitCode).isEqualTo(0) 48 user = device.mainUserId ?: device.currentUser 49 device.startUser(user, true) 50 51 // Clear statsd configs uploaded by other tests 52 ConfigUtils.removeConfig(device) 53 ReportUtils.clearReports(device) 54 } 55 56 @After afternull57 fun after() { 58 val pmResult = device.executeShellV2Command("pm clear $PACKAGE") 59 assertThat(pmResult.exitCode).isEqualTo(0) 60 ConfigUtils.removeConfig(device) 61 ReportUtils.clearReports(device) 62 } 63 64 @Test testWidgetMemoryStatsnull65 fun testWidgetMemoryStats() { 66 // Create statsd config and upload it to the device 67 ConfigUtils.uploadConfigForPulledAtomWithUid( 68 device, 69 PACKAGE, 70 FrameworkExtensionAtoms.WIDGET_MEMORY_STATS_FIELD_NUMBER, 71 /* useUidAttributionChain = */ 72 false, 73 ) 74 75 // Bind widget 76 val bindResult = runDeviceTests(PACKAGE, TEST_CLASS, BIND_WIDGET) 77 assertThat(bindResult).isTrue() 78 79 // Trigger atom pull 80 AtomTestUtils.sendAppBreadcrumbReportedAtom(device) 81 RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_LONG.toLong()) 82 83 // Get atoms 84 val extensionRegistry = ExtensionRegistry.newInstance() 85 FrameworkExtensionAtoms.registerAllExtensions(extensionRegistry) 86 val events = ReportUtils.getGaugeMetricAtoms( 87 device, 88 extensionRegistry, 89 /* checkTimestampTruncated= */ 90 false, 91 ) 92 93 // Verify widget memory stat was logged 94 val stats = events.single().getExtension(FrameworkExtensionAtoms.widgetMemoryStats) 95 assertThat(stats.uid) 96 .isEqualTo(DeviceUtils.getAppUidForUser(device, PACKAGE, user)) 97 // Bitmap is 640x480 with ARGB_8888 config 98 assertThat(stats.bitmapMemoryBytes).isEqualTo(640 * 480 * 4) 99 } 100 } 101