1/*
2 * Copyright (C) 2013 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 */
16package androidx.media.filterfw;
17
18import android.os.Handler;
19import android.os.HandlerThread;
20import android.test.AndroidTestCase;
21
22import junit.framework.TestCase;
23
24import java.util.concurrent.Callable;
25import java.util.concurrent.FutureTask;
26
27/**
28 * A {@link TestCase} for testing objects requiring {@link MffContext}. This test case can only be
29 * used to test the functionality that does not rely on GL support and camera.
30 */
31public class MffTestCase extends AndroidTestCase {
32
33    private HandlerThread mMffContextHandlerThread;
34    private MffContext mMffContext;
35
36    @Override
37    protected void setUp() throws Exception {
38        super.setUp();
39        // MffContext needs to be created on a separate thread to allow MFF to post Runnable's.
40        mMffContextHandlerThread = new HandlerThread("MffContextThread");
41        mMffContextHandlerThread.start();
42        Handler handler = new Handler(mMffContextHandlerThread.getLooper());
43        FutureTask<MffContext> task = new FutureTask<MffContext>(new Callable<MffContext>() {
44            @Override
45            public MffContext call() throws Exception {
46                MffContext.Config config = new MffContext.Config();
47                config.requireCamera = false;
48                config.requireOpenGL = false;
49                config.forceNoGL = true;
50                return new MffContext(getContext(), config);
51            }
52        });
53        handler.post(task);
54        // Wait for the context to be created on the handler thread.
55        mMffContext = task.get();
56    }
57
58    @Override
59    protected void tearDown() throws Exception {
60        mMffContextHandlerThread.getLooper().quit();
61        mMffContextHandlerThread = null;
62        mMffContext.release();
63        mMffContext = null;
64        super.tearDown();
65    }
66
67    protected MffContext getMffContext() {
68        return mMffContext;
69    }
70
71}
72