1 /*
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 
17 package androidx.navigation.fragment
18 
19 import androidx.fragment.app.DialogFragment
20 import androidx.navigation.contains
21 import androidx.navigation.createGraph
22 import androidx.navigation.get
23 import androidx.test.annotation.UiThreadTest
24 import androidx.test.ext.junit.runners.AndroidJUnit4
25 import androidx.test.filters.SmallTest
26 import com.google.common.truth.Truth.assertWithMessage
27 import org.junit.Rule
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 
31 @SmallTest
32 @RunWith(AndroidJUnit4::class)
33 class DialogFragmentNavigatorDestinationBuilderTest {
34     @Suppress("DEPRECATION")
35     @get:Rule
36     val activityRule = androidx.test.rule.ActivityTestRule<TestActivity>(TestActivity::class.java)
37     private val fragmentManager
38         get() = activityRule.activity.supportFragmentManager
39 
40     @Suppress("DEPRECATION")
41     @UiThreadTest
42     @Test
fragmentnull43     fun fragment() {
44         val navHostFragment = NavHostFragment()
45         fragmentManager.beginTransaction().add(android.R.id.content, navHostFragment).commitNow()
46         val graph =
47             navHostFragment.createGraph(startDestination = DESTINATION_ID) {
48                 dialog<BuilderTestDialogFragment>(DESTINATION_ID)
49             }
50         assertWithMessage("Destination should be added to the graph")
51             .that(DESTINATION_ID in graph)
52             .isTrue()
53         assertWithMessage("DialogFragment class should be set to BuilderTestDialogFragment")
54             .that((graph[DESTINATION_ID] as DialogFragmentNavigator.Destination).className)
55             .isEqualTo(BuilderTestDialogFragment::class.java.name)
56     }
57 
58     @Suppress("DEPRECATION")
59     @UiThreadTest
60     @Test
fragmentWithBodynull61     fun fragmentWithBody() {
62         val navHostFragment = NavHostFragment()
63         fragmentManager.beginTransaction().add(android.R.id.content, navHostFragment).commitNow()
64         val graph =
65             navHostFragment.createGraph(startDestination = DESTINATION_ID) {
66                 dialog<BuilderTestDialogFragment>(DESTINATION_ID) { label = LABEL }
67             }
68         assertWithMessage("Destination should be added to the graph")
69             .that(DESTINATION_ID in graph)
70             .isTrue()
71         assertWithMessage("DialogFragment class should be set to BuilderTestDialogFragment")
72             .that((graph[DESTINATION_ID] as DialogFragmentNavigator.Destination).className)
73             .isEqualTo(BuilderTestDialogFragment::class.java.name)
74         assertWithMessage("DialogFragment should have label set")
75             .that(graph[DESTINATION_ID].label)
76             .isEqualTo(LABEL)
77     }
78 
79     @UiThreadTest
80     @Test
fragmentRoutenull81     fun fragmentRoute() {
82         val navHostFragment = NavHostFragment()
83         fragmentManager.beginTransaction().add(android.R.id.content, navHostFragment).commitNow()
84         val graph =
85             navHostFragment.createGraph(startDestination = DESTINATION_ROUTE) {
86                 dialog<BuilderTestDialogFragment>(DESTINATION_ROUTE)
87             }
88         assertWithMessage("Destination should be added to the graph")
89             .that(DESTINATION_ROUTE in graph)
90             .isTrue()
91         assertWithMessage("DialogFragment class should be set to BuilderTestDialogFragment")
92             .that((graph[DESTINATION_ROUTE] as DialogFragmentNavigator.Destination).className)
93             .isEqualTo(BuilderTestDialogFragment::class.java.name)
94     }
95 
96     @UiThreadTest
97     @Test
fragmentWithBodyRoutenull98     fun fragmentWithBodyRoute() {
99         val navHostFragment = NavHostFragment()
100         fragmentManager.beginTransaction().add(android.R.id.content, navHostFragment).commitNow()
101         val graph =
102             navHostFragment.createGraph(startDestination = DESTINATION_ROUTE) {
103                 dialog<BuilderTestDialogFragment>(DESTINATION_ROUTE) { label = LABEL }
104             }
105         assertWithMessage("Destination should be added to the graph")
106             .that(DESTINATION_ROUTE in graph)
107             .isTrue()
108         assertWithMessage("DialogFragment class should be set to BuilderTestDialogFragment")
109             .that((graph[DESTINATION_ROUTE] as DialogFragmentNavigator.Destination).className)
110             .isEqualTo(BuilderTestDialogFragment::class.java.name)
111         assertWithMessage("DialogFragment should have label set")
112             .that(graph[DESTINATION_ROUTE].label)
113             .isEqualTo(LABEL)
114     }
115 
116     @UiThreadTest
117     @Test
fragmentKClassnull118     fun fragmentKClass() {
119         val navHostFragment = NavHostFragment()
120         fragmentManager.beginTransaction().add(android.R.id.content, navHostFragment).commitNow()
121         val graph =
122             navHostFragment.createGraph(startDestination = DESTINATION_ROUTE) {
123                 dialog<BuilderTestDialogFragment, TestClass>()
124             }
125         assertWithMessage("Destination should be added to the graph")
126             .that(TestClass::class in graph)
127             .isTrue()
128         assertWithMessage("DialogFragment class should be set to BuilderTestDialogFragment")
129             .that((graph[TestClass::class] as DialogFragmentNavigator.Destination).className)
130             .isEqualTo(BuilderTestDialogFragment::class.java.name)
131     }
132 
133     @UiThreadTest
134     @Test
fragmentWithBodyKClassnull135     fun fragmentWithBodyKClass() {
136         val navHostFragment = NavHostFragment()
137         fragmentManager.beginTransaction().add(android.R.id.content, navHostFragment).commitNow()
138         val graph =
139             navHostFragment.createGraph(startDestination = DESTINATION_ROUTE) {
140                 dialog<BuilderTestDialogFragment, TestClass> { label = LABEL }
141             }
142         assertWithMessage("Destination should be added to the graph")
143             .that(TestClass::class in graph)
144             .isTrue()
145         assertWithMessage("DialogFragment class should be set to BuilderTestDialogFragment")
146             .that((graph[TestClass::class] as DialogFragmentNavigator.Destination).className)
147             .isEqualTo(BuilderTestDialogFragment::class.java.name)
148         assertWithMessage("DialogFragment should have label set")
149             .that(graph[TestClass::class].label)
150             .isEqualTo(LABEL)
151     }
152 }
153 
154 private const val DESTINATION_ID = 1
155 private const val DESTINATION_ROUTE = "destination"
156 private const val LABEL = "Test"
157 
158 class BuilderTestDialogFragment : DialogFragment()
159