• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 package com.android.multiuser.widget.ui.tests
17 
18 import androidx.activity.ComponentActivity
19 import androidx.compose.ui.test.assertCountEquals
20 import androidx.compose.ui.test.onAllNodesWithText
21 import androidx.compose.ui.test.onNodeWithText
22 import androidx.compose.ui.test.performClick
23 import androidx.compose.ui.test.junit4.createAndroidComposeRule
24 import androidx.test.platform.app.InstrumentationRegistry;
25 import androidx.test.ext.junit.runners.AndroidJUnit4
26 
27 import org.junit.Rule
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.junit.Assert.assertEquals
31 import org.junit.Assert.assertFalse
32 import org.junit.Assert.assertTrue
33 import org.junit.Assert.assertNull
34 import org.junit.Assert.assertNotNull
35 
36 import android.content.Context;
37 import android.content.Intent;
38 
39 import com.android.multiuser.widget.data.IntentRepository
40 import com.android.multiuser.widget.data.model.UserSwitchRestrictions
41 import com.android.multiuser.widget.domain.DialogUseCase
42 import com.android.multiuser.widget.ui.DialogView
43 
44 @RunWith(AndroidJUnit4::class)
45 class DialogViewTest {
46     @get:Rule val composeTestRule = createAndroidComposeRule<ComponentActivity>()
47 
48     @Test
testSwitchFailedDialognull49     fun testSwitchFailedDialog() {
50         val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
51         val intentRepository = IntentRepository(context.resources)
52         val intent = intentRepository.getUserSwitchRestrictedIntent(UserSwitchRestrictions.UNKNOWN)
53         val dialogUseCase = DialogUseCase(intent, context.resources)
54         val model = dialogUseCase()
55         var dismissed = false
56         var actionIntent: Intent? = null
57         composeTestRule.setContent {
58             DialogView(model, { dismissed = true }, { actionIntent = it })
59         }
60         val title = context.getString(R.string.widget_switch_failed_dialog_title)
61         val message = context.getString(R.string.widget_switch_failed_dialog_message)
62         val closeButton = context.getString(R.string.dialog_close_button_text)
63         val actionButton = context.getString(R.string.widget_switch_disabled_dialog_open_settings_button)
64         assertEquals(model.title, title)
65         composeTestRule.onAllNodesWithText(title).assertCountEquals(1)
66         assertEquals(model.message, message)
67         composeTestRule.onAllNodesWithText(message).assertCountEquals(1)
68         composeTestRule.onAllNodesWithText(closeButton).assertCountEquals(1)
69         composeTestRule.onAllNodesWithText(actionButton).assertCountEquals(0)
70         assertFalse(dismissed)
71         composeTestRule.onNodeWithText(closeButton).performClick()
72         assertTrue(dismissed)
73     }
74 
75     @Test
testSwitchDisabledByUsernull76     fun testSwitchDisabledByUser() {
77         val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
78         val intentRepository = IntentRepository(context.resources)
79         val intent = intentRepository.getUserSwitchRestrictedIntent(UserSwitchRestrictions.DISABLED)
80         val dialogUseCase = DialogUseCase(intent, context.resources)
81         val model = dialogUseCase()
82         var dismissed = false
83         var actionIntent: Intent? = null
84         composeTestRule.setContent {
85             DialogView(model, { dismissed = true }, { actionIntent = it })
86         }
87         val title: String = context.getString(R.string.widget_switch_disabled_dialog_title)
88         val message: String = context.getString(R.string.widget_switch_disabled_dialog_message)
89         val closeButton: String = context.getString(R.string.dialog_close_button_text)
90         val actionButton: String = context.getString(R.string.widget_switch_disabled_dialog_open_settings_button)
91 
92         assertEquals(model.title, title)
93         composeTestRule.onAllNodesWithText(title).assertCountEquals(1)
94         assertEquals(model.message, message)
95         composeTestRule.onAllNodesWithText(message).assertCountEquals(1)
96         composeTestRule.onAllNodesWithText(closeButton).assertCountEquals(1)
97         composeTestRule.onAllNodesWithText(actionButton).assertCountEquals(1)
98         assertFalse(dismissed)
99         composeTestRule.onNodeWithText(closeButton).performClick()
100         assertTrue(dismissed)
101         assertNull(actionIntent)
102         composeTestRule.onNodeWithText(actionButton).performClick()
103         assertNotNull(actionIntent)
104     }
105 
106 
107     @Test
testSwitchDisabledByWorkPolicynull108     fun testSwitchDisabledByWorkPolicy() {
109         val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
110         val intentRepository = IntentRepository(context.resources)
111         val intent = intentRepository.getUserSwitchRestrictedIntent(UserSwitchRestrictions.WORK_POLICY)
112         val dialogUseCase = DialogUseCase(intent, context.resources)
113         val model = dialogUseCase()
114         var dismissed = false
115         var actionIntent: Intent? = null
116         composeTestRule.setContent {
117             DialogView(model, { dismissed = true }, { actionIntent = it })
118         }
119         val title: String = context.getString(R.string.widget_switch_work_restriction_dialog_title)
120         val message: String = context.getString(R.string.widget_switch_work_restriction_dialog_message)
121         val closeButton: String = context.getString(R.string.dialog_close_button_text)
122         val actionButton: String = context.getString(R.string.widget_switch_disabled_dialog_open_settings_button)
123 
124         assertEquals(model.title, title)
125         composeTestRule.onAllNodesWithText(title).assertCountEquals(1)
126         assertEquals(model.message, message)
127         composeTestRule.onAllNodesWithText(message).assertCountEquals(1)
128         composeTestRule.onAllNodesWithText(closeButton).assertCountEquals(1)
129         composeTestRule.onAllNodesWithText(actionButton).assertCountEquals(0)
130         assertFalse(dismissed)
131         composeTestRule.onNodeWithText(closeButton).performClick()
132         assertTrue(dismissed)
133     }
134 
135     @Test
testSwitchDisabledNotAllowedOnCallnull136     fun testSwitchDisabledNotAllowedOnCall() {
137         val context = InstrumentationRegistry.getInstrumentation().getTargetContext()
138         val intentRepository = IntentRepository(context.resources)
139         val intent = intentRepository.getUserSwitchRestrictedIntent(UserSwitchRestrictions.ONCALL_OR_LOCKED)
140         val dialogUseCase = DialogUseCase(intent, context.resources)
141         val model = dialogUseCase()
142         var dismissed = false
143         var actionIntent: Intent? = null
144         composeTestRule.setContent {
145             DialogView(model, { dismissed = true }, { actionIntent = it })
146         }
147         val title: String = context.getString(R.string.widget_switch_not_allowed_dialog_title)
148         val message: String = context.getString(R.string.widget_switch_not_allowed_dialog_message)
149         val closeButton: String = context.getString(R.string.dialog_close_button_text)
150         val actionButton: String = context.getString(R.string.widget_switch_disabled_dialog_open_settings_button)
151 
152         assertEquals(model.title, title)
153         composeTestRule.onAllNodesWithText(title).assertCountEquals(1)
154         assertEquals(model.message, message)
155         composeTestRule.onAllNodesWithText(message).assertCountEquals(1)
156         composeTestRule.onAllNodesWithText(closeButton).assertCountEquals(1)
157         composeTestRule.onAllNodesWithText(actionButton).assertCountEquals(0)
158         assertFalse(dismissed)
159         composeTestRule.onNodeWithText(closeButton).performClick()
160         assertTrue(dismissed)
161     }
162 }
163