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 com.android.intentresolver.platform
18 
19 import com.google.common.truth.Truth.assertThat
20 import org.junit.Test
21 
22 class FakeSettingsTest {
23 
<lambda>null24     private val settings: FakeSettings = fakeSettings {
25         putInt(intKey, intVal)
26         putString(stringKey, stringVal)
27         putFloat(floatKey, floatVal)
28         putLong(longKey, longVal)
29     }
30 
31     @Test
testExpectedValues_returnednull32     fun testExpectedValues_returned() {
33         assertThat(settings.getIntOrNull(intKey)).isEqualTo(intVal)
34         assertThat(settings.getStringOrNull(stringKey)).isEqualTo(stringVal)
35         assertThat(settings.getFloatOrNull(floatKey)).isEqualTo(floatVal)
36         assertThat(settings.getLongOrNull(longKey)).isEqualTo(longVal)
37     }
38 
39     @Test
testUndefinedValues_returnNullnull40     fun testUndefinedValues_returnNull() {
41         assertThat(settings.getIntOrNull("unknown")).isNull()
42         assertThat(settings.getStringOrNull("unknown")).isNull()
43         assertThat(settings.getFloatOrNull("unknown")).isNull()
44         assertThat(settings.getLongOrNull("unknown")).isNull()
45     }
46 
47     /**
48      * FakeSecureSettings models the real secure settings by storing values in String form. The
49      * value is returned if/when it can be parsed from the string value, otherwise null.
50      */
51     @Test
testMismatchedTypesnull52     fun testMismatchedTypes() {
53         assertThat(settings.getStringOrNull(intKey)).isEqualTo(intVal.toString())
54         assertThat(settings.getStringOrNull(floatKey)).isEqualTo(floatVal.toString())
55         assertThat(settings.getStringOrNull(longKey)).isEqualTo(longVal.toString())
56 
57         assertThat(settings.getIntOrNull(stringKey)).isNull()
58         assertThat(settings.getLongOrNull(stringKey)).isNull()
59         assertThat(settings.getFloatOrNull(stringKey)).isNull()
60 
61         assertThat(settings.getIntOrNull(longKey)).isNull()
62         assertThat(settings.getFloatOrNull(longKey)).isWithin(0.00001f).of(Long.MAX_VALUE.toFloat())
63 
64         assertThat(settings.getLongOrNull(floatKey)).isNull()
65         assertThat(settings.getIntOrNull(floatKey)).isNull()
66     }
67 
68     companion object Data {
69         const val intKey = "int"
70         const val intVal = Int.MAX_VALUE
71 
72         const val stringKey = "string"
73         const val stringVal = "String"
74 
75         const val floatKey = "float"
76         const val floatVal = Float.MAX_VALUE
77 
78         const val longKey = "long"
79         const val longVal = Long.MAX_VALUE
80     }
81 }
82