1 /*
2  * 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.systemui.communal.shared.model
18 
19 import android.appwidget.AppWidgetProviderInfo
20 import android.content.ComponentName
21 import android.os.Parcel
22 import android.os.UserHandle
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.filters.SmallTest
25 import com.android.systemui.SysuiTestCase
26 import com.google.common.truth.Truth.assertThat
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 
30 @SmallTest
31 @RunWith(AndroidJUnit4::class)
32 class CommunalWidgetContentModelTest : SysuiTestCase() {
33     @Test
testParcelizeAvailableWidgetnull34     fun testParcelizeAvailableWidget() {
35         val widgetToParcelize =
36             CommunalWidgetContentModel.Available(
37                 appWidgetId = 1,
38                 providerInfo =
39                     AppWidgetProviderInfo().apply { provider = ComponentName("pkg", "cls") },
40                 rank = 2,
41                 spanY = 3,
42             )
43 
44         val parcel = Parcel.obtain()
45         widgetToParcelize.writeToParcel(parcel, flags = 0)
46 
47         parcel.setDataPosition(0)
48 
49         // Only checking fields are equal and not complete equality because not all fields are
50         // specified in the fake AppWidgetProviderInfo
51         val widgetFromParcel =
52             CommunalWidgetContentModel.createFromParcel(parcel)
53                 as CommunalWidgetContentModel.Available
54         assertThat(widgetFromParcel.appWidgetId).isEqualTo(widgetToParcelize.appWidgetId)
55         assertThat(widgetFromParcel.rank).isEqualTo(widgetToParcelize.rank)
56         assertThat(widgetFromParcel.spanY).isEqualTo(widgetToParcelize.spanY)
57         assertThat(widgetFromParcel.providerInfo.provider)
58             .isEqualTo(widgetToParcelize.providerInfo.provider)
59     }
60 
61     @Test
testParcelizePendingWidgetnull62     fun testParcelizePendingWidget() {
63         val widgetToParcelize =
64             CommunalWidgetContentModel.Pending(
65                 appWidgetId = 2,
66                 rank = 3,
67                 componentName = ComponentName("pkg", "cls"),
68                 icon = null,
69                 user = UserHandle(0),
70                 spanY = 6,
71             )
72 
73         val parcel = Parcel.obtain()
74         widgetToParcelize.writeToParcel(parcel, flags = 0)
75 
76         parcel.setDataPosition(0)
77 
78         val widgetFromParcel = CommunalWidgetContentModel.createFromParcel(parcel)
79         assertThat(widgetFromParcel).isEqualTo(widgetToParcelize)
80     }
81 }
82