1 /*
2  * Copyright (C) 2023 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.settings.network.apn
18 
19 import android.content.Context
20 import android.net.Uri
21 import androidx.compose.runtime.mutableStateOf
22 import androidx.compose.runtime.remember
23 import androidx.compose.ui.test.assertIsDisplayed
24 import androidx.compose.ui.test.assertIsOff
25 import androidx.compose.ui.test.assertIsOn
26 import androidx.compose.ui.test.hasText
27 import androidx.compose.ui.test.isFocused
28 import androidx.compose.ui.test.junit4.createComposeRule
29 import androidx.compose.ui.test.onAllNodesWithText
30 import androidx.compose.ui.test.onChild
31 import androidx.compose.ui.test.onChildAt
32 import androidx.compose.ui.test.onLast
33 import androidx.compose.ui.test.onNodeWithText
34 import androidx.compose.ui.test.onRoot
35 import androidx.compose.ui.test.performClick
36 import androidx.compose.ui.test.performScrollToNode
37 import androidx.test.core.app.ApplicationProvider
38 import androidx.test.ext.junit.runners.AndroidJUnit4
39 import com.android.settings.R
40 import com.google.common.truth.Truth
41 import org.junit.Ignore
42 import org.junit.Rule
43 import org.junit.Test
44 import org.junit.runner.RunWith
45 import org.mockito.kotlin.mock
46 
47 @RunWith(AndroidJUnit4::class)
48 class ApnEditPageProviderTest {
49     @get:Rule
50     val composeTestRule = createComposeRule()
51 
52     private val context: Context = ApplicationProvider.getApplicationContext()
53     private val apnName = "apn_name"
54     private val proxy = "proxy"
55     private val port = "port"
56     private val apnType = context.resources.getString(R.string.apn_type)
57     private val apnRoaming = "IPv4"
58     private val apnEnable = context.resources.getString(R.string.carrier_enabled)
59     private val apnProtocolOptions =
60         context.resources.getStringArray(R.array.apn_protocol_entries).toList()
61     private val networkType = context.resources.getString(R.string.network_type)
62     private val passwordTitle = context.resources.getString(R.string.apn_password)
63     private val apnInit = ApnData(
64         name = apnName,
65         proxy = proxy,
66         port = port,
67         apnType = apnType,
68         apnRoaming = apnProtocolOptions.indexOf(apnRoaming),
69         apnEnable = true
70     )
71     private val apnData = mutableStateOf(
72         apnInit
73     )
<lambda>null74     private val uri = mock<Uri> {}
75 
76     @Test
apnEditPageProvider_namenull77     fun apnEditPageProvider_name() {
78         Truth.assertThat(ApnEditPageProvider.name).isEqualTo("ApnEdit")
79     }
80 
81     @Test
title_displayednull82     fun title_displayed() {
83         composeTestRule.setContent {
84             ApnPage(apnInit, remember { apnData }, uri)
85         }
86         composeTestRule.onNodeWithText(context.getString(R.string.apn_edit)).assertIsDisplayed()
87     }
88 
89     @Test
name_displayednull90     fun name_displayed() {
91         composeTestRule.setContent {
92             ApnPage(apnInit, remember { apnData }, uri)
93         }
94         composeTestRule.onNodeWithText(apnName, true).assertIsDisplayed()
95     }
96 
97     @Test
proxy_displayednull98     fun proxy_displayed() {
99         composeTestRule.setContent {
100             ApnPage(apnInit, remember { apnData }, uri)
101         }
102         composeTestRule.onRoot().onChild().onChildAt(0)
103             .performScrollToNode(hasText(proxy, true))
104         composeTestRule.onNodeWithText(proxy, true).assertIsDisplayed()
105     }
106 
107     @Test
port_displayednull108     fun port_displayed() {
109         composeTestRule.setContent {
110             ApnPage(apnInit, remember { apnData }, uri)
111         }
112         composeTestRule.onRoot().onChild().onChildAt(0)
113             .performScrollToNode(hasText(port, true))
114         composeTestRule.onNodeWithText(port, true).assertIsDisplayed()
115     }
116 
117     @Test
apn_type_displayednull118     fun apn_type_displayed() {
119         composeTestRule.setContent {
120             ApnPage(apnInit, remember { apnData }, uri)
121         }
122         composeTestRule.onRoot().onChild().onChildAt(0)
123             .performScrollToNode(hasText(apnType, true))
124         composeTestRule.onNodeWithText(apnType, true).assertIsDisplayed()
125     }
126 
127     @Test
apn_roaming_displayednull128     fun apn_roaming_displayed() {
129         composeTestRule.setContent {
130             ApnPage(apnInit, remember { apnData }, uri)
131         }
132         composeTestRule.onRoot().onChild().onChildAt(0)
133             .performScrollToNode(hasText(apnRoaming, true))
134         composeTestRule.onNodeWithText(apnRoaming, true).assertIsDisplayed()
135     }
136 
137     @Ignore("b/342374681")
138     @Test
carrier_enabled_displayednull139     fun carrier_enabled_displayed() {
140         composeTestRule.setContent {
141             ApnPage(apnInit, remember { apnData }, uri)
142         }
143         composeTestRule.onRoot().onChild().onChildAt(0)
144             .performScrollToNode(hasText(apnEnable, true))
145         composeTestRule.onNodeWithText(apnEnable, true).assertIsDisplayed()
146     }
147 
148     @Test
carrier_enabled_isCheckednull149     fun carrier_enabled_isChecked() {
150         composeTestRule.setContent {
151             ApnPage(apnInit, remember { apnData }, uri)
152         }
153         composeTestRule.onRoot().onChild().onChildAt(0)
154             .performScrollToNode(hasText(apnEnable, true))
155         composeTestRule.onNodeWithText(apnEnable, true).assertIsOn()
156     }
157 
158     @Ignore("b/342374681")
159     @Test
carrier_enabled_checkChangednull160     fun carrier_enabled_checkChanged() {
161         composeTestRule.setContent {
162             ApnPage(apnInit, remember { apnData }, uri)
163         }
164         composeTestRule.onRoot().onChild().onChildAt(0)
165             .performScrollToNode(hasText(apnEnable, true))
166         composeTestRule.onNodeWithText(apnEnable, true).performClick()
167         composeTestRule.onNodeWithText(apnEnable, true).assertIsOff()
168     }
169 
170     @Test
network_type_displayednull171     fun network_type_displayed() {
172         composeTestRule.setContent {
173             ApnPage(apnInit, remember { apnData }, uri)
174         }
175         composeTestRule.onRoot().onChild().onChildAt(0)
176             .performScrollToNode(hasText(networkType, true))
177         composeTestRule.onNodeWithText(networkType, true).assertIsDisplayed()
178     }
179 
180     @Test
network_type_changednull181     fun network_type_changed() {
182         composeTestRule.setContent {
183             ApnPage(apnInit, remember { apnData }, uri)
184         }
185         composeTestRule.onRoot().onChild().onChildAt(0)
186             .performScrollToNode(hasText(networkType, true))
187         composeTestRule.onNodeWithText(networkType, true).performClick()
188         composeTestRule.onNodeWithText(NETWORK_TYPE_LTE, true).performClick()
189         composeTestRule.onNode(hasText(NETWORK_TYPE_UNSPECIFIED) and isFocused(), true)
190             .assertDoesNotExist()
191         composeTestRule.onNode(hasText(NETWORK_TYPE_LTE) and isFocused(), true).assertIsDisplayed()
192     }
193 
194     @Test
network_type_changed_back2Defaultnull195     fun network_type_changed_back2Default() {
196         composeTestRule.setContent {
197             ApnPage(apnInit, remember { apnData }, uri)
198         }
199         composeTestRule.onRoot().onChild().onChildAt(0)
200             .performScrollToNode(hasText(networkType, true))
201         composeTestRule.onNodeWithText(networkType, true).performClick()
202         composeTestRule.onNodeWithText(NETWORK_TYPE_LTE, true).performClick()
203         composeTestRule.onNode(hasText(NETWORK_TYPE_UNSPECIFIED) and isFocused(), true)
204             .assertDoesNotExist()
205         composeTestRule.onNode(hasText(NETWORK_TYPE_LTE) and isFocused(), true).assertIsDisplayed()
206         composeTestRule.onAllNodesWithText(NETWORK_TYPE_LTE, true).onLast().performClick()
207         composeTestRule.onNode(hasText(NETWORK_TYPE_UNSPECIFIED) and isFocused(), true)
208             .assertIsDisplayed()
209         composeTestRule.onNode(hasText(NETWORK_TYPE_LTE) and isFocused(), true).assertDoesNotExist()
210     }
211 
212     @Test
password_displayednull213     fun password_displayed() {
214         composeTestRule.setContent {
215             ApnPage(apnInit, remember { apnData }, uri)
216         }
217         composeTestRule.onRoot().onChild().onChildAt(0)
218             .performScrollToNode(hasText(passwordTitle, true))
219         composeTestRule.onNodeWithText(passwordTitle, true).assertIsDisplayed()
220     }
221 
222     private companion object {
223         const val NETWORK_TYPE_UNSPECIFIED = "Unspecified"
224         const val NETWORK_TYPE_LTE = "LTE"
225     }
226 }