1 /*
<lambda>null2  * 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 package androidx.navigation.dynamicfeatures
17 
18 import android.content.ComponentName
19 import android.content.Context
20 import androidx.navigation.NavController
21 import androidx.navigation.plusAssign
22 import androidx.test.core.app.ApplicationProvider
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.filters.SmallTest
25 import com.google.android.play.core.splitinstall.SplitInstallManagerFactory
26 import com.google.common.truth.Truth.assertThat
27 import kotlinx.serialization.Serializable
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 
31 @SmallTest
32 @RunWith(AndroidJUnit4::class)
33 public class DynamicActivityNavigatorDestinationBuilderTest {
34 
35     private val context: Context = ApplicationProvider.getApplicationContext()
36 
37     private val navController =
38         NavController(context).also { controller ->
39             val installManager =
40                 DynamicInstallManager(context, SplitInstallManagerFactory.create(context))
41             val navigatorProvider = controller.navigatorProvider
42             navigatorProvider += DynamicActivityNavigator(context, installManager)
43         }
44 
45     @Suppress("DEPRECATION")
46     @Test
47     public fun module() {
48         val graph =
49             navController.createGraph(startDestination = DESTINATION_ID) {
50                 activity(DESTINATION_ID) { moduleName = MODULE_NAME }
51             }
52 
53         assertThat(
54                 (graph.findNode(DESTINATION_ID) as DynamicActivityNavigator.Destination).moduleName
55             )
56             .isEqualTo(MODULE_NAME)
57     }
58 
59     @Suppress("DEPRECATION")
60     @Test
61     public fun noModule() {
62         val graph =
63             navController.createGraph(startDestination = DESTINATION_ID) {
64                 activity(DESTINATION_ID) {}
65             }
66         assertThat(
67                 (graph.findNode(DESTINATION_ID) as DynamicActivityNavigator.Destination).moduleName
68             )
69             .isNull()
70     }
71 
72     @Suppress("DEPRECATION")
73     @Test
74     public fun activity() {
75         val graph =
76             navController.createGraph(startDestination = DESTINATION_ID) {
77                 activity(DESTINATION_ID) {
78                     moduleName = MODULE_NAME
79                     activityClassName = CLASS_NAME
80                 }
81             }
82         val destination = graph.findNode(DESTINATION_ID) as DynamicActivityNavigator.Destination
83         assertThat(destination.component).isEqualTo(ComponentName(context, CLASS_NAME))
84     }
85 
86     @Suppress("DEPRECATION")
87     @Test
88     public fun noActivity() {
89         val graph =
90             navController.createGraph(startDestination = DESTINATION_ID) {
91                 activity(DESTINATION_ID) {}
92             }
93         val destination = graph.findNode(DESTINATION_ID) as DynamicActivityNavigator.Destination
94         assertThat(destination.component).isNull()
95     }
96 
97     @Suppress("DEPRECATION")
98     @Test
99     public fun modulePackage() {
100         val graph =
101             navController.createGraph(startDestination = DESTINATION_ID) {
102                 activity(DESTINATION_ID) {
103                     moduleName = MODULE_NAME
104                     activityClassName = CLASS_NAME
105                     targetPackage = PACKAGE_NAME
106                 }
107             }
108         val destination = graph.findNode(DESTINATION_ID) as DynamicActivityNavigator.Destination
109         assertThat(destination.component).isEqualTo(ComponentName(PACKAGE_NAME, CLASS_NAME))
110     }
111 
112     @Test
113     public fun moduleRoute() {
114         val graph =
115             navController.createGraph(startDestination = DESTINATION_ROUTE) {
116                 activity(DESTINATION_ROUTE) { moduleName = MODULE_NAME }
117             }
118 
119         assertThat(
120                 (graph.findNode(DESTINATION_ROUTE) as DynamicActivityNavigator.Destination)
121                     .moduleName
122             )
123             .isEqualTo(MODULE_NAME)
124     }
125 
126     @Test
127     public fun noModuleRoute() {
128         val graph =
129             navController.createGraph(startDestination = DESTINATION_ROUTE) {
130                 activity(DESTINATION_ROUTE) {}
131             }
132         assertThat(
133                 (graph.findNode(DESTINATION_ROUTE) as DynamicActivityNavigator.Destination)
134                     .moduleName
135             )
136             .isNull()
137     }
138 
139     @Test
140     public fun activityRoute() {
141         val graph =
142             navController.createGraph(startDestination = DESTINATION_ROUTE) {
143                 activity(DESTINATION_ROUTE) {
144                     moduleName = MODULE_NAME
145                     activityClassName = CLASS_NAME
146                 }
147             }
148         val destination = graph.findNode(DESTINATION_ROUTE) as DynamicActivityNavigator.Destination
149         assertThat(destination.component).isEqualTo(ComponentName(context, CLASS_NAME))
150     }
151 
152     @Test
153     public fun noActivityRoute() {
154         val graph =
155             navController.createGraph(startDestination = DESTINATION_ROUTE) {
156                 activity(DESTINATION_ROUTE) {}
157             }
158         val destination = graph.findNode(DESTINATION_ROUTE) as DynamicActivityNavigator.Destination
159         assertThat(destination.component).isNull()
160     }
161 
162     @Test
163     public fun modulePackageRoute() {
164         val graph =
165             navController.createGraph(startDestination = DESTINATION_ROUTE) {
166                 activity(DESTINATION_ROUTE) {
167                     moduleName = MODULE_NAME
168                     activityClassName = CLASS_NAME
169                     targetPackage = PACKAGE_NAME
170                 }
171             }
172         val destination = graph.findNode(DESTINATION_ROUTE) as DynamicActivityNavigator.Destination
173         assertThat(destination.component).isEqualTo(ComponentName(PACKAGE_NAME, CLASS_NAME))
174     }
175 
176     @Test
177     public fun moduleKClass() {
178         val graph =
179             navController.createGraph(startDestination = TestClass::class) {
180                 activity<TestClass> { moduleName = MODULE_NAME }
181             }
182 
183         assertThat((graph.findNode<TestClass>() as DynamicActivityNavigator.Destination).moduleName)
184             .isEqualTo(MODULE_NAME)
185     }
186 
187     @Test
188     public fun noModuleKClass() {
189         val graph =
190             navController.createGraph(startDestination = TestClass::class) {
191                 activity<TestClass> {}
192             }
193         assertThat((graph.findNode<TestClass>() as DynamicActivityNavigator.Destination).moduleName)
194             .isNull()
195     }
196 
197     @Test
198     public fun activityKClass() {
199         val graph =
200             navController.createGraph(startDestination = TestClass::class) {
201                 activity<TestClass> {
202                     moduleName = MODULE_NAME
203                     activityClassName = CLASS_NAME
204                 }
205             }
206         val destination = graph.findNode<TestClass>() as DynamicActivityNavigator.Destination
207         assertThat(destination.component).isEqualTo(ComponentName(context, CLASS_NAME))
208     }
209 
210     @Test
211     public fun noActivityKClass() {
212         val graph =
213             navController.createGraph(startDestination = TestClass::class) {
214                 activity<TestClass> {}
215             }
216         val destination = graph.findNode<TestClass>() as DynamicActivityNavigator.Destination
217         assertThat(destination.component).isNull()
218     }
219 
220     @Test
221     public fun modulePackageKClass() {
222         val graph =
223             navController.createGraph(startDestination = TestClass::class) {
224                 activity<TestClass> {
225                     moduleName = MODULE_NAME
226                     activityClassName = CLASS_NAME
227                     targetPackage = PACKAGE_NAME
228                 }
229             }
230         val destination = graph.findNode<TestClass>() as DynamicActivityNavigator.Destination
231         assertThat(destination.component).isEqualTo(ComponentName(PACKAGE_NAME, CLASS_NAME))
232     }
233 
234     @Test
235     public fun moduleObject() {
236         @Serializable class TestClass(val arg: Int)
237         val graph =
238             navController.createGraph(startDestination = TestClass(0)) {
239                 activity<TestClass> { moduleName = MODULE_NAME }
240             }
241 
242         val dest = graph.findNode<TestClass>() as DynamicActivityNavigator.Destination
243         assertThat(dest.moduleName).isEqualTo(MODULE_NAME)
244         assertThat(dest.arguments["arg"]).isNotNull()
245     }
246 }
247 
248 private const val CLASS_NAME = "com.example.DynamicDestination"
249 private const val PACKAGE_NAME = "com.example.myPackage"
250 private const val DESTINATION_ID = 1
251 private const val DESTINATION_ROUTE = "route"
252 private const val MODULE_NAME = "myModule"
253