1 /*
2  * Copyright (C) 2022 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 android.view.cts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.Manifest;
22 import android.app.Activity;
23 import android.platform.test.annotations.AppModeSdkSandbox;
24 import android.util.DisplayMetrics;
25 import android.util.TypedValue;
26 import android.view.View;
27 
28 import androidx.test.filters.MediumTest;
29 import androidx.test.platform.app.InstrumentationRegistry;
30 import androidx.test.rule.ActivityTestRule;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import com.android.compatibility.common.util.AdoptShellPermissionsRule;
34 
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 
39 
40 @MediumTest
41 @RunWith(AndroidJUnit4.class)
42 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).")
43 public class HandwritingBoundsOffsetTest {
44 
45     @Rule(order = 0)
46     public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule(
47             InstrumentationRegistry.getInstrumentation().getUiAutomation(),
48             Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX);
49 
50     @Rule(order = 1)
51     public ActivityTestRule<HandwritingActivity> mActivityRule =
52             new ActivityTestRule<>(HandwritingActivity.class);
53 
54     @Test
handwritingBoundsOffset_viewDefaultValue()55     public void handwritingBoundsOffset_viewDefaultValue() {
56         final Activity activity = mActivityRule.getActivity();
57         final View view = activity.findViewById(R.id.default_view);
58 
59         // The default value for handwriting bounds offset is: 10dp for the left and right edge and
60         // 40dp for the top and bottom edge.
61         assertThat(view.getHandwritingBoundsOffsetLeft()).isZero();
62         assertThat(view.getHandwritingBoundsOffsetRight()).isZero();
63         assertThat(view.getHandwritingBoundsOffsetTop()).isZero();
64         assertThat(view.getHandwritingBoundsOffsetBottom()).isZero();
65     }
66 
67     @Test
handwritingBoundsOffset_textViewDefaultValue()68     public void handwritingBoundsOffset_textViewDefaultValue() {
69         final Activity activity = mActivityRule.getActivity();
70         final View view = activity.findViewById(R.id.default_textview);
71 
72         final DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
73         // The default value for handwriting bounds offset is: 10dp for the left and right edge and
74         // 40dp for the top and bottom edge.
75         assertThat(view.getHandwritingBoundsOffsetLeft()).isEqualTo(
76                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, displayMetrics));
77         assertThat(view.getHandwritingBoundsOffsetRight()).isEqualTo(
78                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, displayMetrics));
79 
80         assertThat(view.getHandwritingBoundsOffsetTop()).isEqualTo(
81                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, displayMetrics));
82         assertThat(view.getHandwritingBoundsOffsetBottom()).isEqualTo(
83                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, displayMetrics));
84     }
85 
86     @Test
handwritingBoundsOffset_editTextDefaultValue()87     public void handwritingBoundsOffset_editTextDefaultValue() {
88         final Activity activity = mActivityRule.getActivity();
89         final View view = activity.findViewById(R.id.default_edittext);
90 
91         final DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
92         // The default value for handwriting bounds offset is: 10dp for the left and right edge and
93         // 40dp for the top and bottom edge.
94         assertThat(view.getHandwritingBoundsOffsetLeft()).isEqualTo(
95                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, displayMetrics));
96         assertThat(view.getHandwritingBoundsOffsetRight()).isEqualTo(
97                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, displayMetrics));
98 
99         assertThat(view.getHandwritingBoundsOffsetTop()).isEqualTo(
100                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, displayMetrics));
101         assertThat(view.getHandwritingBoundsOffsetBottom()).isEqualTo(
102                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, displayMetrics));
103     }
104 
105     @Test
handwritingBoundsOffset_setThoughXml()106     public void handwritingBoundsOffset_setThoughXml() {
107         final Activity activity = mActivityRule.getActivity();
108         final View view = activity.findViewById(R.id.handwriting_bounds_offset);
109 
110         assertThat(view.getHandwritingBoundsOffsetLeft()).isEqualTo(5);
111         assertThat(view.getHandwritingBoundsOffsetTop()).isEqualTo(10);
112         assertThat(view.getHandwritingBoundsOffsetRight()).isEqualTo(15);
113         assertThat(view.getHandwritingBoundsOffsetBottom()).isEqualTo(20);
114     }
115 
116     @Test
setHandwritingBoundsOffsets()117     public void setHandwritingBoundsOffsets() {
118         final Activity activity = mActivityRule.getActivity();
119         final View view = activity.findViewById(R.id.default_view);
120 
121         view.setHandwritingBoundsOffsets(1, 2, 3, 4);
122         assertThat(view.getHandwritingBoundsOffsetLeft()).isEqualTo(1);
123         assertThat(view.getHandwritingBoundsOffsetTop()).isEqualTo(2);
124         assertThat(view.getHandwritingBoundsOffsetRight()).isEqualTo(3);
125         assertThat(view.getHandwritingBoundsOffsetBottom()).isEqualTo(4);
126     }
127 }
128