1/*
2 * Copyright (C) 2008 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;
18
19
20//import android.content.Resources;
21import android.util.Log;
22
23import android.media.ToneGenerator;
24import android.media.AudioManager;
25
26/**
27 * Junit / Instrumentation test case for the Sim tones tests
28
29 */
30    public class TonesAutoTest {
31        private static String TAG = "TonesAutoTest";
32
33    // Test all DTMF tones one by one
34    public static boolean tonesDtmfTest() throws Exception {
35        Log.v(TAG, "DTMF tones test");
36        ToneGenerator toneGen;
37        int type;
38        boolean result = true;
39
40        toneGen = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
41
42        for (type = ToneGenerator.TONE_DTMF_0; type <= ToneGenerator.TONE_DTMF_D; type++) {
43            if (toneGen.startTone(type)) {
44                Thread.sleep(200);
45                toneGen.stopTone();
46                Thread.sleep(100);
47            } else {
48                result = false;
49                break;
50            }
51        }
52
53        toneGen.release();
54        return result;
55    }
56
57    // Test all supervisory tones one by one
58    public static boolean tonesSupervisoryTest() throws Exception {
59      Log.v(TAG, "Supervisory tones test");
60      ToneGenerator toneGen;
61      int type;
62      boolean result = true;
63
64      toneGen = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
65
66      for (type = ToneGenerator.TONE_SUP_DIAL;
67      type <= ToneGenerator.TONE_SUP_RINGTONE; type++) {
68          if (toneGen.startTone(type)) {
69              Thread.sleep(2000);
70              toneGen.stopTone();
71              Thread.sleep(200);
72          } else {
73              result = false;
74              break;
75          }
76      }
77
78      for (type = ToneGenerator.TONE_SUP_INTERCEPT;
79      type <= ToneGenerator.TONE_SUP_PIP; type++) {
80          if (toneGen.startTone(type)) {
81              Thread.sleep(5000);
82              toneGen.stopTone();
83              Thread.sleep(200);
84          } else {
85              result = false;
86              break;
87          }
88      }
89
90      toneGen.release();
91      return result;
92    }
93
94    // Test all proprietary tones one by one
95    public static boolean tonesProprietaryTest() throws Exception {
96        Log.v(TAG, "Proprietary tones test");
97        ToneGenerator toneGen;
98        int type;
99        boolean result = true;
100
101        toneGen = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
102
103        for (type = ToneGenerator.TONE_PROP_BEEP; type <= ToneGenerator.TONE_PROP_BEEP2; type++) {
104            if (toneGen.startTone(type)) {
105                Thread.sleep(1000);
106                toneGen.stopTone();
107                Thread.sleep(100);
108            } else {
109                result = false;
110                break;
111            }
112        }
113
114        toneGen.release();
115        return result;
116    }
117
118    // Test playback of 2 tones simultaneously
119    public static boolean tonesSimultaneousTest() throws Exception {
120        Log.v(TAG, "Simultaneous tones test");
121        ToneGenerator toneGen1;
122        ToneGenerator toneGen2;
123        int type;
124        boolean result = true;
125
126        toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
127        toneGen2 = new ToneGenerator(AudioManager.STREAM_MUSIC, 50);
128
129        if (toneGen1.startTone(ToneGenerator.TONE_DTMF_1)) {
130            Thread.sleep(100);
131            if (toneGen2.startTone(ToneGenerator.TONE_DTMF_2)) {
132                Thread.sleep(500);
133                toneGen1.stopTone();
134                Thread.sleep(100);
135                toneGen2.stopTone();
136            } else {
137                toneGen1.stopTone();
138                result = false;
139            }
140        } else {
141            result = false;
142        }
143
144        toneGen1.release();
145        toneGen2.release();
146        return result;
147    }
148
149    // Test start of new tone without stopping previous one
150    public static boolean tonesStressTest() throws Exception {
151        Log.v(TAG, "Stress tones test");
152        ToneGenerator toneGen;
153        int type;
154        boolean result = true;
155
156        toneGen = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
157
158        for (type = ToneGenerator.TONE_DTMF_1; type <= ToneGenerator.TONE_DTMF_9; type++) {
159            if (toneGen.startTone(type)) {
160                Thread.sleep(200);
161            } else {
162                result = false;
163                break;
164            }
165        }
166
167        toneGen.release();
168        return result;
169    }
170
171    // Perform all tones tests
172    public static boolean tonesAllTest() throws Exception {
173        Log.v(TAG, "All tones tests");
174
175        if (!tonesDtmfTest()) {
176            return false;
177        }
178        if (!tonesSupervisoryTest()) {
179            return false;
180        }
181        if (!tonesProprietaryTest()) {
182            return false;
183        }
184        if (!tonesSimultaneousTest()) {
185            return false;
186        }
187        if (!tonesStressTest()) {
188            return false;
189        }
190        return true;
191    }
192}
193