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