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