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