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