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.hamcrest.MatcherAssert.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.junit.MockitoJUnitRunner;
042
043@Category(SmallTests.class)
044@RunWith(MockitoJUnitRunner.class)
045public class TestInputModeScreenPresenter {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestInputModeScreenPresenter.class);
050
051  private static final String TEST_INPUT_MESSAGE = "test input message";
052
053  @Mock
054  private InputModeScreenView inputModeScreenView;
055
056  @Mock
057  private TopScreenView topScreenView;
058
059  @Mock
060  private Function<String, ScreenView> resultListener;
061
062  private InputModeScreenPresenter inputModeScreenPresenter;
063
064  @Before
065  public void setup() {
066    List<String> histories = new ArrayList<>();
067    histories.add("history1");
068    histories.add("history2");
069
070    inputModeScreenPresenter = new InputModeScreenPresenter(inputModeScreenView, TEST_INPUT_MESSAGE,
071      histories, resultListener);
072  }
073
074  @Test
075  public void testInit() {
076    inputModeScreenPresenter.init();
077
078    verify(inputModeScreenView).showInput(eq(TEST_INPUT_MESSAGE), eq(""), eq(0));
079  }
080
081  @Test
082  public void testCharacter() {
083    inputModeScreenPresenter.character('a');
084    inputModeScreenPresenter.character('b');
085    inputModeScreenPresenter.character('c');
086
087    InOrder inOrder = inOrder(inputModeScreenView);
088    inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1));
089    inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2));
090    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3));
091  }
092
093  @Test
094  public void testArrowLeftAndRight() {
095    inputModeScreenPresenter.character('a');
096    inputModeScreenPresenter.character('b');
097    inputModeScreenPresenter.character('c');
098    inputModeScreenPresenter.arrowLeft();
099    inputModeScreenPresenter.arrowLeft();
100    inputModeScreenPresenter.arrowLeft();
101    inputModeScreenPresenter.arrowLeft();
102    inputModeScreenPresenter.arrowRight();
103    inputModeScreenPresenter.arrowRight();
104    inputModeScreenPresenter.arrowRight();
105    inputModeScreenPresenter.arrowRight();
106
107    InOrder inOrder = inOrder(inputModeScreenView);
108    inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1));
109    inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2));
110    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3));
111    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(2));
112    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(1));
113    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(0));
114    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(1));
115    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(2));
116    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3));
117  }
118
119  @Test
120  public void testHomeAndEnd() {
121    inputModeScreenPresenter.character('a');
122    inputModeScreenPresenter.character('b');
123    inputModeScreenPresenter.character('c');
124    inputModeScreenPresenter.home();
125    inputModeScreenPresenter.home();
126    inputModeScreenPresenter.end();
127    inputModeScreenPresenter.end();
128
129    InOrder inOrder = inOrder(inputModeScreenView);
130    inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1));
131    inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2));
132    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3));
133    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(0));
134    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3));
135  }
136
137  @Test
138  public void testBackspace() {
139    inputModeScreenPresenter.character('a');
140    inputModeScreenPresenter.character('b');
141    inputModeScreenPresenter.character('c');
142    inputModeScreenPresenter.backspace();
143    inputModeScreenPresenter.backspace();
144    inputModeScreenPresenter.backspace();
145    inputModeScreenPresenter.backspace();
146
147    InOrder inOrder = inOrder(inputModeScreenView);
148    inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1));
149    inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2));
150    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3));
151    inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2));
152    inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1));
153    inOrder.verify(inputModeScreenView).showInput(any(), eq(""), eq(0));
154  }
155
156  @Test
157  public void testDelete() {
158    inputModeScreenPresenter.character('a');
159    inputModeScreenPresenter.character('b');
160    inputModeScreenPresenter.character('c');
161    inputModeScreenPresenter.delete();
162    inputModeScreenPresenter.arrowLeft();
163    inputModeScreenPresenter.delete();
164
165    InOrder inOrder = inOrder(inputModeScreenView);
166    inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1));
167    inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2));
168    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3));
169    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(2));
170    inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2));
171  }
172
173  @Test
174  public void testHistories() {
175    inputModeScreenPresenter.character('a');
176    inputModeScreenPresenter.character('b');
177    inputModeScreenPresenter.character('c');
178    inputModeScreenPresenter.arrowUp();
179    inputModeScreenPresenter.arrowUp();
180    inputModeScreenPresenter.arrowUp();
181    inputModeScreenPresenter.arrowDown();
182    inputModeScreenPresenter.arrowDown();
183    inputModeScreenPresenter.arrowDown();
184
185    InOrder inOrder = inOrder(inputModeScreenView);
186    inOrder.verify(inputModeScreenView).showInput(any(), eq("a"), eq(1));
187    inOrder.verify(inputModeScreenView).showInput(any(), eq("ab"), eq(2));
188    inOrder.verify(inputModeScreenView).showInput(any(), eq("abc"), eq(3));
189    inOrder.verify(inputModeScreenView).showInput(any(), eq("history2"), eq(8));
190    inOrder.verify(inputModeScreenView).showInput(any(), eq("history1"), eq(8));
191    inOrder.verify(inputModeScreenView).showInput(any(), eq("history2"), eq(8));
192  }
193
194  @Test
195  public void testReturnToTopScreen() {
196    when(resultListener.apply(any())).thenReturn(topScreenView);
197
198    inputModeScreenPresenter.character('a');
199    inputModeScreenPresenter.character('b');
200    inputModeScreenPresenter.character('c');
201
202    assertThat(inputModeScreenPresenter.returnToNextScreen(), is(topScreenView));
203    verify(resultListener).apply(eq("abc"));
204  }
205}