1 /*
2  * Copyright (C) 2022 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.launcher3.util
18 
19 import android.view.View
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import com.google.common.truth.Truth.assertThat
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 
26 /** Unit tests for [MultiScalePropertyFactory] */
27 @SmallTest
28 @RunWith(AndroidJUnit4::class)
29 class MultiScalePropertyTest {
30 
31     private val received = mutableListOf<Float>()
32 
33     private val factory =
34         object : MultiScalePropertyFactory<View?>("Test") {
applynull35             override fun apply(obj: View?, value: Float) {
36                 received.add(value)
37             }
38         }
39 
40     private val p1 = factory.get(1)
41     private val p2 = factory.get(2)
42     private val p3 = factory.get(3)
43 
44     @Test
set_multipleSame_bothApplieddnull45     fun set_multipleSame_bothAppliedd() {
46         p1.set(null, 0.5f)
47         p1.set(null, 0.5f)
48 
49         assertThat(received).containsExactly(0.5f, 0.5f)
50     }
51 
52     @Test
set_differentIndexes_oneValuesNotCountednull53     fun set_differentIndexes_oneValuesNotCounted() {
54         val v1 = 0.5f
55         val v2 = 1.0f
56         p1.set(null, v1)
57         p2.set(null, v2)
58 
59         assertThat(received).containsExactly(v1, v1)
60     }
61 
62     @Test
set_onlyOneSetToOne_oneAppliednull63     fun set_onlyOneSetToOne_oneApplied() {
64         p1.set(null, 1.0f)
65 
66         assertThat(received).containsExactly(1.0f)
67     }
68 
69     @Test
set_onlyOneLessThanOne_appliednull70     fun set_onlyOneLessThanOne_applied() {
71         p1.set(null, 0.5f)
72 
73         assertThat(received).containsExactly(0.5f)
74     }
75 
76     @Test
set_differentIndexes_boundToMinnull77     fun set_differentIndexes_boundToMin() {
78         val v1 = 0.5f
79         val v2 = 0.6f
80         p1.set(null, v1)
81         p2.set(null, v2)
82 
83         assertThat(received).containsExactly(v1, v1)
84     }
85 
86     @Test
set_allHigherThanOne_boundToMaxnull87     fun set_allHigherThanOne_boundToMax() {
88         val v1 = 3.0f
89         val v2 = 2.0f
90         val v3 = 1.0f
91         p1.set(null, v1)
92         p2.set(null, v2)
93         p3.set(null, v3)
94 
95         assertThat(received).containsExactly(v1, v1, v1)
96     }
97 
98     @Test
set_differentIndexes_firstModified_aggregationAppliednull99     fun set_differentIndexes_firstModified_aggregationApplied() {
100         val v1 = 0.5f
101         val v2 = 0.6f
102         val v3 = 4f
103         p1.set(null, v1)
104         p2.set(null, v2)
105         p3.set(null, v3)
106 
107         assertThat(received).containsExactly(v1, v1, v1 * v2 * v3)
108     }
109 }
110