1/* 2 * Copyright (C) 2017 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 static org.hamcrest.CoreMatchers.is; 20import static org.hamcrest.MatcherAssert.assertThat; 21import static org.junit.Assert.fail; 22 23import android.support.test.filters.SmallTest; 24 25import org.junit.Test; 26import org.junit.runner.RunWith; 27import org.junit.runners.JUnit4; 28 29@SuppressWarnings("unchecked") 30@RunWith(JUnit4.class) 31@SmallTest 32public class SimpleNavigatorProviderTest { 33 @Test 34 public void addWithMissingAnnotationName() { 35 SimpleNavigatorProvider provider = new SimpleNavigatorProvider(); 36 Navigator navigator = new NoNameNavigator(); 37 try { 38 provider.addNavigator(navigator); 39 fail("Adding a provider with no @Navigator.Name should cause an " 40 + "IllegalArgumentException"); 41 } catch (IllegalArgumentException e) { 42 // Expected 43 } 44 } 45 46 @Test 47 public void addWithMissingAnnotationNameGetWithExplicitName() { 48 SimpleNavigatorProvider provider = new SimpleNavigatorProvider(); 49 Navigator navigator = new NoNameNavigator(); 50 provider.addNavigator("name", navigator); 51 assertThat(provider.getNavigator("name"), is(navigator)); 52 } 53 54 @Test 55 public void addWithExplicitNameGetWithExplicitName() { 56 SimpleNavigatorProvider provider = new SimpleNavigatorProvider(); 57 Navigator navigator = new EmptyNavigator(); 58 provider.addNavigator("name", navigator); 59 assertThat(provider.getNavigator("name"), is(navigator)); 60 try { 61 provider.getNavigator(EmptyNavigator.class); 62 fail("getNavigator(Class) with an invalid name should cause an IllegalStateException"); 63 } catch (IllegalStateException e) { 64 // Expected 65 } 66 } 67 68 @Test 69 public void addWithExplicitNameGetWithMissingAnnotationName() { 70 SimpleNavigatorProvider provider = new SimpleNavigatorProvider(); 71 Navigator navigator = new NoNameNavigator(); 72 provider.addNavigator("name", navigator); 73 try { 74 provider.getNavigator(NoNameNavigator.class); 75 fail("getNavigator(Class) with no @Navigator.Name should cause " 76 + "an IllegalArgumentException"); 77 } catch (IllegalArgumentException e) { 78 // Expected 79 } 80 } 81 82 @Test 83 public void addWithAnnotationNameGetWithAnnotationName() { 84 SimpleNavigatorProvider provider = new SimpleNavigatorProvider(); 85 Navigator navigator = new EmptyNavigator(); 86 provider.addNavigator(navigator); 87 assertThat(provider.getNavigator(EmptyNavigator.class), is(navigator)); 88 } 89 90 @Test 91 public void addWithAnnotationNameGetWithExplicitName() { 92 SimpleNavigatorProvider provider = new SimpleNavigatorProvider(); 93 Navigator navigator = new EmptyNavigator(); 94 provider.addNavigator(navigator); 95 assertThat(provider.getNavigator(EmptyNavigator.NAME), is(navigator)); 96 } 97} 98