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.BassBoost;
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 MediaBassBoostTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> {
46    private String TAG = "MediaBassBoostTest";
47    private final static short TEST_STRENGTH = 500;
48
49    private BassBoost mBassBoost = null;
50    private int mSession = -1;
51
52    public MediaBassBoostTest() {
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        releaseBassBoost();
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    // BASS BOOST 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        BassBoost bb = null;
94         try {
95            bb = new BassBoost(0, 0);
96            assertNotNull(msg + ": could not create BassBoost", bb);
97            try {
98                assertTrue(msg +": invalid effect ID", (bb.getId() != 0));
99            } catch (IllegalStateException e) {
100                msg = msg.concat(": BassBoost not initialized");
101            }
102            result = true;
103        } catch (IllegalArgumentException e) {
104            msg = msg.concat(": BassBoost not found");
105        } catch (UnsupportedOperationException e) {
106            msg = msg.concat(": Effect library not loaded");
107        } finally {
108            if (bb != null) {
109                bb.release();
110            }
111        }
112        assertTrue(msg, result);
113    }
114
115    //-----------------------------------------------------------------
116    // 1 - get/set parameters
117    //----------------------------------
118
119    //Test case 1.0: test strength
120    @LargeTest
121    public void test1_0Strength() throws Exception {
122        boolean result = false;
123        String msg = "test1_0Strength()";
124        getBassBoost(0);
125        try {
126            if (mBassBoost.getStrengthSupported()) {
127                mBassBoost.setStrength((short)TEST_STRENGTH);
128                short strength = mBassBoost.getRoundedStrength();
129                // allow 10% difference between set strength and rounded strength
130                assertTrue(msg +": got incorrect strength",
131                        ((float)strength > (float)TEST_STRENGTH * 0.9f) &&
132                        ((float)strength < (float)TEST_STRENGTH * 1.1f));
133            } else {
134                short strength = mBassBoost.getRoundedStrength();
135                assertTrue(msg +": got incorrect strength", strength >= 0 && strength <= 1000);
136            }
137            result = true;
138        } catch (IllegalArgumentException e) {
139            msg = msg.concat(": Bad parameter value");
140            loge(msg, "Bad parameter value");
141        } catch (UnsupportedOperationException e) {
142            msg = msg.concat(": get parameter() rejected");
143            loge(msg, "get parameter() rejected");
144        } catch (IllegalStateException e) {
145            msg = msg.concat("get parameter() called in wrong state");
146            loge(msg, "get parameter() called in wrong state");
147        } finally {
148            releaseBassBoost();
149        }
150        assertTrue(msg, result);
151    }
152
153    //Test case 1.1: test properties
154    @LargeTest
155    public void test1_1Properties() throws Exception {
156        boolean result = false;
157        String msg = "test1_1Properties()";
158        getBassBoost(0);
159        try {
160            BassBoost.Settings settings = mBassBoost.getProperties();
161            String str = settings.toString();
162            settings = new BassBoost.Settings(str);
163            mBassBoost.setProperties(settings);
164            result = true;
165        } catch (IllegalArgumentException e) {
166            msg = msg.concat(": Bad parameter value");
167            loge(msg, "Bad parameter value");
168        } catch (UnsupportedOperationException e) {
169            msg = msg.concat(": get parameter() rejected");
170            loge(msg, "get parameter() rejected");
171        } catch (IllegalStateException e) {
172            msg = msg.concat("get parameter() called in wrong state");
173            loge(msg, "get parameter() called in wrong state");
174        } finally {
175            releaseBassBoost();
176        }
177        assertTrue(msg, result);
178    }
179
180    //-----------------------------------------------------------------
181    // private methods
182    //----------------------------------
183
184    private void getBassBoost(int session) {
185         if (mBassBoost == null || session != mSession) {
186             if (session != mSession && mBassBoost != null) {
187                 mBassBoost.release();
188                 mBassBoost = null;
189             }
190             try {
191                mBassBoost = new BassBoost(0, session);
192                mSession = session;
193            } catch (IllegalArgumentException e) {
194                Log.e(TAG, "getBassBoost() BassBoost not found exception: "+e);
195            } catch (UnsupportedOperationException e) {
196                Log.e(TAG, "getBassBoost() Effect library not loaded exception: "+e);
197            }
198         }
199         assertNotNull("could not create mBassBoost", mBassBoost);
200    }
201
202    private void releaseBassBoost() {
203        if (mBassBoost != null) {
204            mBassBoost.release();
205            mBassBoost = null;
206        }
207   }
208
209}
210