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.service.settings.preferences
18 
19 import android.content.Intent
20 import android.os.Bundle
21 import android.os.Parcel
22 import android.platform.test.annotations.RequiresFlagsEnabled
23 import android.platform.test.flag.junit.CheckFlagsRule
24 import android.platform.test.flag.junit.DeviceFlagsValueProvider
25 import androidx.test.ext.junit.runners.AndroidJUnit4
26 import androidx.test.filters.SmallTest
27 import com.android.settingslib.flags.Flags.FLAG_SETTINGS_CATALYST
28 import com.google.common.truth.Truth.assertThat
29 import org.junit.Rule
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 @RunWith(AndroidJUnit4::class)
34 @SmallTest
35 @RequiresFlagsEnabled(FLAG_SETTINGS_CATALYST)
36 class SettingsPreferenceMetadataTest {
37 
38     @get:Rule
39     val checkFlagsRule: CheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()
40 
41     private val intent = Intent(Intent.ACTION_VIEW).setPackage("test")
42 
43     private val metadata: SettingsPreferenceMetadata
44         get() = SettingsPreferenceMetadata.Builder("screenKey", "key")
45             .setTitle("title")
46             .setSummary("summary")
47             .setBreadcrumbs(listOf("first", "second"))
48             .setReadPermissions(listOf("readPermission"))
49             .setWritePermissions(listOf("writePermission"))
50             .setEnabled(true)
51             .setAvailable(true)
52             .setWritable(true)
53             .setRestricted(true)
54             .setWriteSensitivity(SettingsPreferenceMetadata.EXPECT_POST_CONFIRMATION)
55             .setLaunchIntent(intent)
<lambda>null56             .setExtras(Bundle().apply { putString("bKey", "bValue") })
57             .build()
58 
59     @Test
buildMetadata_allFieldsSetnull60     fun buildMetadata_allFieldsSet() {
61         with(metadata) {
62             assertThat(key).isEqualTo("key")
63             assertThat(screenKey).isEqualTo("screenKey")
64             assertThat(title!!).isEqualTo("title")
65             assertThat(summary!!).isEqualTo("summary")
66             assertThat(breadcrumbs).isEqualTo(listOf("first", "second"))
67             assertThat(readPermissions).isEqualTo(listOf("readPermission"))
68             assertThat(writePermissions).isEqualTo(listOf("writePermission"))
69             assertThat(isEnabled).isTrue()
70             assertThat(isAvailable).isTrue()
71             assertThat(isWritable).isTrue()
72             assertThat(isRestricted).isTrue()
73             assertThat(writeSensitivity)
74                 .isEqualTo(SettingsPreferenceMetadata.EXPECT_POST_CONFIRMATION)
75             assertThat(launchIntent!!).isEqualTo(intent)
76             assertThat(extras.getString("bKey")!!).isEqualTo("bValue")
77         }
78     }
79 
80     @Test
buildMetadata_excludedSensitivity_nullLaunchIntentnull81     fun buildMetadata_excludedSensitivity_nullLaunchIntent() {
82         val md = SettingsPreferenceMetadata.Builder("screenKey", "key")
83             .setLaunchIntent(intent)
84             .setWriteSensitivity(SettingsPreferenceMetadata.NO_DIRECT_ACCESS)
85             .build()
86         assertThat(md.writeSensitivity).isEqualTo(SettingsPreferenceMetadata.NO_DIRECT_ACCESS)
87         assertThat(md.launchIntent).isNull()
88     }
89 
90     @Test
buildMetadata_fromParcelablenull91     fun buildMetadata_fromParcelable() {
92         val parcel = Parcel.obtain()
93         metadata.writeToParcel(parcel, 0)
94         parcel.setDataPosition(0)
95         val new = SettingsPreferenceMetadata.CREATOR.createFromParcel(parcel)
96 
97         with(new) {
98             assertThat(key).isEqualTo("key")
99             assertThat(screenKey).isEqualTo("screenKey")
100             assertThat(title!!).isEqualTo("title")
101             assertThat(summary!!).isEqualTo("summary")
102             assertThat(breadcrumbs).isEqualTo(listOf("first", "second"))
103             assertThat(readPermissions).isEqualTo(listOf("readPermission"))
104             assertThat(writePermissions).isEqualTo(listOf("writePermission"))
105             assertThat(isEnabled).isTrue()
106             assertThat(isAvailable).isTrue()
107             assertThat(isWritable).isTrue()
108             assertThat(isRestricted).isTrue()
109             assertThat(writeSensitivity)
110                 .isEqualTo(SettingsPreferenceMetadata.EXPECT_POST_CONFIRMATION)
111             assertThat(launchIntent!!.toUri(0)).isEqualTo(intent.toUri(0))
112             assertThat(extras.getString("bKey")!!).isEqualTo("bValue")
113         }
114     }
115 }
116