• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.widget.cts.util
18 
19 import android.appwidget.flags.Flags
20 import android.content.Context
21 import android.platform.test.flag.junit.DeviceFlagsValueProvider
22 import android.util.SizeF
23 import android.util.proto.ProtoInputStream
24 import android.util.proto.ProtoOutputStream
25 import android.view.View
26 import android.widget.RemoteViews
27 import androidx.test.rule.ActivityTestRule
28 import java.util.concurrent.atomic.AtomicReference
29 import org.junit.Assume
30 
31 object RemoteViewsUtil {
32 
33     @JvmStatic
recreateFromProtonull34     fun recreateFromProto(context: Context, views: RemoteViews): RemoteViews {
35         val output = ProtoOutputStream()
36         views.writePreviewToProto(context, output)
37         val input = ProtoInputStream(output.bytes)
38         return RemoteViews.createPreviewFromProto(context, input)
39     }
40 
41     @JvmOverloads
42     @JvmStatic
43     @Suppress("deprecation")
applyRemoteViewsnull44     fun applyRemoteViews(
45         activityRule: ActivityTestRule<*>,
46         context: Context,
47         remoteViews: RemoteViews,
48         isProtoTest: Boolean,
49         initialSize: SizeF? = null,
50         colorResources: RemoteViews.ColorResources? = null,
51     ): View {
52         val viewsToApply = if (isProtoTest) {
53             recreateFromProto(context, remoteViews)
54         } else {
55             remoteViews
56         }
57         val result = AtomicReference<View>()
58         activityRule.runOnUiThread {
59             result.set(
60                 viewsToApply.apply(context, null, null, initialSize, colorResources)
61             )
62         }
63         return result.get()
64     }
65 
66     @JvmOverloads
67     @JvmStatic
68     @Suppress("deprecation")
reapplyRemoteViewsnull69     fun reapplyRemoteViews(
70         activityRule: ActivityTestRule<*>,
71         context: Context,
72         remoteViews: RemoteViews,
73         root: View,
74         isProtoTest: Boolean,
75         initialSize: SizeF? = null,
76         async: Boolean = false,
77     ) {
78         val viewsToApply = if (isProtoTest) {
79             recreateFromProto(context, remoteViews)
80         } else {
81             remoteViews
82         }
83         activityRule.runOnUiThread {
84             if (async) {
85                 viewsToApply.reapplyAsync(context, root, null, null)
86             } else {
87                 viewsToApply.reapply( context, root, null, initialSize, null)
88             }
89         }
90     }
91 
92     /**
93      * Skips the proto test if the RemoteViews proto flag is not enabled.
94      */
95     @JvmStatic
checkRemoteViewsProtoFlagnull96     fun checkRemoteViewsProtoFlag(
97         isProtoTest: Boolean
98     ) {
99         if (isProtoTest) {
100             Assume.assumeTrue(
101                 String.format(
102                     "Flag %s must be enabled to run the proto test",
103                     Flags.FLAG_REMOTE_VIEWS_PROTO,
104                 ),
105                 DeviceFlagsValueProvider().getBoolean(Flags.FLAG_REMOTE_VIEWS_PROTO)
106             )
107         }
108     }
109 }
110