Home | History | Annotate | Download | only in cts
      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 package android.ui.cts;
     17 
     18 import android.content.Context;
     19 import android.content.res.Configuration;
     20 import android.content.res.Resources;
     21 import android.content.res.TypedArray;
     22 import android.support.test.InstrumentationRegistry;
     23 import android.support.test.runner.AndroidJUnit4;
     24 import android.test.suitebuilder.annotation.SmallTest;
     25 import android.util.DisplayMetrics;
     26 import junit.framework.Assert;
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 
     31 @RunWith(AndroidJUnit4.class)
     32 @SmallTest
     33 public class WatchPercentageScreenDimenTest {
     34 
     35     private Context mContext;
     36     private Configuration mConfig;
     37     private float mScreenWidth;
     38     private DisplayMetrics mDisplayMetrics;
     39 
     40     private boolean isRoundWatch() {
     41         return mConfig.isScreenRound() && (mConfig.uiMode & Configuration.UI_MODE_TYPE_WATCH)
     42                 == Configuration.UI_MODE_TYPE_WATCH;
     43     }
     44 
     45     @Before
     46     public void setUp() throws Exception {
     47         mContext = InstrumentationRegistry.getTargetContext();
     48         mConfig = mContext.getResources().getConfiguration();
     49         mDisplayMetrics = mContext.getResources().getDisplayMetrics();
     50         mScreenWidth = mDisplayMetrics.widthPixels;
     51     }
     52 
     53     @Test
     54     public void test_10() {
     55         if (!isRoundWatch()) {
     56             return; // skip if not round watch
     57         }
     58 
     59         float expected = mScreenWidth * 0.1f;
     60         float expectedDelta = getMaxErrorRatio() * expected;
     61 
     62         TypedArray attrs = mContext.obtainStyledAttributes(new int[] {
     63             android.R.attr.listPreferredItemPaddingEnd
     64         });
     65         Assert.assertEquals("invalid number of attributes", 1, attrs.length());
     66 
     67         for (int i = 0; i < attrs.length(); ++i) {
     68             float actual = attrs.getDimension(i, -1);
     69             Assert.assertEquals("screen_percentage_10 is not 10% of screen width",
     70                     expected, actual, expectedDelta + 0.01f);
     71         }
     72     }
     73 
     74     @Test
     75     public void test_15() {
     76         if (!isRoundWatch()) {
     77             return; // skip if not round watch
     78         }
     79 
     80         float expected = mScreenWidth * 0.15f;
     81         float expectedDelta = getMaxErrorRatio() * expected;
     82 
     83         TypedArray attrs = mContext.obtainStyledAttributes(new int[] {
     84             android.R.attr.dialogPreferredPadding,
     85             android.R.attr.listPreferredItemPaddingLeft,
     86             android.R.attr.listPreferredItemPaddingRight,
     87             android.R.attr.listPreferredItemPaddingStart
     88         });
     89         Assert.assertEquals("invalid number of attributes", 4, attrs.length());
     90 
     91         for (int i = 0; i < attrs.length(); ++i) {
     92             float actual = attrs.getDimension(i, -1);
     93             Assert.assertEquals("screen_percentage_15 is not 15% of screen width",
     94                     expected, actual, expectedDelta + 0.01f);
     95         }
     96     }
     97 
     98     private float getMaxErrorRatio() {
     99         // The size used will be the closest qualifier with width <= device width, so there may be
    100         // small rounding errors.
    101         float widthDp = mDisplayMetrics.widthPixels / mDisplayMetrics.density;
    102         return (widthDp - (float) Math.floor(widthDp)) / widthDp;
    103     }
    104 }
    105