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 package com.android.systemui.shade
17 
18 import android.testing.AndroidTestingRunner
19 import androidx.constraintlayout.widget.ConstraintSet
20 import androidx.test.filters.SmallTest
21 import com.android.systemui.SysuiTestCase
22 import com.android.systemui.util.mockito.mock
23 import com.google.common.truth.Truth.assertThat
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.mockito.Mockito.inOrder
27 import org.mockito.Mockito.verify
28 
29 @SmallTest
30 @RunWith(AndroidTestingRunner::class)
31 class ConstraintChangeTest : SysuiTestCase() {
32 
33     @Test
34     fun testSumNonNull() {
35         val mock1: ConstraintChange = mock()
36         val mock2: ConstraintChange = mock()
37 
38         val constraintSet = ConstraintSet()
39 
40         val sum = mock1 + mock2
41         sum?.invoke(constraintSet)
42 
43         val inOrder = inOrder(mock1, mock2)
44         inOrder.verify(mock1).invoke(constraintSet)
45         inOrder.verify(mock2).invoke(constraintSet)
46     }
47 
48     @Test
49     fun testSumThisNull() {
50         val mock: ConstraintChange = mock()
51         val constraintSet = ConstraintSet()
52 
53         val sum = (null as? ConstraintChange?) + mock
54         sum?.invoke(constraintSet)
55 
56         verify(mock).invoke(constraintSet)
57     }
58 
59     @Test
60     fun testSumThisNull_notWrapped() {
61         val change: ConstraintChange = {}
62 
63         val sum = (null as? ConstraintChange?) + change
64         assertThat(sum).isSameInstanceAs(change)
65     }
66 
67     @Test
68     fun testSumOtherNull() {
69         val mock: ConstraintChange = mock()
70         val constraintSet = ConstraintSet()
71 
72         val sum = mock + (null as? ConstraintChange?)
73         sum?.invoke(constraintSet)
74 
75         verify(mock).invoke(constraintSet)
76     }
77 
78     @Test
79     fun testSumOtherNull_notWrapped() {
80         val change: ConstraintChange = {}
81 
82         val sum = change + (null as? ConstraintChange?)
83         assertThat(sum).isSameInstanceAs(change)
84     }
85 }
86