summaryrefslogtreecommitdiff
path: root/chromium/services/shape_detection/android/javatests/src/org/chromium/shape_detection/TestUtils.java
blob: afd00963bc1ca6845f40521ca7e22a11972c2cf6 (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.shape_detection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.gms.ChromiumPlayServicesAvailability;
import org.chromium.skia.mojom.ColorType;
import org.chromium.skia.mojom.ImageInfo;

import java.nio.ByteBuffer;

/**
 * Utility class for ShapeDetection instrumentation tests,
 * provides support for e.g. reading files and converting
 * Bitmaps to mojom.Bitmaps.
 */
public class TestUtils {
    public static final boolean IS_GMS_CORE_SUPPORTED = isGmsCoreSupported();

    private static boolean isGmsCoreSupported() {
        return ChromiumPlayServicesAvailability.isGooglePlayServicesAvailable(
                ContextUtils.getApplicationContext());
    }

    public static org.chromium.skia.mojom.Bitmap mojoBitmapFromBitmap(Bitmap bitmap) {
        ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(buffer);

        org.chromium.skia.mojom.Bitmap mojoBitmap = new org.chromium.skia.mojom.Bitmap();
        mojoBitmap.imageInfo = new ImageInfo();
        mojoBitmap.imageInfo.width = bitmap.getWidth();
        mojoBitmap.imageInfo.height = bitmap.getHeight();
        mojoBitmap.imageInfo.colorType = ColorType.RGBA_8888;
        mojoBitmap.pixelData = new org.chromium.mojo_base.mojom.BigBuffer();
        mojoBitmap.pixelData.setBytes(buffer.array());
        return mojoBitmap;
    }

    public static org.chromium.skia.mojom.Bitmap mojoBitmapFromFile(String relPath) {
        String path = UrlUtils.getIsolatedTestFilePath("services/test/data/" + relPath);
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        return mojoBitmapFromBitmap(bitmap);
    }

    public static org.chromium.skia.mojom.Bitmap mojoBitmapFromText(String[] texts) {
        final int x = 10;
        final int baseline = 100;

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(36.0f);
        paint.setTextAlign(Paint.Align.LEFT);

        Bitmap bitmap = Bitmap.createBitmap(1080, 480, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);

        for (int i = 0; i < texts.length; i++) {
            canvas.drawText(texts[i], x, baseline * (i + 1), paint);
        }

        return mojoBitmapFromBitmap(bitmap);
    }
}