1 /*
2  * Copyright (C) 2017 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 
17 package com.android.settings.widget;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyBoolean;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 import static org.robolectric.RuntimeEnvironment.application;
28 
29 import android.graphics.Rect;
30 import android.widget.FrameLayout;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.robolectric.Robolectric;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.Shadows;
38 import org.robolectric.shadows.ShadowView;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class ScrollToParentEditTextTest {
42 
43     private static final int EDIT_TEXT_SIZE = 20;
44     private static final int PARENT_SIZE = 50;
45     private static final int SCROLL_RECT_SIZE = 30;
46 
47     private ScrollToParentEditText mEditText;
48     private FrameLayout mParent;
49 
50     @Before
setUp()51     public void setUp() {
52         mEditText =
53             new ScrollToParentEditText(application, Robolectric.buildAttributeSet().build());
54         mEditText.layout(0, 0, EDIT_TEXT_SIZE, EDIT_TEXT_SIZE);
55 
56         mParent = spy(new FrameLayout(application));
57         mParent.layout(0, 0, PARENT_SIZE, PARENT_SIZE);
58 
59         doReturn(true).when(mParent).requestRectangleOnScreen(any(Rect.class), anyBoolean());
60     }
61 
62     @Test
requestRectangleOnScreen_noParent_shouldScrollToItself()63     public void requestRectangleOnScreen_noParent_shouldScrollToItself() {
64         assertThat(mEditText.requestRectangleOnScreen(
65                 new Rect(0, 0, SCROLL_RECT_SIZE, SCROLL_RECT_SIZE), true)).isFalse();
66     }
67 
68     @Test
requestRectangleOnScreen_withParent_shouldScrollToParent()69     public void requestRectangleOnScreen_withParent_shouldScrollToParent() {
70         ShadowView shadowEditText = Shadows.shadowOf(mEditText);
71         shadowEditText.setMyParent(mParent);
72 
73         assertThat(mEditText.requestRectangleOnScreen(
74                 new Rect(0, 0, SCROLL_RECT_SIZE, SCROLL_RECT_SIZE), true)).isTrue();
75         verify(mParent)
76                 .requestRectangleOnScreen(eq(new Rect(0, 0, PARENT_SIZE, PARENT_SIZE)), eq(true));
77     }
78 }
79