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