Home | History | Annotate | Download | only in menu
      1 /*
      2  * Copyright (C) 2015 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 package com.android.tv.menu;
     17 
     18 import static android.support.test.InstrumentationRegistry.getTargetContext;
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.support.test.filters.SmallTest;
     23 
     24 import com.android.tv.menu.Menu.OnMenuVisibilityChangeListener;
     25 
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 import org.mockito.Matchers;
     29 import org.mockito.Mockito;
     30 import org.mockito.invocation.InvocationOnMock;
     31 import org.mockito.stubbing.Answer;
     32 
     33 /**
     34  * Tests for {@link Menu}.
     35  */
     36 @SmallTest
     37 public class MenuTest {
     38     private Menu mMenu;
     39     private IMenuView mMenuView;
     40     private OnMenuVisibilityChangeListener mVisibilityChangeListener;
     41 
     42     @Before
     43     public void setUp() {
     44         mMenuView = Mockito.mock(IMenuView.class);
     45         MenuRowFactory factory = Mockito.mock(MenuRowFactory.class);
     46         Mockito.when(factory.createMenuRow(Mockito.any(Menu.class), Mockito.any(Class.class)))
     47                 .thenReturn(null);
     48         mVisibilityChangeListener = Mockito.mock(OnMenuVisibilityChangeListener.class);
     49         mMenu = new Menu(getTargetContext(), mMenuView, factory, mVisibilityChangeListener);
     50         mMenu.disableAnimationForTest();
     51     }
     52 
     53     @Test
     54     public void testScheduleHide() {
     55         mMenu.show(Menu.REASON_NONE);
     56         setMenuVisible(true);
     57         assertTrue("Hide is not scheduled", mMenu.isHideScheduled());
     58         mMenu.hide(false);
     59         setMenuVisible(false);
     60         assertFalse("Hide is scheduled", mMenu.isHideScheduled());
     61 
     62         mMenu.setKeepVisible(true);
     63         mMenu.show(Menu.REASON_NONE);
     64         setMenuVisible(true);
     65         assertFalse("Hide is scheduled", mMenu.isHideScheduled());
     66         mMenu.setKeepVisible(false);
     67         assertTrue("Hide is not scheduled", mMenu.isHideScheduled());
     68         mMenu.setKeepVisible(true);
     69         assertFalse("Hide is scheduled", mMenu.isHideScheduled());
     70         mMenu.hide(false);
     71         setMenuVisible(false);
     72         assertFalse("Hide is scheduled", mMenu.isHideScheduled());
     73     }
     74 
     75     @Test
     76     public void testShowHide_ReasonNone() {
     77         // Show with REASON_NONE
     78         mMenu.show(Menu.REASON_NONE);
     79         setMenuVisible(true);
     80         // Listener should be called with "true" argument.
     81         Mockito.verify(mVisibilityChangeListener, Mockito.atLeastOnce())
     82                 .onMenuVisibilityChange(Matchers.eq(true));
     83         Mockito.verify(mVisibilityChangeListener, Mockito.never())
     84                 .onMenuVisibilityChange(Matchers.eq(false));
     85         // IMenuView.show should be called with the same parameter.
     86         Mockito.verify(mMenuView).onShow(Matchers.eq(Menu.REASON_NONE),
     87                 Matchers.isNull(String.class), Matchers.isNull(Runnable.class));
     88         mMenu.hide(true);
     89         setMenuVisible(false);
     90         // Listener should be called with "false" argument.
     91         Mockito.verify(mVisibilityChangeListener, Mockito.atLeastOnce())
     92                 .onMenuVisibilityChange(Matchers.eq(false));
     93         Mockito.verify(mMenuView).onHide();
     94     }
     95 
     96     @Test
     97     public void testShowHide_ReasonGuide() {
     98         // Show with REASON_GUIDE
     99         mMenu.show(Menu.REASON_GUIDE);
    100         setMenuVisible(true);
    101         // Listener should be called with "true" argument.
    102         Mockito.verify(mVisibilityChangeListener, Mockito.atLeastOnce())
    103                 .onMenuVisibilityChange(Matchers.eq(true));
    104         Mockito.verify(mVisibilityChangeListener, Mockito.never())
    105                 .onMenuVisibilityChange(Matchers.eq(false));
    106         // IMenuView.show should be called with the same parameter.
    107         Mockito.verify(mMenuView).onShow(Matchers.eq(Menu.REASON_GUIDE),
    108                 Matchers.eq(ChannelsRow.ID), Matchers.isNull(Runnable.class));
    109         mMenu.hide(false);
    110         setMenuVisible(false);
    111         // Listener should be called with "false" argument.
    112         Mockito.verify(mVisibilityChangeListener, Mockito.atLeastOnce())
    113                 .onMenuVisibilityChange(Matchers.eq(false));
    114         Mockito.verify(mMenuView).onHide();
    115     }
    116 
    117     @Test
    118     public void testShowHide_ReasonPlayControlsFastForward() {
    119         // Show with REASON_PLAY_CONTROLS_FAST_FORWARD
    120         mMenu.show(Menu.REASON_PLAY_CONTROLS_FAST_FORWARD);
    121         setMenuVisible(true);
    122         // Listener should be called with "true" argument.
    123         Mockito.verify(mVisibilityChangeListener, Mockito.atLeastOnce())
    124                 .onMenuVisibilityChange(Matchers.eq(true));
    125         Mockito.verify(mVisibilityChangeListener, Mockito.never())
    126                 .onMenuVisibilityChange(Matchers.eq(false));
    127         // IMenuView.show should be called with the same parameter.
    128         Mockito.verify(mMenuView).onShow(Matchers.eq(Menu.REASON_PLAY_CONTROLS_FAST_FORWARD),
    129                 Matchers.eq(PlayControlsRow.ID), Matchers.isNull(Runnable.class));
    130         mMenu.hide(false);
    131         setMenuVisible(false);
    132         // Listener should be called with "false" argument.
    133         Mockito.verify(mVisibilityChangeListener, Mockito.atLeastOnce())
    134                 .onMenuVisibilityChange(Matchers.eq(false));
    135         Mockito.verify(mMenuView).onHide();
    136     }
    137 
    138     private void setMenuVisible(final boolean visible) {
    139         Mockito.when(mMenuView.isVisible()).thenAnswer(new Answer<Boolean>() {
    140             @Override
    141             public Boolean answer(InvocationOnMock invocation) throws Throwable {
    142                 return visible;
    143             }
    144         });
    145     }
    146 }
    147