[go: nahoru, domu]

blob: 5baa9cec49f1c0b6a10d06c19ce0b523409df1b2 [file] [log] [blame]
Ralston Da Silva3e89a472019-07-31 01:21:51 -07001/*
2 * Copyright 2019 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
17package androidx.ui.autofill
18
Ralston Da Silva91f20612019-08-13 15:07:17 -070019import android.app.Activity
Ralston Da Silvaad9752a2020-06-22 17:55:50 -070020import android.graphics.Rect as AndroidRect
Ralston Da Silva3e89a472019-07-31 01:21:51 -070021import android.view.View
22import android.view.autofill.AutofillManager
23import androidx.test.filters.SmallTest
Ralston Da Silva91f20612019-08-13 15:07:17 -070024import androidx.ui.ComposeUiRobolectricTestRunner
Ralston Da Silvaad9752a2020-06-22 17:55:50 -070025import androidx.ui.core.toComposeRect
26import androidx.ui.geometry.Rect
Ralston Da Silva91f20612019-08-13 15:07:17 -070027import com.google.common.truth.Truth.assertThat
Ralston Da Silva3e89a472019-07-31 01:21:51 -070028import org.junit.Before
29import org.junit.Rule
30import org.junit.Test
31import org.junit.rules.ExpectedException
32import org.junit.runner.RunWith
Ralston Da Silva91f20612019-08-13 15:07:17 -070033import org.robolectric.Robolectric
34import org.robolectric.annotation.Config
35import org.robolectric.annotation.Implementation
36import org.robolectric.annotation.Implements
37import org.robolectric.shadow.api.Shadow
Ralston Da Silva3e89a472019-07-31 01:21:51 -070038
39@SmallTest
Ralston Da Silva91f20612019-08-13 15:07:17 -070040@RunWith(ComposeUiRobolectricTestRunner::class)
41@Config(
Ralston Da Silva91f20612019-08-13 15:07:17 -070042 shadows = [ShadowAutofillManager::class],
43 minSdk = 26
44)
Ralston Da Silva3e89a472019-07-31 01:21:51 -070045class AndroidAutofillTest {
46
47 @get:Rule
48 val expectedException = ExpectedException.none()!!
49
50 private lateinit var androidAutofill: AndroidAutofill
Ralston Da Silva91f20612019-08-13 15:07:17 -070051 private lateinit var autofillManager: ShadowAutofillManager
Ralston Da Silva3e89a472019-07-31 01:21:51 -070052 private lateinit var view: View
53 private val autofillTree = AutofillTree()
54
55 @Before
56 fun setup() {
Ralston Da Silva28c22892020-05-06 12:07:02 -070057 val activity = Robolectric.buildActivity(Activity::class.java).get()
Ralston Da Silva91f20612019-08-13 15:07:17 -070058 view = View(activity)
59 activity.setContentView(view)
Ralston Da Silva3e89a472019-07-31 01:21:51 -070060
Ralston Da Silva91f20612019-08-13 15:07:17 -070061 autofillManager = Shadow.extract<ShadowAutofillManager>(
62 activity.getSystemService(AutofillManager::class.java)
63 )
Ralston Da Silva3e89a472019-07-31 01:21:51 -070064
65 androidAutofill = AndroidAutofill(view, autofillTree)
66 }
67
68 @Test
69 fun importantForAutofill_is_yes() {
Ralston Da Silva91f20612019-08-13 15:07:17 -070070 assertThat(view.importantForAutofill).isEqualTo(View.IMPORTANT_FOR_AUTOFILL_YES)
Ralston Da Silva3e89a472019-07-31 01:21:51 -070071 }
72
Louis Pullen-Freilich689e9352020-06-18 20:40:43 +010073 @Test
Ralston Da Silva3e89a472019-07-31 01:21:51 -070074 fun requestAutofillForNode_calls_notifyViewEntered() {
75 // Arrange.
Nikolay Igotti2c24c562020-06-18 12:49:02 +030076 val bounds = Rect(0f, 0f, 0f, 0f)
Ralston Da Silva91f20612019-08-13 15:07:17 -070077 val autofillNode = AutofillNode(onFill = {}, boundingBox = bounds)
Ralston Da Silva3e89a472019-07-31 01:21:51 -070078
79 // Act.
80 androidAutofill.requestAutofillForNode(autofillNode)
81
82 // Assert.
Ralston Da Silva91f20612019-08-13 15:07:17 -070083 assertThat(autofillManager.viewEnteredStats).containsExactly(
84 ShadowAutofillManager.NotifyViewEntered(view, autofillNode.id, bounds)
85 )
Ralston Da Silva3e89a472019-07-31 01:21:51 -070086 }
87
88 @Test
89 fun requestAutofillForNode_beforeComposableIsPositioned_throwsError() {
90 // Arrange - Before the composable is positioned, the boundingBox is null.
91 val autofillNode = AutofillNode(onFill = {})
92
93 // Assert.
94 expectedException.expectMessage("requestAutofill called before onChildPositioned()")
95
96 // Act.
97 androidAutofill.requestAutofillForNode(autofillNode)
98 }
99
100 @Test
101 fun cancelAutofillForNode_calls_notifyViewExited() {
102 // Arrange.
103 val autofillNode = AutofillNode(onFill = {})
104
105 // Act.
106 androidAutofill.cancelAutofillForNode(autofillNode)
107
108 // Assert.
Ralston Da Silva91f20612019-08-13 15:07:17 -0700109 assertThat(autofillManager.viewExitedStats).containsExactly(
110 ShadowAutofillManager.NotifyViewExited(view, autofillNode.id)
111 )
112 }
113}
114
115@Implements(value = AutofillManager::class, minSdk = 26)
116internal class ShadowAutofillManager {
117 data class NotifyViewEntered(val view: View, val virtualId: Int, val rect: Rect)
118 data class NotifyViewExited(val view: View, val virtualId: Int)
119
120 val viewEnteredStats = mutableListOf<NotifyViewEntered>()
121 val viewExitedStats = mutableListOf<NotifyViewExited>()
122
123 @Implementation
Ralston Da Silvaad9752a2020-06-22 17:55:50 -0700124 fun notifyViewEntered(view: View, virtualId: Int, rect: AndroidRect) {
125 viewEnteredStats += NotifyViewEntered(view, virtualId, rect.toComposeRect())
Ralston Da Silva91f20612019-08-13 15:07:17 -0700126 }
127
128 @Implementation
129 fun notifyViewExited(view: View, virtualId: Int) {
130 viewExitedStats += NotifyViewExited(view, virtualId)
Ralston Da Silva3e89a472019-07-31 01:21:51 -0700131 }
Nikolay Igotti2c24c562020-06-18 12:49:02 +0300132}