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