|
| 1 | +/* |
| 2 | + * Copyright 2024 Esri |
| 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.arcgismaps.toolkit.featureforms |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import androidx.compose.ui.platform.LocalContext |
| 21 | +import androidx.compose.ui.test.assert |
| 22 | +import androidx.compose.ui.test.assertContentDescriptionContains |
| 23 | +import androidx.compose.ui.test.assertIsDisplayed |
| 24 | +import androidx.compose.ui.test.assertIsNotFocused |
| 25 | +import androidx.compose.ui.test.assertIsOff |
| 26 | +import androidx.compose.ui.test.assertIsOn |
| 27 | +import androidx.compose.ui.test.assertTextEquals |
| 28 | +import androidx.compose.ui.test.hasAnyChild |
| 29 | +import androidx.compose.ui.test.hasContentDescription |
| 30 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 31 | +import androidx.compose.ui.test.onNodeWithText |
| 32 | +import androidx.compose.ui.test.performClick |
| 33 | +import com.arcgismaps.ArcGISEnvironment |
| 34 | +import com.arcgismaps.data.ArcGISFeature |
| 35 | +import com.arcgismaps.data.QueryParameters |
| 36 | +import com.arcgismaps.mapping.ArcGISMap |
| 37 | +import com.arcgismaps.mapping.featureforms.FeatureForm |
| 38 | +import com.arcgismaps.mapping.featureforms.FieldFormElement |
| 39 | +import com.arcgismaps.mapping.layers.FeatureLayer |
| 40 | +import junit.framework.TestCase |
| 41 | +import kotlinx.coroutines.test.runTest |
| 42 | +import org.junit.Assert.fail |
| 43 | +import org.junit.Before |
| 44 | +import org.junit.BeforeClass |
| 45 | +import org.junit.Rule |
| 46 | +import org.junit.Test |
| 47 | + |
| 48 | +class SwitchFieldTests { |
| 49 | + |
| 50 | + @get:Rule |
| 51 | + val composeTestRule = createComposeRule() |
| 52 | + |
| 53 | + private lateinit var context: Context |
| 54 | + |
| 55 | + @Before |
| 56 | + fun setUp() { |
| 57 | + composeTestRule.setContent { |
| 58 | + context = LocalContext.current |
| 59 | + FeatureForm(featureForm = featureForm) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test case 5.1: |
| 65 | + * Given a SwitchField type with a pre-existing "on" value |
| 66 | + * When the switch is tapped |
| 67 | + * Then the switch toggles to an "off" state. |
| 68 | + * https://devtopia.esri.com/runtime/common-toolkit/blob/main/designs/Forms/FormsTestDesign.md#test-case-51-test-switch-on |
| 69 | + */ |
| 70 | + @Test |
| 71 | + fun testSwitchIsOn() { |
| 72 | + // get the switch form element |
| 73 | + val switchElement = featureForm.getFieldFormElementWithLabel("switch integer") |
| 74 | + ?: return fail("element not found") |
| 75 | + // find the field with the the label |
| 76 | + val switchField = composeTestRule.onNodeWithText(switchElement.label) |
| 77 | + // assert it is displayed and not focused |
| 78 | + switchField.assertIsDisplayed() |
| 79 | + switchField.assertIsNotFocused() |
| 80 | + // find the switch field |
| 81 | + val switch = switchField.onChildWithContentDescription("switch", recurse = true) |
| 82 | + switch.assertIsDisplayed() |
| 83 | + // assert that the switch is on |
| 84 | + switch.assertIsOn() |
| 85 | + // assert the value displayed is the current "on" value |
| 86 | + switchField.assertEditableTextEquals(switchElement.formattedValue) |
| 87 | + // tap on the switch |
| 88 | + switchField.performClick() |
| 89 | + // assert that the switch is on |
| 90 | + switch.assertIsOff() |
| 91 | + // assert the value displayed is the current "off" value |
| 92 | + switchField.assertEditableTextEquals(switchElement.formattedValue) |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Test case 5.2: |
| 97 | + * Given a SwitchField type with a pre-existing "off" value |
| 98 | + * When the switch is tapped |
| 99 | + * Then the switch toggles to an "on" state. |
| 100 | + * https://devtopia.esri.com/runtime/common-toolkit/blob/main/designs/Forms/FormsTestDesign.md#test-case-52-test-switch-off |
| 101 | + */ |
| 102 | + @Test |
| 103 | + fun testSwitchIsOff() { |
| 104 | + // get the switch form element |
| 105 | + val switchElement = featureForm.getFieldFormElementWithLabel("switch string") |
| 106 | + ?: return fail("element not found") |
| 107 | + // find the field with the the label |
| 108 | + val switchField = composeTestRule.onNodeWithText(switchElement.label) |
| 109 | + // assert it is displayed and not focused |
| 110 | + switchField.assertIsDisplayed() |
| 111 | + switchField.assertIsNotFocused() |
| 112 | + // find the switch control |
| 113 | + val switch = switchField.onChildWithContentDescription("switch", recurse = true) |
| 114 | + switch.assertIsDisplayed() |
| 115 | + // assert that the switch is off |
| 116 | + switch.assertIsOff() |
| 117 | + // assert the value displayed is the current "off" value |
| 118 | + switchField.assertEditableTextEquals(switchElement.formattedValue) |
| 119 | + // tap on the switch |
| 120 | + switchField.performClick() |
| 121 | + // assert that the switch is on |
| 122 | + switch.assertIsOn() |
| 123 | + // assert the value displayed is the current "on" value |
| 124 | + switchField.assertEditableTextEquals(switchElement.formattedValue) |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Test case 5.3: |
| 129 | + * Given a FieldFormElement with a SwitchFormInput type and no pre-existing value |
| 130 | + * When the FeatureForm is displayed |
| 131 | + * Then the FieldFormElement is displayed as a ComboBox. |
| 132 | + * https://devtopia.esri.com/runtime/common-toolkit/blob/main/designs/Forms/FormsTestDesign.md#test-case-53-test-switch-with-no-value |
| 133 | + */ |
| 134 | + @Test |
| 135 | + fun testSwitchWithNoValue() { |
| 136 | + // get the switch form element |
| 137 | + val switchElement = featureForm.getFieldFormElementWithLabel("switch double") |
| 138 | + ?: return fail("element not found") |
| 139 | + // find the field with the the label |
| 140 | + val comboBoxField = composeTestRule.onNodeWithText(switchElement.label) |
| 141 | + // assert it is displayed and not focused |
| 142 | + comboBoxField.assertIsDisplayed() |
| 143 | + comboBoxField.assertIsNotFocused() |
| 144 | + // assert that this field does not have any switch control |
| 145 | + comboBoxField.assert(!hasAnyChild(hasContentDescription("switch"))) |
| 146 | + // assert a "no value" placeholder is visible |
| 147 | + comboBoxField.assertTextEquals( |
| 148 | + switchElement.label, |
| 149 | + context.getString(R.string.no_value) |
| 150 | + ) |
| 151 | + // validate that the options icon is visible |
| 152 | + // since combo box fields have an icon and switch fields do not |
| 153 | + comboBoxField.assertContentDescriptionContains("field icon").assertIsDisplayed() |
| 154 | + } |
| 155 | + |
| 156 | + companion object { |
| 157 | + private lateinit var featureForm: FeatureForm |
| 158 | + |
| 159 | + @BeforeClass |
| 160 | + @JvmStatic |
| 161 | + fun setupClass() = runTest { |
| 162 | + ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = |
| 163 | + FeatureFormsTestChallengeHandler( |
| 164 | + BuildConfig.webMapUser, |
| 165 | + BuildConfig.webMapPassword |
| 166 | + ) |
| 167 | + val map = |
| 168 | + ArcGISMap("https://runtimecoretest.maps.arcgis.com/home/item.html?id=ff98f13b32b349adb55da5528d9174dc") |
| 169 | + map.load().onFailure { TestCase.fail("failed to load webmap with ${it.message}") } |
| 170 | + val featureLayer = map.operationalLayers.first() as? FeatureLayer |
| 171 | + featureLayer?.let { layer -> |
| 172 | + layer.load().onFailure { TestCase.fail("failed to load layer with ${it.message}") } |
| 173 | + val featureFormDefinition = layer.featureFormDefinition!! |
| 174 | + val parameters = QueryParameters().also { |
| 175 | + it.objectIds.add(1L) |
| 176 | + it.maxFeatures = 1 |
| 177 | + } |
| 178 | + layer.featureTable?.queryFeatures(parameters)?.onSuccess { featureQueryResult -> |
| 179 | + val feature = featureQueryResult.find { |
| 180 | + it is ArcGISFeature |
| 181 | + } as? ArcGISFeature |
| 182 | + if (feature == null) TestCase.fail("failed to fetch feature") |
| 183 | + feature?.load()?.onFailure { |
| 184 | + TestCase.fail("failed to load feature with ${it.message}") |
| 185 | + } |
| 186 | + featureForm = FeatureForm(feature!!, featureFormDefinition) |
| 187 | + featureForm.evaluateExpressions() |
| 188 | + }?.onFailure { |
| 189 | + TestCase.fail("failed to query features on layer's featuretable with ${it.message}") |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +/** |
| 197 | + * Returns a [FieldFormElement] with the given [label] if it exists. Else a null is returned. |
| 198 | + */ |
| 199 | +internal fun FeatureForm.getFieldFormElementWithLabel(label: String): FieldFormElement? = |
| 200 | + elements.find { |
| 201 | + it is FieldFormElement && it.label == label |
| 202 | + } as? FieldFormElement |
| 203 | + |
0 commit comments