1 /*
2  * Copyright (C) 2018 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.autofillservice.cts.testcore;
17 
18 import android.graphics.Canvas;
19 import android.graphics.ColorFilter;
20 import android.graphics.Rect;
21 import android.graphics.drawable.Drawable;
22 import android.util.Log;
23 
24 import com.android.compatibility.common.util.RetryableException;
25 
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.TimeUnit;
28 
29 public class MyDrawable extends Drawable {
30 
31     private static final String TAG = "MyDrawable";
32 
33     private static CountDownLatch sLatch;
34     private static MyDrawable sInstance;
35 
36     private static Rect sAutofilledBounds;
37 
MyDrawable()38     public MyDrawable() {
39         if (sInstance != null) {
40             throw new IllegalStateException("There can be only one!");
41         }
42         sInstance = this;
43     }
44 
45     @Override
draw(Canvas canvas)46     public void draw(Canvas canvas) {
47         if (sInstance != null && sAutofilledBounds == null) {
48             sAutofilledBounds = new Rect(getBounds());
49             Log.d(TAG, "Autofilled at " + sAutofilledBounds);
50             sLatch.countDown();
51         }
52     }
53 
getAutofilledBounds()54     public static Rect getAutofilledBounds() throws InterruptedException {
55         if (sLatch == null) {
56             throw new AssertionError("sLatch should be not null");
57         }
58 
59         if (!sLatch.await(Timeouts.FILL_TIMEOUT.ms(), TimeUnit.MILLISECONDS)) {
60             throw new RetryableException(Timeouts.FILL_TIMEOUT, "custom drawable not drawn");
61         }
62         return sAutofilledBounds;
63     }
64 
65     /**
66      * Asserts the custom drawable is not drawn.
67      */
assertDrawableNotDrawn()68     public static void assertDrawableNotDrawn() throws Exception {
69         if (sLatch == null) {
70             throw new AssertionError("sLatch should be not null");
71         }
72 
73         if (sLatch.await(Timeouts.DRAWABLE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
74             throw new AssertionError("custom drawable is drawn");
75         }
76     }
77 
initStatus()78     public static void initStatus() {
79         sLatch = new CountDownLatch(1);
80         sInstance = null;
81         sAutofilledBounds = null;
82     }
83 
clearStatus()84     public static void clearStatus() {
85         sLatch = null;
86         sInstance = null;
87         sAutofilledBounds = null;
88     }
89 
90     @Override
setAlpha(int alpha)91     public void setAlpha(int alpha) {
92     }
93 
94     @Override
setColorFilter(ColorFilter colorFilter)95     public void setColorFilter(ColorFilter colorFilter) {
96     }
97 
98     @Override
getOpacity()99     public int getOpacity() {
100         return 0;
101     }
102 }
103