[go: nahoru, domu]

blob: 8a8ff4fb22a3490f1e1beb38dcf325df64eb5327 [file] [log] [blame]
Ralston Da Silva14373642020-06-02 12:08:49 -07001/*
2 * Copyright 2020 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
Louis Pullen-Freilicha11fd942020-07-24 19:31:19 +010017package androidx.compose.ui.focus
Ralston Da Silva14373642020-06-02 12:08:49 -070018
Andrey Kulikov38320372020-05-07 19:55:05 +010019import android.view.View
Mihai Popa0189e8a2020-09-16 13:23:09 +010020import androidx.compose.foundation.layout.Box
Louis Pullen-Freilicha03fd6c2020-07-24 23:26:29 +010021import androidx.compose.ui.Modifier
Ralston Da Silvab497e222020-08-12 13:35:42 -070022import androidx.compose.ui.focus.FocusState.Active
23import androidx.compose.ui.focus.FocusState.Inactive
Louis Pullen-Freilich209df682020-11-11 00:13:18 +000024import androidx.compose.ui.platform.AmbientView
Ralston Da Silva262f1d62021-01-06 03:09:02 -080025import androidx.compose.ui.platform.testTag
Filip Pavlisce1489432020-10-29 12:18:06 +000026import androidx.compose.ui.test.junit4.createComposeRule
Ralston Da Silva262f1d62021-01-06 03:09:02 -080027import androidx.compose.ui.test.onNodeWithTag
28import androidx.compose.ui.test.performClick
Jelle Fresen53dd7b72020-09-25 10:02:27 +010029import androidx.test.ext.junit.runners.AndroidJUnit4
30import androidx.test.filters.MediumTest
Ralston Da Silva14373642020-06-02 12:08:49 -070031import com.google.common.truth.Truth.assertThat
32import org.junit.Ignore
33import org.junit.Rule
34import org.junit.Test
35import org.junit.runner.RunWith
Ralston Da Silva14373642020-06-02 12:08:49 -070036
Jelle Fresen53dd7b72020-09-25 10:02:27 +010037@MediumTest
Jelle Fresen17628d72020-09-24 16:23:51 +010038@RunWith(AndroidJUnit4::class)
Ralston Da Silva14373642020-06-02 12:08:49 -070039class OwnerFocusTest {
40 @get:Rule
Filip Pavlis375534f2020-09-03 16:59:33 +010041 val rule = createComposeRule()
Ralston Da Silva14373642020-06-02 12:08:49 -070042
43 @Test
44 fun requestFocus_bringsViewInFocus() {
45 // Arrange.
Andrey Kulikov38320372020-05-07 19:55:05 +010046 lateinit var ownerView: View
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -080047 val focusRequester = FocusRequester()
Filip Pavlis375534f2020-09-03 16:59:33 +010048 rule.setFocusableContent {
Ralston Da Silvabdaaf172021-01-11 14:23:39 -080049 ownerView = AmbientView.current
Ralston Da Silvaad70d262020-07-24 12:40:16 -070050 Box(
51 modifier = Modifier
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -080052 .focusRequester(focusRequester)
Ralston Da Silva24cb7a02020-12-08 17:08:54 -080053 .focusModifier()
Ralston Da Silvaad70d262020-07-24 12:40:16 -070054 )
Ralston Da Silva14373642020-06-02 12:08:49 -070055 }
56
57 // Act.
Filip Pavlis375534f2020-09-03 16:59:33 +010058 rule.runOnIdle {
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -080059 focusRequester.requestFocus()
Ralston Da Silva14373642020-06-02 12:08:49 -070060 }
61
62 // Assert.
Filip Pavlis375534f2020-09-03 16:59:33 +010063 rule.runOnIdle {
Ralston Da Silva14373642020-06-02 12:08:49 -070064 assertThat(ownerView.isFocused).isTrue()
65 }
66 }
67
68 @Ignore("Enable this test after the owner propagates focus to the hierarchy (b/152535715)")
69 @Test
70 fun whenOwnerGainsFocus_focusModifiersAreUpdated() {
71 // Arrange.
Andrey Kulikov38320372020-05-07 19:55:05 +010072 lateinit var ownerView: View
Ralston Da Silvaad70d262020-07-24 12:40:16 -070073 var focusState = Inactive
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -080074 val focusRequester = FocusRequester()
Filip Pavlis375534f2020-09-03 16:59:33 +010075 rule.setFocusableContent {
Ralston Da Silvabdaaf172021-01-11 14:23:39 -080076 ownerView = AmbientView.current
Ralston Da Silvaad70d262020-07-24 12:40:16 -070077 Box(
78 modifier = Modifier
Ralston Da Silvaa19b9ec2020-12-03 16:09:45 -080079 .onFocusChanged { focusState = it }
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -080080 .focusRequester(focusRequester)
Ralston Da Silva24cb7a02020-12-08 17:08:54 -080081 .focusModifier()
Ralston Da Silvaad70d262020-07-24 12:40:16 -070082 )
Ralston Da Silva14373642020-06-02 12:08:49 -070083 }
84
85 // Act.
Filip Pavlis375534f2020-09-03 16:59:33 +010086 rule.runOnIdle {
Ralston Da Silva14373642020-06-02 12:08:49 -070087 ownerView.requestFocus()
88 }
89
90 // Assert.
Filip Pavlis375534f2020-09-03 16:59:33 +010091 rule.runOnIdle {
Ralston Da Silvaad70d262020-07-24 12:40:16 -070092 assertThat(focusState).isEqualTo(Active)
Ralston Da Silva14373642020-06-02 12:08:49 -070093 }
94 }
95
96 @Ignore("Enable this test after the owner propagates focus to the hierarchy (b/152535715)")
97 @Test
98 fun whenWindowGainsFocus_focusModifiersAreUpdated() {
99 // Arrange.
Andrey Kulikov38320372020-05-07 19:55:05 +0100100 lateinit var ownerView: View
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700101 var focusState = Inactive
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -0800102 val focusRequester = FocusRequester()
Filip Pavlis375534f2020-09-03 16:59:33 +0100103 rule.setFocusableContent {
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800104 ownerView = AmbientView.current
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700105 Box(
106 modifier = Modifier
Ralston Da Silvaa19b9ec2020-12-03 16:09:45 -0800107 .onFocusChanged { focusState = it }
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -0800108 .focusRequester(focusRequester)
Ralston Da Silva24cb7a02020-12-08 17:08:54 -0800109 .focusModifier()
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700110 )
Ralston Da Silva14373642020-06-02 12:08:49 -0700111 }
112
113 // Act.
Filip Pavlis375534f2020-09-03 16:59:33 +0100114 rule.runOnIdle {
Ralston Da Silva14373642020-06-02 12:08:49 -0700115 ownerView.dispatchWindowFocusChanged(true)
116 }
117
118 // Assert.
Filip Pavlis375534f2020-09-03 16:59:33 +0100119 rule.runOnIdle {
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700120 assertThat(focusState).isEqualTo(Active)
Ralston Da Silva14373642020-06-02 12:08:49 -0700121 }
122 }
123
124 @Test
125 fun whenOwnerLosesFocus_focusModifiersAreUpdated() {
126 // Arrange.
Andrey Kulikov38320372020-05-07 19:55:05 +0100127 lateinit var ownerView: View
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700128 var focusState = Inactive
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -0800129 val focusRequester = FocusRequester()
Filip Pavlis375534f2020-09-03 16:59:33 +0100130 rule.setFocusableContent {
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800131 ownerView = AmbientView.current
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700132 Box(
133 modifier = Modifier
Ralston Da Silvaa19b9ec2020-12-03 16:09:45 -0800134 .onFocusChanged { focusState = it }
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -0800135 .focusRequester(focusRequester)
Ralston Da Silva24cb7a02020-12-08 17:08:54 -0800136 .focusModifier()
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700137 )
Ralston Da Silva14373642020-06-02 12:08:49 -0700138 }
Filip Pavlis375534f2020-09-03 16:59:33 +0100139 rule.runOnIdle {
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -0800140 focusRequester.requestFocus()
Ralston Da Silva14373642020-06-02 12:08:49 -0700141 }
142
143 // Act.
Filip Pavlis375534f2020-09-03 16:59:33 +0100144 rule.runOnIdle {
Ralston Da Silva14373642020-06-02 12:08:49 -0700145 ownerView.clearFocus()
146 }
147
148 // Assert.
Filip Pavlis375534f2020-09-03 16:59:33 +0100149 rule.runOnIdle {
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700150 assertThat(focusState).isEqualTo(Inactive)
Ralston Da Silva14373642020-06-02 12:08:49 -0700151 }
152 }
153
154 @Test
155 fun whenWindowLosesFocus_focusStateIsUnchanged() {
156 // Arrange.
Andrey Kulikov38320372020-05-07 19:55:05 +0100157 lateinit var ownerView: View
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700158 var focusState = Inactive
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -0800159 val focusRequester = FocusRequester()
Filip Pavlis375534f2020-09-03 16:59:33 +0100160 rule.setFocusableContent {
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800161 ownerView = AmbientView.current
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700162 Box(
163 modifier = Modifier
Ralston Da Silvaa19b9ec2020-12-03 16:09:45 -0800164 .onFocusChanged { focusState = it }
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -0800165 .focusRequester(focusRequester)
Ralston Da Silva24cb7a02020-12-08 17:08:54 -0800166 .focusModifier()
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700167 )
Ralston Da Silva14373642020-06-02 12:08:49 -0700168 }
Filip Pavlis375534f2020-09-03 16:59:33 +0100169 rule.runOnIdle {
Ralston Da Silva71a5eaf2020-12-15 18:01:50 -0800170 focusRequester.requestFocus()
Ralston Da Silva14373642020-06-02 12:08:49 -0700171 }
172
173 // Act.
Filip Pavlis375534f2020-09-03 16:59:33 +0100174 rule.runOnIdle {
Ralston Da Silva14373642020-06-02 12:08:49 -0700175 ownerView.dispatchWindowFocusChanged(false)
176 }
177
178 // Assert.
Filip Pavlis375534f2020-09-03 16:59:33 +0100179 rule.runOnIdle {
Ralston Da Silvaad70d262020-07-24 12:40:16 -0700180 assertThat(focusState).isEqualTo(Active)
Ralston Da Silva14373642020-06-02 12:08:49 -0700181 }
182 }
183
Ralston Da Silva262f1d62021-01-06 03:09:02 -0800184 @Test
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800185 fun clickingOnNonClickableSpaceInAppWhenViewIsFocused_doesNotChangeViewFocus() {
Ralston Da Silva262f1d62021-01-06 03:09:02 -0800186 // Arrange.
187 val nonClickable = "notClickable"
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800188 var didViewFocusChange = false
Ralston Da Silva262f1d62021-01-06 03:09:02 -0800189 lateinit var ownerView: View
190 rule.setFocusableContent {
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800191 ownerView = AmbientView.current
Ralston Da Silva262f1d62021-01-06 03:09:02 -0800192 Box(Modifier.testTag(nonClickable))
193 }
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800194 rule.runOnIdle {
195 ownerView.requestFocus()
196 assertThat(ownerView.isFocused).isTrue()
197 }
Ralston Da Silva262f1d62021-01-06 03:09:02 -0800198 ownerView.setOnFocusChangeListener { _, hasFocus ->
199 if (hasFocus) {
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800200 didViewFocusChange = true
Ralston Da Silva262f1d62021-01-06 03:09:02 -0800201 }
202 }
203
204 // Act.
205 rule.onNodeWithTag(nonClickable).performClick()
206
207 // Assert.
208 rule.runOnIdle {
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800209 assertThat(didViewFocusChange).isFalse()
Ralston Da Silva262f1d62021-01-06 03:09:02 -0800210 assertThat(ownerView.isFocused).isTrue()
211 }
212 }
213
Ralston Da Silvabdaaf172021-01-11 14:23:39 -0800214 @Test
215 fun clickingOnNonClickableSpaceInAppWhenViewIsNotFocused_doesNotChangeViewFocus() {
216 // Arrange.
217 val nonClickable = "notClickable"
218 var didViewFocusChange = false
219 lateinit var ownerView: View
220 rule.setFocusableContent {
221 ownerView = AmbientView.current
222 Box(Modifier.testTag(nonClickable))
223 }
224 rule.runOnIdle { assertThat(ownerView.isFocused).isFalse() }
225 ownerView.setOnFocusChangeListener { _, hasFocus ->
226 if (hasFocus) {
227 didViewFocusChange = true
228 }
229 }
230
231 // Act.
232 rule.onNodeWithTag(nonClickable).performClick()
233
234 // Assert.
235 rule.runOnIdle {
236 assertThat(didViewFocusChange).isFalse()
237 assertThat(ownerView.isFocused).isFalse()
238 }
239 }
Ralston Da Silva14373642020-06-02 12:08:49 -0700240}