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