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.launcher3
18 
19 import android.content.Context
20 import android.widget.LinearLayout
21 import android.widget.TextView
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import androidx.test.platform.app.InstrumentationRegistry
25 import com.android.launcher3.UtilitiesKt.CLIP_CHILDREN_FALSE_MODIFIER
26 import com.android.launcher3.UtilitiesKt.CLIP_TO_PADDING_FALSE_MODIFIER
27 import com.android.launcher3.UtilitiesKt.modifyAttributesOnViewTree
28 import com.android.launcher3.UtilitiesKt.restoreAttributesOnViewTree
29 import com.google.common.truth.Truth.assertThat
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 @SmallTest
34 @RunWith(AndroidJUnit4::class)
35 class UtilitiesKtTest {
36     val context: Context = InstrumentationRegistry.getInstrumentation().context
37 
38     private val childView = TextView(context)
39 
<lambda>null40     private val midView = LinearLayout(context).apply { addView(childView) }
41 
<lambda>null42     private val rootView = LinearLayout(context).apply { addView(midView) }
43 
44     @Test
set_clipChildren_falsenull45     fun set_clipChildren_false() {
46         assertThat(rootView.clipChildren).isTrue()
47         assertThat(midView.clipChildren).isTrue()
48 
49         modifyAttributesOnViewTree(childView, rootView, CLIP_CHILDREN_FALSE_MODIFIER)
50 
51         assertThat(rootView.clipChildren).isFalse()
52         assertThat(midView.clipChildren).isFalse()
53     }
54 
55     @Test
restore_clipChildren_truenull56     fun restore_clipChildren_true() {
57         assertThat(rootView.clipChildren).isTrue()
58         assertThat(midView.clipChildren).isTrue()
59         modifyAttributesOnViewTree(childView, rootView, CLIP_CHILDREN_FALSE_MODIFIER)
60         assertThat(rootView.clipChildren).isFalse()
61         assertThat(midView.clipChildren).isFalse()
62 
63         restoreAttributesOnViewTree(childView, rootView, CLIP_CHILDREN_FALSE_MODIFIER)
64 
65         assertThat(rootView.clipChildren).isTrue()
66         assertThat(midView.clipChildren).isTrue()
67     }
68 
69     @Test
restore_clipChildren_skipRestoreMidViewnull70     fun restore_clipChildren_skipRestoreMidView() {
71         assertThat(rootView.clipChildren).isTrue()
72         assertThat(midView.clipChildren).isTrue()
73         rootView.clipChildren = false
74         modifyAttributesOnViewTree(childView, rootView, CLIP_CHILDREN_FALSE_MODIFIER)
75         assertThat(rootView.clipChildren).isFalse()
76         assertThat(midView.clipChildren).isFalse()
77 
78         restoreAttributesOnViewTree(childView, rootView, CLIP_CHILDREN_FALSE_MODIFIER)
79 
80         assertThat(rootView.clipChildren).isFalse()
81         assertThat(midView.clipChildren).isTrue()
82     }
83 
84     @Test
set_clipToPadding_falsenull85     fun set_clipToPadding_false() {
86         assertThat(rootView.clipToPadding).isTrue()
87         assertThat(midView.clipToPadding).isTrue()
88 
89         modifyAttributesOnViewTree(childView, rootView, CLIP_TO_PADDING_FALSE_MODIFIER)
90 
91         assertThat(rootView.clipToPadding).isFalse()
92         assertThat(midView.clipToPadding).isFalse()
93     }
94 
95     @Test
restore_clipToPadding_truenull96     fun restore_clipToPadding_true() {
97         assertThat(rootView.clipToPadding).isTrue()
98         assertThat(midView.clipToPadding).isTrue()
99         modifyAttributesOnViewTree(childView, rootView, CLIP_TO_PADDING_FALSE_MODIFIER)
100         assertThat(rootView.clipToPadding).isFalse()
101         assertThat(midView.clipToPadding).isFalse()
102 
103         restoreAttributesOnViewTree(childView, rootView, CLIP_TO_PADDING_FALSE_MODIFIER)
104 
105         assertThat(rootView.clipToPadding).isTrue()
106         assertThat(midView.clipToPadding).isTrue()
107     }
108 }
109