Home | History | Annotate | Download | only in navigation
      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 
     17 package androidx.navigation;
     18 
     19 import static org.hamcrest.CoreMatchers.is;
     20 import static org.hamcrest.CoreMatchers.not;
     21 import static org.hamcrest.CoreMatchers.nullValue;
     22 import static org.hamcrest.MatcherAssert.assertThat;
     23 import static org.mockito.Mockito.mock;
     24 
     25 import android.support.annotation.IdRes;
     26 import android.support.test.filters.SmallTest;
     27 
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 import org.junit.runners.JUnit4;
     31 
     32 @SuppressWarnings("unchecked")
     33 @RunWith(JUnit4.class)
     34 @SmallTest
     35 public class NavDestinationTest {
     36     @IdRes
     37     private static final int INVALID_ACTION_ID = 0;
     38     @IdRes
     39     private static final int ACTION_ID = 1;
     40     @IdRes
     41     private static final int DESTINATION_ID = 1;
     42 
     43     @Test
     44     public void buildDeepLinkIds() {
     45         NavDestination destination = new NavDestination(mock(Navigator.class));
     46         destination.setId(DESTINATION_ID);
     47         int parentId = 2;
     48         NavGraph parent = new NavGraph(mock(Navigator.class));
     49         parent.setId(parentId);
     50         destination.setParent(parent);
     51         int[] deepLinkIds = destination.buildDeepLinkIds();
     52         assertThat(deepLinkIds.length, is(2));
     53         assertThat(deepLinkIds[0], is(parentId));
     54         assertThat(deepLinkIds[1], is(DESTINATION_ID));
     55     }
     56 
     57     @Test
     58     public void putActionByDestinationId() {
     59         NavDestination destination = new NavDestination(mock(Navigator.class));
     60         destination.putAction(ACTION_ID, DESTINATION_ID);
     61 
     62         assertThat(destination.getAction(ACTION_ID), not(nullValue()));
     63         assertThat(destination.getAction(ACTION_ID).getDestinationId(), is(DESTINATION_ID));
     64     }
     65 
     66     @Test(expected = IllegalArgumentException.class)
     67     public void putActionWithInvalidDestinationId() {
     68         NavDestination destination = new NavDestination(mock(Navigator.class));
     69         destination.putAction(INVALID_ACTION_ID, DESTINATION_ID);
     70     }
     71 
     72     @Test
     73     public void putAction() {
     74         NavDestination destination = new NavDestination(mock(Navigator.class));
     75         NavAction action = new NavAction(DESTINATION_ID);
     76         destination.putAction(ACTION_ID, action);
     77 
     78         assertThat(destination.getAction(ACTION_ID), is(action));
     79     }
     80 
     81     @Test
     82     public void removeAction() {
     83         NavDestination destination = new NavDestination(mock(Navigator.class));
     84         NavAction action = new NavAction(DESTINATION_ID);
     85         destination.putAction(ACTION_ID, action);
     86 
     87         assertThat(destination.getAction(ACTION_ID), is(action));
     88 
     89         destination.removeAction(ACTION_ID);
     90 
     91         assertThat(destination.getAction(ACTION_ID), nullValue());
     92     }
     93 }
     94