aboutsummaryrefslogtreecommitdiff
path: root/tests/c2_e2e_test/src/org/chromium/c2/test/E2eTestActivity.java
blob: 140ff825e4bd9d6448188e663dd08547f39f9f71 (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
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
/*
 * Copyright 2019 The Chromium OS 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.c2.test;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.LinearLayout;

import java.util.concurrent.CountDownLatch;

/** Activity responsible for running the native Codec2.0 E2E tests. */
public class E2eTestActivity extends Activity implements SurfaceHolder.Callback {

    public final String TAG = "E2eTestActivity";

    private SurfaceView mSurfaceView;
    private Size mSize;

    private boolean mSurfaceCreated = false;
    private boolean mCanStartTest = false;
    private Size mExpectedSize;
    private CountDownLatch mLatch;

    private long mDecoderPtr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.loadLibrary("codectest");

        setContentView(R.layout.main_activity);
        mSurfaceView = (SurfaceView) findViewById(R.id.surface);

        mSurfaceView.getHolder().addCallback(this);

        mCanStartTest = !getIntent().getBooleanExtra("delay-start", false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // gtest can't reuse a process
        System.exit(0);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mSurfaceCreated = true;
        maybeStartTest();
    }

    private void maybeStartTest() {
        if (!mSurfaceCreated || !mCanStartTest) {
            return;
        }
        boolean encode = getIntent().getBooleanExtra("do-encode", false);
        String[] testArgs =
                getIntent().getStringArrayExtra("test-args") != null
                        ? getIntent().getStringArrayExtra("test-args")
                        : new String[0];
        String logFile = getIntent().getStringExtra("log-file");

        AsyncTask.execute(
                new Runnable() {
                    @Override
                    public void run() {
                        int res =
                                c2VideoTest(
                                        encode,
                                        testArgs,
                                        testArgs.length,
                                        mSurfaceView.getHolder().getSurface(),
                                        logFile);
                        Log.i(TAG, "Test returned result code " + res);

                        new Handler(Looper.getMainLooper())
                                .post(
                                        new Runnable() {
                                            @Override
                                            public void run() {
                                                finish();
                                            }
                                        });
                    }
                });
    }

    void onDecoderReady(long decoderPtr) {
        synchronized (this) {
            mDecoderPtr = decoderPtr;
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        if (intent.getAction().equals("org.chromium.c2.test.START_TEST")) {
            mCanStartTest = true;
            maybeStartTest();
            return;
        }

        synchronized (this) {
            if (mDecoderPtr != 0) {
                stopDecoderLoop(mDecoderPtr);
            }
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        synchronized (this) {
            mSize = new Size(width, height);
            if (mLatch != null && mExpectedSize.equals(mSize)) {
                mLatch.countDown();
            }
        }
    }

    // Configures the SurfaceView with the dimensions of the video the test is currently playing.
    void onSizeChanged(int width, int height) {
        synchronized (this) {
            mExpectedSize = new Size(width, height);
            if (mSize != null && mSize.equals(mExpectedSize)) {
                return;
            }
            mLatch = new CountDownLatch(1);
        }

        new Handler(Looper.getMainLooper())
                .post(
                        new Runnable() {
                            @Override
                            public void run() {
                                mSurfaceView.setLayoutParams(
                                        new LinearLayout.LayoutParams(width, height));
                            }
                        });

        try {
            mLatch.await();
        } catch (Exception e) {
        }
        synchronized (this) {
            mLatch = null;
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (!isFinishing()) {
            throw new RuntimeException("Surface destroyed during test");
        }
    }

    public native int c2VideoTest(
            boolean encode, String[] testArgs, int testArgsCount, Surface surface, String tmpFile);

    public native void stopDecoderLoop(long decoderPtr);
}