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 android.content.Context;
     22 import android.view.View;
     23 
     24 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     25 
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 import org.robolectric.RuntimeEnvironment;
     30 import org.robolectric.util.ReflectionHelpers;
     31 
     32 @RunWith(SettingsRobolectricTestRunner.class)
     33 public class AspectRatioFrameLayoutTest {
     34 
     35     private Context mContext;
     36     private AspectRatioFrameLayout mLayout;
     37 
     38     @Before
     39     public void setup() {
     40         mContext = RuntimeEnvironment.application;
     41     }
     42 
     43     @Test
     44     public void measure_squareAspectRatio_squeezeWidth() {
     45         mLayout = new AspectRatioFrameLayout(mContext);
     46 
     47         int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
     48         int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(50, View.MeasureSpec.EXACTLY);
     49 
     50         mLayout.measure(widthMeasureSpec, heightMeasureSpec);
     51 
     52         assertThat(mLayout.getMeasuredWidth()).isEqualTo(50);
     53         assertThat(mLayout.getMeasuredHeight()).isEqualTo(50);
     54     }
     55 
     56     @Test
     57     public void measure_squareAspectRatio_stretchWidth() {
     58         mLayout = new AspectRatioFrameLayout(mContext);
     59 
     60         int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(50, View.MeasureSpec.EXACTLY);
     61         int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
     62 
     63         mLayout.measure(widthMeasureSpec, heightMeasureSpec);
     64 
     65         assertThat(mLayout.getMeasuredWidth()).isEqualTo(100);
     66         assertThat(mLayout.getMeasuredHeight()).isEqualTo(100);
     67     }
     68 
     69     @Test
     70     public void measure_squareAspectRatio_doNotStretch() {
     71         mLayout = new AspectRatioFrameLayout(mContext);
     72 
     73         int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
     74         int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
     75 
     76         mLayout.measure(widthMeasureSpec, heightMeasureSpec);
     77 
     78         assertThat(mLayout.getMeasuredWidth()).isEqualTo(100);
     79         assertThat(mLayout.getMeasuredHeight()).isEqualTo(100);
     80     }
     81 
     82     @Test
     83     public void measure_rectangleAspectRatio_stretch() {
     84         mLayout = new AspectRatioFrameLayout(mContext);
     85         // Set aspect ratio to 2:1.
     86         ReflectionHelpers.setField(mLayout, "mAspectRatio", 2f);
     87 
     88         int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
     89         int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
     90 
     91         mLayout.measure(widthMeasureSpec, heightMeasureSpec);
     92 
     93         // Should stretch width/height to 2:1 ratio
     94         assertThat(mLayout.getMeasuredWidth()).isEqualTo(200);
     95         assertThat(mLayout.getMeasuredHeight()).isEqualTo(100);
     96     }
     97 }
     98