1/*
2 * Copyright (C) 2010 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
17package com.android.mediaframeworktest.functional.audio;
18
19import com.android.mediaframeworktest.MediaFrameworkTest;
20import com.android.mediaframeworktest.MediaNames;
21import com.android.mediaframeworktest.functional.EnergyProbe;
22import android.content.Context;
23import android.content.res.AssetFileDescriptor;
24import android.media.audiofx.AudioEffect;
25import android.media.AudioManager;
26import android.media.audiofx.Virtualizer;
27import android.media.audiofx.Visualizer;
28import android.media.MediaPlayer;
29
30import android.os.Looper;
31import android.test.suitebuilder.annotation.LargeTest;
32import android.test.suitebuilder.annotation.MediumTest;
33import android.test.suitebuilder.annotation.Suppress;
34import android.test.ActivityInstrumentationTestCase2;
35import android.util.Log;
36
37import java.nio.ByteOrder;
38import java.nio.ByteBuffer;
39import java.util.UUID;
40
41/**
42 * Junit / Instrumentation test case for the media AudioTrack api
43
44 */
45public class MediaVirtualizerTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> {
46    private String TAG = "MediaVirtualizerTest";
47    private final static short TEST_STRENGTH = 500;
48
49    private Virtualizer mVirtualizer = null;
50    private int mSession = -1;
51
52    public MediaVirtualizerTest() {
53        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
54    }
55
56    @Override
57    protected void setUp() throws Exception {
58      super.setUp();
59    }
60
61    @Override
62    protected void tearDown() throws Exception {
63        super.tearDown();
64        releaseVirtualizer();
65    }
66
67    private static void assumeTrue(String message, boolean cond) {
68        assertTrue("(assume)"+message, cond);
69    }
70
71    private void log(String testName, String message) {
72        Log.v(TAG, "["+testName+"] "+message);
73    }
74
75    private void loge(String testName, String message) {
76        Log.e(TAG, "["+testName+"] "+message);
77    }
78
79    //-----------------------------------------------------------------
80    // VIRTUALIZER TESTS:
81    //----------------------------------
82
83
84    //-----------------------------------------------------------------
85    // 0 - constructor
86    //----------------------------------
87
88    //Test case 0.0: test constructor and release
89    @LargeTest
90    public void test0_0ConstructorAndRelease() throws Exception {
91        boolean result = false;
92        String msg = "test1_0ConstructorAndRelease()";
93        Virtualizer virtualizer = null;
94         try {
95            virtualizer = new Virtualizer(0, 0);
96            assertNotNull(msg + ": could not create Virtualizer", virtualizer);
97            try {
98                assertTrue(msg +": invalid effect ID", (virtualizer.getId() != 0));
99            } catch (IllegalStateException e) {
100                msg = msg.concat(": Virtualizer not initialized");
101            }
102            result = true;
103        } catch (IllegalArgumentException e) {
104            msg = msg.concat(": Virtualizer not found");
105        } catch (UnsupportedOperationException e) {
106            msg = msg.concat(": Effect library not loaded");
107        } finally {
108            if (virtualizer != null) {
109                virtualizer.release();
110            }
111        }
112        assertTrue(msg, result);
113    }
114
115
116    //-----------------------------------------------------------------
117    // 1 - get/set parameters
118    //----------------------------------
119
120    //Test case 1.0: test strength
121    @LargeTest
122    public void test1_0Strength() throws Exception {
123        boolean result = false;
124        String msg = "test1_0Strength()";
125        getVirtualizer(0);
126        try {
127            if (mVirtualizer.getStrengthSupported()) {
128                mVirtualizer.setStrength((short)TEST_STRENGTH);
129                short strength = mVirtualizer.getRoundedStrength();
130                // allow 10% difference between set strength and rounded strength
131                assertTrue(msg +": got incorrect strength",
132                        ((float)strength > (float)TEST_STRENGTH * 0.9f) &&
133                        ((float)strength < (float)TEST_STRENGTH * 1.1f));
134            } else {
135                short strength = mVirtualizer.getRoundedStrength();
136                assertTrue(msg +": got incorrect strength", strength >= 0 && strength <= 1000);
137            }
138            result = true;
139        } catch (IllegalArgumentException e) {
140            msg = msg.concat(": Bad parameter value");
141            loge(msg, "Bad parameter value");
142        } catch (UnsupportedOperationException e) {
143            msg = msg.concat(": get parameter() rejected");
144            loge(msg, "get parameter() rejected");
145        } catch (IllegalStateException e) {
146            msg = msg.concat("get parameter() called in wrong state");
147            loge(msg, "get parameter() called in wrong state");
148        } finally {
149            releaseVirtualizer();
150        }
151        assertTrue(msg, result);
152    }
153
154    //Test case 1.1: test properties
155    @LargeTest
156    public void test1_1Properties() throws Exception {
157        boolean result = false;
158        String msg = "test1_1Properties()";
159        getVirtualizer(0);
160        try {
161            Virtualizer.Settings settings = mVirtualizer.getProperties();
162            String str = settings.toString();
163            settings = new Virtualizer.Settings(str);
164            mVirtualizer.setProperties(settings);
165            result = true;
166        } catch (IllegalArgumentException e) {
167            msg = msg.concat(": Bad parameter value");
168            loge(msg, "Bad parameter value");
169        } catch (UnsupportedOperationException e) {
170            msg = msg.concat(": get parameter() rejected");
171            loge(msg, "get parameter() rejected");
172        } catch (IllegalStateException e) {
173            msg = msg.concat("get parameter() called in wrong state");
174            loge(msg, "get parameter() called in wrong state");
175        } finally {
176            releaseVirtualizer();
177        }
178        assertTrue(msg, result);
179    }
180
181    //-----------------------------------------------------------------
182    // private methods
183    //----------------------------------
184
185    private void getVirtualizer(int session) {
186         if (mVirtualizer == null || session != mSession) {
187             if (session != mSession && mVirtualizer != null) {
188                 mVirtualizer.release();
189                 mVirtualizer = null;
190             }
191             try {
192                mVirtualizer = new Virtualizer(0, session);
193                mSession = session;
194            } catch (IllegalArgumentException e) {
195                Log.e(TAG, "getVirtualizer() Virtualizer not found exception: "+e);
196            } catch (UnsupportedOperationException e) {
197                Log.e(TAG, "getVirtualizer() Effect library not loaded exception: "+e);
198            }
199         }
200         assertNotNull("could not create mVirtualizer", mVirtualizer);
201    }
202
203    private void releaseVirtualizer() {
204        if (mVirtualizer != null) {
205            mVirtualizer.release();
206            mVirtualizer = null;
207        }
208   }
209
210}
211