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