1/* 2 * Copyright 2018 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 17package androidx.navigation 18 19import android.net.Uri 20import android.support.test.InstrumentationRegistry 21import android.support.test.filters.SmallTest 22import android.support.test.runner.AndroidJUnit4 23import org.junit.Assert.assertEquals 24import org.junit.Assert.assertTrue 25import org.junit.Test 26import org.junit.runner.RunWith 27 28@SmallTest 29@RunWith(AndroidJUnit4::class) 30class ActivityNavigatorDestinationBuilderTest { 31 private val navController = NavController(InstrumentationRegistry.getTargetContext()) 32 33 @Test 34 fun activity() { 35 val graph = navController.createGraph(startDestination = DESTINATION_ID) { 36 activity(DESTINATION_ID) { 37 label = LABEL 38 } 39 } 40 assertTrue("Destination should be added to the graph", 41 DESTINATION_ID in graph) 42 assertEquals("Destination should have label set", 43 LABEL, 44 graph[DESTINATION_ID].label) 45 } 46 47 @Test 48 fun activityClass() { 49 val graph = navController.createGraph(startDestination = DESTINATION_ID) { 50 activity(DESTINATION_ID) { 51 activityClass = TestActivity::class 52 } 53 } 54 assertTrue("Destination should be added to the graph", 55 DESTINATION_ID in graph) 56 assertEquals("Destination should have ComponentName set", 57 TestActivity::class.java.name, 58 (graph[DESTINATION_ID] as ActivityNavigator.Destination).component?.className) 59 } 60 61 @Test 62 fun action() { 63 val graph = navController.createGraph(startDestination = DESTINATION_ID) { 64 activity(DESTINATION_ID) { 65 action = ACTION 66 } 67 } 68 assertTrue("Destination should be added to the graph", 69 DESTINATION_ID in graph) 70 assertEquals("Destination should have action set", 71 ACTION, 72 (graph[DESTINATION_ID] as ActivityNavigator.Destination).action) 73 } 74 75 @Test 76 fun data() { 77 val graph = navController.createGraph(startDestination = DESTINATION_ID) { 78 activity(DESTINATION_ID) { 79 data = DATA 80 } 81 } 82 assertTrue("Destination should be added to the graph", 83 DESTINATION_ID in graph) 84 assertEquals("Destination should have data set", 85 DATA, 86 (graph[DESTINATION_ID] as ActivityNavigator.Destination).data) 87 } 88 89 @Test 90 fun dataPattern() { 91 val graph = navController.createGraph(startDestination = DESTINATION_ID) { 92 activity(DESTINATION_ID) { 93 dataPattern = DATA_PATTERN 94 } 95 } 96 assertTrue("Destination should be added to the graph", 97 DESTINATION_ID in graph) 98 assertEquals("Destination should have data pattern set", 99 DATA_PATTERN, 100 (graph[DESTINATION_ID] as ActivityNavigator.Destination).dataPattern) 101 } 102} 103 104private const val DESTINATION_ID = 1 105private const val LABEL = "Test" 106private const val ACTION = "ACTION_TEST" 107private val DATA = Uri.parse("http://www.example.com") 108private const val DATA_PATTERN = "http://www.example.com/{id}" 109