-
Notifications
You must be signed in to change notification settings - Fork 229
/
ScreenshotImpl.java
356 lines (308 loc) · 11.2 KB
/
ScreenshotImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.testing.screenshot.internal;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import com.facebook.testing.screenshot.WindowAttachment;
import com.facebook.testing.screenshot.layouthierarchy.AccessibilityHierarchyDumper;
import com.facebook.testing.screenshot.layouthierarchy.AccessibilityIssuesDumper;
import com.facebook.testing.screenshot.layouthierarchy.AccessibilityUtil;
import com.facebook.testing.screenshot.layouthierarchy.LayoutHierarchyDumper;
import java.io.IOException;
import java.util.Locale;
import java.util.concurrent.Callable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Implementation for Screenshot class.
*
* <p>The Screenshot class has static methods, because that's how the API should look like, this
* class has all its implementation for testability.
*
* <p>This is public only for implementation convenient for using UiThreadHelper.
*/
public class ScreenshotImpl {
/**
* The version of the metadata file generated. This should be bumped whenever the structure of the
* metadata file changes in such a way that would cause a comparison between old and new files to
* be invalid or not useful.
*/
private static final int METADATA_VERSION = 1;
private static ScreenshotImpl sInstance;
/** The album of all the screenshots taken in this run. */
private final Album mAlbum;
private int mTileSize = 512;
private Bitmap mBitmap = null;
private Canvas mCanvas = null;
private boolean mEnableBitmapReconfigure = Build.VERSION.SDK_INT >= 19;
ScreenshotImpl(Album album) {
mAlbum = album;
}
/**
* Factory method that creates this instance based on what arguments are passed to the
* instrumentation
*/
private static ScreenshotImpl create(Context context) {
Album album = AlbumImpl.create(context, "default");
album.cleanup();
return new ScreenshotImpl(album);
}
/** Get a singleton instance of the ScreenshotImpl */
public static ScreenshotImpl getInstance() {
if (sInstance != null) {
return sInstance;
}
synchronized (ScreenshotImpl.class) {
if (sInstance != null) {
return sInstance;
}
Instrumentation instrumentation = Registry.getRegistry().instrumentation;
sInstance = create(instrumentation.getContext());
return sInstance;
}
}
/**
* Check if getInstance() has ever been called.
*
* <p>This is for a minor optimization to avoid creating a ScreenshotImpl at onDestroy() if it was
* never called during the run.
*/
public static boolean hasBeenCreated() {
return sInstance != null;
}
// VisibleForTesting
void setEnableBitmapReconfigure(boolean enableBitmapReconfigure) {
mEnableBitmapReconfigure = enableBitmapReconfigure;
}
public int getTileSize() {
return mTileSize;
}
public void setTileSize(int tileSize) {
mTileSize = tileSize;
mBitmap = null;
mCanvas = null;
}
/** Snaps a screenshot of the activity using the testName as the name. */
public RecordBuilderImpl snapActivity(final Activity activity) {
if (!isUiThread()) {
return runCallableOnUiThread(
new Callable<RecordBuilderImpl>() {
@Override
public RecordBuilderImpl call() {
return snapActivity(activity);
}
})
.setTestClass(TestNameDetector.getTestClass())
.setTestName(TestNameDetector.getTestName());
}
View rootView = activity.getWindow().getDecorView();
return snap(rootView);
}
/**
* Snaps a screenshot of the view (which should already be measured and layouted) using testName
* as the name.
*/
public RecordBuilderImpl snap(final View measuredView) {
RecordBuilderImpl recordBuilder =
new RecordBuilderImpl(this)
.setView(measuredView)
.setTestClass(TestNameDetector.getTestClass())
.setTestName(TestNameDetector.getTestName());
return recordBuilder;
}
// VisibleForTesting
public void flush() {
mAlbum.flush();
}
private void storeBitmap(final RecordBuilderImpl recordBuilder) {
if (recordBuilder.getTiling().getAt(0, 0) != null || recordBuilder.getError() != null) {
return;
}
if (!isUiThread()) {
runCallableOnUiThread(
new Callable<Void>() {
@Override
public Void call() {
storeBitmap(recordBuilder);
return null;
}
});
return;
}
View measuredView = recordBuilder.getView();
if (measuredView.getMeasuredHeight() == 0 || measuredView.getMeasuredWidth() == 0) {
throw new RuntimeException("Can't take a screenshot, since this view is not measured");
}
WindowAttachment.Detacher detacher = WindowAttachment.dispatchAttach(measuredView);
try {
int width = measuredView.getWidth();
int height = measuredView.getHeight();
assertNotTooLarge(width, height, recordBuilder);
int maxi = (width + mTileSize - 1) / mTileSize;
int maxj = (height + mTileSize - 1) / mTileSize;
recordBuilder.setTiling(new Tiling(maxi, maxj));
for (int i = 0; i < maxi; i++) {
for (int j = 0; j < maxj; j++) {
drawTile(measuredView, i, j, recordBuilder);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
detacher.detach();
}
}
private static void assertNotTooLarge(int width, int height, RecordBuilderImpl recordBuilder) {
final long maxPixels = recordBuilder.getMaxPixels();
if (maxPixels <= 0) {
return;
}
if (((long) width) * height > maxPixels) {
throw new IllegalStateException(
String.format(Locale.US, "View too large: (%d, %d)", width, height));
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void drawTile(View measuredView, int i, int j, RecordBuilderImpl recordBuilder)
throws IOException {
int width = measuredView.getWidth();
int height = measuredView.getHeight();
int left = i * mTileSize;
int top = j * mTileSize;
int right = Math.min(left + mTileSize, width);
int bottom = Math.min(top + mTileSize, height);
lazyInitBitmap();
if (mEnableBitmapReconfigure) {
mBitmap.reconfigure(right - left, bottom - top, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
clearCanvas(mCanvas);
drawClippedView(measuredView, left, top, mCanvas);
String tempName = mAlbum.writeBitmap(recordBuilder.getName(), i, j, mBitmap);
if (tempName == null) {
throw new NullPointerException();
}
recordBuilder.getTiling().setAt(left / mTileSize, top / mTileSize, tempName);
}
private void lazyInitBitmap() {
if (mBitmap != null) {
return;
}
mBitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
private void clearCanvas(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC);
}
/**
* Draw a part of the view, in particular it returns a bitmap of dimensions <code>
* (right-left)*(bottom-top)</code>, with the rendering of the view starting from position (<code>
* left</code>, <code>top</code>).
*
* <p>For well behaved views, calling this repeatedly shouldn't change the rendering, so it should
* it okay to render each tile one by one and combine it later.
*/
private void drawClippedView(View view, int left, int top, Canvas canvas) {
canvas.translate(-left, -top);
view.draw(canvas);
canvas.translate(left, top);
}
/** Records the RecordBuilderImpl, and verifies if required */
public void record(RecordBuilderImpl recordBuilder) {
storeBitmap(recordBuilder);
try {
JSONObject dump = new JSONObject();
JSONObject viewDump = LayoutHierarchyDumper.create().dumpHierarchy(recordBuilder.getView());
AccessibilityUtil.AXTreeNode axTree =
AccessibilityUtil.generateAccessibilityTree(recordBuilder.getView(), null);
JSONObject axHierarchyDump = AccessibilityHierarchyDumper.dumpHierarchy(axTree);
dump.put("viewHierarchy", viewDump);
dump.put("axHierarchy", axHierarchyDump);
dump.put("version", METADATA_VERSION);
mAlbum.writeViewHierarchyFile(recordBuilder.getName(), dump.toString(2));
JSONObject issues = new JSONObject();
JSONArray axIssuesDump = AccessibilityIssuesDumper.dumpIssues(axTree);
issues.put("axIssues", axIssuesDump);
mAlbum.writeAxIssuesFile(recordBuilder.getName(), issues.toString(2));
mAlbum.addRecord(recordBuilder);
} catch (IOException | JSONException e) {
throw new RuntimeException(e);
}
}
Bitmap getBitmap(RecordBuilderImpl recordBuilder) {
if (recordBuilder.getTiling().getAt(0, 0) != null) {
throw new IllegalArgumentException("can't call getBitmap() after record()");
}
View view = recordBuilder.getView();
Bitmap bmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
WindowAttachment.Detacher detacher = WindowAttachment.dispatchAttach(recordBuilder.getView());
try {
drawClippedView(view, 0, 0, new Canvas(bmp));
} finally {
detacher.detach();
}
return bmp;
}
private boolean isUiThread() {
return Looper.getMainLooper().getThread() == Thread.currentThread();
}
private <T> T runCallableOnUiThread(final Callable<T> callable) {
final T[] ret = (T[]) new Object[1];
final Exception[] e = new Exception[1];
final Object lock = new Object();
Handler handler = new Handler(Looper.getMainLooper());
synchronized (lock) {
handler.post(
new Runnable() {
@Override
public void run() {
try {
ret[0] = callable.call();
} catch (Exception ee) {
e[0] = ee;
}
synchronized (lock) {
lock.notifyAll();
}
}
});
try {
lock.wait();
} catch (InterruptedException ee) {
throw new RuntimeException(ee);
}
}
if (e[0] != null) {
throw new RuntimeException(e[0]);
}
return ret[0];
}
/** @return The largest amount of pixels we'll capture, otherwise an exception will be thrown. */
public static long getMaxPixels() {
return RecordBuilderImpl.DEFAULT_MAX_PIXELS;
}
}