Home | History | Annotate | Download | only in widget
      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.Matchers.any;
     22 import static org.mockito.Matchers.anyBoolean;
     23 import static org.mockito.Matchers.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 import static org.robolectric.Shadows.shadowOf;
     29 
     30 import android.graphics.Rect;
     31 import android.widget.FrameLayout;
     32 
     33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     34 import com.android.settings.TestConfig;
     35 
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 import org.robolectric.Robolectric;
     40 import org.robolectric.annotation.Config;
     41 import org.robolectric.shadows.ShadowView;
     42 
     43 @RunWith(SettingsRobolectricTestRunner.class)
     44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     45 public 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