001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.hbtop.screen.top; 019 020import static org.hamcrest.CoreMatchers.is; 021import static org.junit.Assert.assertThat; 022import static org.mockito.ArgumentMatchers.any; 023import static org.mockito.ArgumentMatchers.eq; 024import static org.mockito.Mockito.inOrder; 025import static org.mockito.Mockito.verify; 026import static org.mockito.Mockito.when; 027 028import java.util.ArrayList; 029import java.util.List; 030import java.util.function.Function; 031import org.apache.hadoop.hbase.HBaseClassTestRule; 032import org.apache.hadoop.hbase.hbtop.screen.ScreenView; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.junit.Before; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038import org.junit.runner.RunWith; 039import org.mockito.InOrder; 040import org.mockito.Mock; 041import org.mockito.runners.MockitoJUnitRunner; 042 043 044@Category(SmallTests.class) 045@RunWith(MockitoJUnitRunner.class) 046public class InputModeScreenPresenterTest { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(InputModeScreenPresenterTest.class); 051 052 private static final String TEST_INPUT_MESSAGE = "test input message"; 053 054 @Mock 055 private InputModeScreenView inputModeScreenView; 056 057 @Mock 058 private TopScreenView topScreenView; 059 060 @Mock 061 private Function<String, ScreenView> resultListener; 062 063 private InputModeScreenPresenter inputModeScreenPresenter; 064 065 @Before 066 public void setup() { 067 List<String> histories = new ArrayList<>(); 068 histories.add("history1"); 069 histories.add("history2"); 070 071 inputModeScreenPresenter = new InputModeScreenPresenter(inputModeScreenView, 072 TEST_INPUT_MESSAGE, histories, resultListener); 073 } 074 075 @Test 076 public void testInit() { 077 inputModeScreenPresenter.init(); 078 079 verify(inputModeScreenView).showInput(eq(TEST_INPUT_MESSAGE), eq(""), eq(0)); 080 } 081 082 @Test 083 public void testCharacter() { 084 inputModeScreenPresenter.character('a'); 085 inputModeScreenPresenter.character('b'); 086 inputModeScreenPresenter.character('c'); 087 088 InOrder inOrder = inOrder(inputModeScreenView); 089 inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1)); 090 inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2)); 091 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3)); 092 } 093 094 @Test 095 public void testArrowLeftAndRight() { 096 inputModeScreenPresenter.character('a'); 097 inputModeScreenPresenter.character('b'); 098 inputModeScreenPresenter.character('c'); 099 inputModeScreenPresenter.arrowLeft(); 100 inputModeScreenPresenter.arrowLeft(); 101 inputModeScreenPresenter.arrowLeft(); 102 inputModeScreenPresenter.arrowLeft(); 103 inputModeScreenPresenter.arrowRight(); 104 inputModeScreenPresenter.arrowRight(); 105 inputModeScreenPresenter.arrowRight(); 106 inputModeScreenPresenter.arrowRight(); 107 108 InOrder inOrder = inOrder(inputModeScreenView); 109 inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1)); 110 inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2)); 111 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3)); 112 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(2)); 113 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(1)); 114 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(0)); 115 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(1)); 116 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(2)); 117 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3)); 118 } 119 120 @Test 121 public void testHomeAndEnd() { 122 inputModeScreenPresenter.character('a'); 123 inputModeScreenPresenter.character('b'); 124 inputModeScreenPresenter.character('c'); 125 inputModeScreenPresenter.home(); 126 inputModeScreenPresenter.home(); 127 inputModeScreenPresenter.end(); 128 inputModeScreenPresenter.end(); 129 130 InOrder inOrder = inOrder(inputModeScreenView); 131 inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1)); 132 inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2)); 133 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3)); 134 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(0)); 135 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3)); 136 } 137 138 @Test 139 public void testBackspace() { 140 inputModeScreenPresenter.character('a'); 141 inputModeScreenPresenter.character('b'); 142 inputModeScreenPresenter.character('c'); 143 inputModeScreenPresenter.backspace(); 144 inputModeScreenPresenter.backspace(); 145 inputModeScreenPresenter.backspace(); 146 inputModeScreenPresenter.backspace(); 147 148 InOrder inOrder = inOrder(inputModeScreenView); 149 inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1)); 150 inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2)); 151 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3)); 152 inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2)); 153 inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1)); 154 inOrder.verify(inputModeScreenView).showInput(any(), eq(""), eq(0)); 155 } 156 157 @Test 158 public void testDelete() { 159 inputModeScreenPresenter.character('a'); 160 inputModeScreenPresenter.character('b'); 161 inputModeScreenPresenter.character('c'); 162 inputModeScreenPresenter.delete(); 163 inputModeScreenPresenter.arrowLeft(); 164 inputModeScreenPresenter.delete(); 165 166 InOrder inOrder = inOrder(inputModeScreenView); 167 inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1)); 168 inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2)); 169 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3)); 170 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(2)); 171 inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2)); 172 } 173 174 @Test 175 public void testHistories() { 176 inputModeScreenPresenter.character('a'); 177 inputModeScreenPresenter.character('b'); 178 inputModeScreenPresenter.character('c'); 179 inputModeScreenPresenter.arrowUp(); 180 inputModeScreenPresenter.arrowUp(); 181 inputModeScreenPresenter.arrowUp(); 182 inputModeScreenPresenter.arrowDown(); 183 inputModeScreenPresenter.arrowDown(); 184 inputModeScreenPresenter.arrowDown(); 185 186 InOrder inOrder = inOrder(inputModeScreenView); 187 inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1)); 188 inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2)); 189 inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3)); 190 inOrder.verify(inputModeScreenView).showInput(any(), eq("history2"), eq(8)); 191 inOrder.verify(inputModeScreenView).showInput(any(), eq("history1"), eq(8)); 192 inOrder.verify(inputModeScreenView).showInput(any(), eq("history2"), eq(8)); 193 } 194 195 @Test 196 public void testReturnToTopScreen() { 197 when(resultListener.apply(any())).thenReturn(topScreenView); 198 199 inputModeScreenPresenter.character('a'); 200 inputModeScreenPresenter.character('b'); 201 inputModeScreenPresenter.character('c'); 202 203 assertThat(inputModeScreenPresenter.returnToNextScreen(), is(topScreenView)); 204 verify(resultListener).apply(eq("abc")); 205 } 206}