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.testing 18 19 import androidx.navigation.NavDestination 20 import androidx.navigation.NavGraphNavigator 21 import androidx.navigation.Navigator 22 import androidx.navigation.NavigatorProvider 23 24 /** 25 * A [NavigatorProvider] for testing that only parses 26 * [navigation graphs][androidx.navigation.NavGraph] and [destinations][NavDestination]. 27 */ 28 internal class TestNavigatorProvider : NavigatorProvider() { 29 30 /** A [Navigator] that only supports creating destinations. */ 31 private val navigator = 32 object : Navigator<NavDestination>() { createDestinationnull33 override fun createDestination() = NavDestination("test") 34 } 35 36 init { 37 addNavigator(NavGraphNavigator(this)) 38 addNavigator("test", navigator) 39 } 40 getNavigatornull41 override fun <T : Navigator<out NavDestination>> getNavigator(name: String): T { 42 return try { 43 super.getNavigator(name) 44 } catch (_: IllegalStateException) { 45 @Suppress("UNCHECKED_CAST") 46 navigator as T 47 } 48 } 49 } 50