aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/org/cyanogenmod/tests/settings/unit/CMPartnerInterfaceTest.java
blob: c2b01c20790d2aabc2af609c728e68667dff800b (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
 * Copyright (c) 2015, The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cyanogenmod.tests.settings.unit;

import android.app.INotificationManager;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.service.notification.Condition;
import android.service.notification.IConditionListener;
import android.test.AndroidTestCase;
import android.test.FlakyTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.provider.Settings;
import android.text.format.DateUtils;
import android.util.Log;

import cyanogenmod.app.CMContextConstants;
import cyanogenmod.app.PartnerInterface;

import java.util.List;

/**
 * Unit test for PartnerInterface
 */
public class CMPartnerInterfaceTest extends AndroidTestCase {

    private static final String TAG = "CMPartnerInterfaceTest";

    private PartnerInterface mPartnerInterface;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // Only run this if we support partner interfaces
        org.junit.Assume.assumeTrue(mContext.getPackageManager().hasSystemFeature(
                CMContextConstants.Features.PARTNER));
        mPartnerInterface = PartnerInterface.getInstance(getContext());

        setupAirplaneModeTests();
        setupZenModeTests();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        teardownAirplaneModeTests();
        teardownZenModeTests();
    }

    @SmallTest
    public void testPartnerInterfaceExists() {
        assertNotNull(mPartnerInterface);
    }

    @SmallTest
    public void testPartnerInterfaceAvailable() {
        assertNotNull(mPartnerInterface.getService());
    }

    /////////////////////////////////////////////////////
    // Airplane Mode tests

    private boolean mAirplaneModeEnabled;
    private void setupAirplaneModeTests() {
        // Remember the initial state
        mAirplaneModeEnabled = getAirplaneModeEnabled();
    }

    private void teardownAirplaneModeTests() {
        // Restore airplane mode
        mPartnerInterface.setAirplaneModeEnabled(mAirplaneModeEnabled);
    }
    @SmallTest
    public void testSetAirplaneModeOn() {
        mPartnerInterface.setAirplaneModeEnabled(true);
        assertTrue(getAirplaneModeEnabled());
    }

    @SmallTest
    public void testSetAirplaneModeOff() {
        mPartnerInterface.setAirplaneModeEnabled(false);
        assertTrue(getAirplaneModeEnabled() == false);
    }

    private boolean getAirplaneModeEnabled() {
        return Settings.Global.getInt(getContext().getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
    }

    /////////////////////////////////////////////////////
    // Zen Mode Testing:
    // Allow the tests below for now, but in the future
    // implement a valid method for restoring the zen mode
    // setting, e.g. in tearDownZenModeTests

    // Because it's not possible to get & restore zen mode
    // duration, these tests will contaminate system state
    // whenver they're run. Ideally testing should instead
    // be done in another suite, such as using Mokito
    // https://corner.squareup.com/2012/10/mockito-android.html
    //
    // However because of the complexity, for now allow
    // these unit tests as-is.
    //
    // THESE WILL WIPE OUT any duration-based zen modes in
    // effect!
    private static final long DURATION_30s_MS = 30 * DateUtils.SECOND_IN_MILLIS;
    private static final long DURATION_TOLERANCE_MS = 5;    //5 ms in tolerance due to latency
    private static final long DURATION_TEST_MAX_MS = DateUtils.MINUTE_IN_MILLIS; //Allow 1 minute max for unit testing

    private INotificationManager mNotificationManager;
    private CountdownConditionListener mConditionListener;

    private void setupZenModeTests() {
        mNotificationManager = INotificationManager.Stub.asInterface(
                ServiceManager.getService(Context.NOTIFICATION_SERVICE));

        mConditionListener = new CountdownConditionListener();
        try {
            mNotificationManager.requestZenModeConditions(mConditionListener, Condition.FLAG_RELEVANT_ALWAYS);
        } catch (RemoteException e) {
            fail("requestZenModeConditions exception " + e);
        }
    }

    private void teardownZenModeTests() {
        // For now, restore the zen mode to the system default
        mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_OFF);
    }

    @SmallTest
    public void testSetZenModeImportantInterruptions() {
        mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
        assertEquals(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS, getZenMode());
    }

    @FlakyTest(tolerance = 5)
    @SmallTest
    public void testSetZenModeImportantInterruptionsWithDurations() {
        // 0 duration
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS, 0);

        // Indefinitely
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS, Long.MAX_VALUE);
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS, -1);

        // normal duration values (1, 5s)
        // NOTE: these tests do not return until duration has passed. Use with care!
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS, DateUtils.SECOND_IN_MILLIS);
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS, 5 * DateUtils.SECOND_IN_MILLIS);
    }

    @SmallTest
    public void testSetZenModeNoInterruptions() {
        mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS);
        assertEquals(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS, getZenMode());
    }

    @FlakyTest(tolerance = 5)
    @SmallTest
    public void testSetZenModeNoInterruptionsWithDurations() {
        // 0 duration
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS, 0);

        // Indefinitely
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS, Long.MAX_VALUE);
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS, -1);

        // normal duration values (1, 5s)
        // NOTE: these tests do not return until duration has passed. Use with care!
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS, DateUtils.SECOND_IN_MILLIS);
        testZenModeWithDuration(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS, 5 * DateUtils.SECOND_IN_MILLIS);
    }

    @SmallTest
    public void testSetZenModeOff() {
        mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_OFF);
        assertEquals(PartnerInterface.ZEN_MODE_OFF, getZenMode());
    }

    private final static int BUFFER_ELEMENTS_TO_REC = 1024;
    private final static int BYTES_PER_ELEMENT = 2;
    private static final int RECORDER_SAMPLERATE = 41000;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

    private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
    public AudioRecord findAudioRecord() {
        for (int rate : mSampleRates) {
            for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT,
                    AudioFormat.ENCODING_PCM_16BIT }) {
                for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO,
                        AudioFormat.CHANNEL_IN_STEREO }) {
                    try {
                        Log.d(TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
                                + channelConfig);
                        int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                        if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                            AudioRecord recorder = new AudioRecord(
                                    cyanogenmod.media.MediaRecorder.AudioSource.HOTWORD,
                                    rate, channelConfig, audioFormat, bufferSize);

                            if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
                                return recorder;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, rate + "Exception, keep trying.",e);
                    }
                }
            }
        }
        return null;
    }

    @SmallTest
    public void testGetCurrentHotwordPackageName() {
        // make sure no one is actively stealing this as we attempt to
        assertNull(mPartnerInterface.getCurrentHotwordPackageName());

        // find first viable audio record
        final AudioRecord audioRecorder = findAudioRecord();

        audioRecorder.startRecording();
        assertEquals(mContext.getPackageName(), mPartnerInterface.getCurrentHotwordPackageName());
        audioRecorder.stop();
    }

    /**
     * testZenModeWithDuration: sets Zen Mode with duration and blocks
     * until the duration is verified. Use with care! This does not
     * return until after durationMillis
     * @param mode
     * @param durationMillis
     */
    private void testZenModeWithDuration(int mode, long durationMillis) {

        final long startTimeMillis = System.currentTimeMillis();
        mPartnerInterface.setZenModeWithDuration(mode, durationMillis);
        final int actualZenMode = getZenMode();

        // check zen mode is correct
        if (durationMillis == 0) {
            assertTrue(actualZenMode == mode || actualZenMode == PartnerInterface.ZEN_MODE_OFF);
        } else {
            assertEquals(mode, actualZenMode);
        }
        // skip durations that are indefinite
        if (actualZenMode == PartnerInterface.ZEN_MODE_OFF || durationMillis < 0 || durationMillis == Long.MAX_VALUE) {
            return;
        }
        // check duration is correct
        final long zenDuration = getZenModeDuration(startTimeMillis);
        assertTrue(Math.abs(zenDuration - durationMillis) <= DURATION_TOLERANCE_MS); //Allow a tolerance
    }

    /**
     * getZenModeDuration: Blocking call to wait for ZenMode duration.
     * @param startTimeMillis - start time of the duration
     * @return
     */
    private long getZenModeDuration(long startTimeMillis) {
        // NOTE: waits for the next duration to be triggered
        return mConditionListener.waitForDuration(startTimeMillis);
    }

    private int getZenMode() {
        int zenMode = -1;
        try {
            zenMode = mNotificationManager.getZenMode();
        } catch (RemoteException rex) {
            fail("INotificationManager.getZenMode() " + rex.getMessage());
            return -1;
        }
        Log.d(TAG, "getZenMode returning " + zenMode);
        return zenMode;
    }
    /**
     * CountdownConditionListener
     * This class is passed to blocks until the Countdown is received
     */
    private static class CountdownConditionListener
            extends IConditionListener.Stub {

        private static long INVALID_ENDTIME = -1;
        private long mEndTime = INVALID_ENDTIME;

        public CountdownConditionListener() {
        }

        /**
         * waitForDuration: blocks until onConditionReceived
         * This CountdownConditionListener was previously passed
         * to the
         * @return
         * the duration of
         */
        public synchronized long waitForDuration(long startTimeMillis) {
            Log.d(TAG, "waitForDuration");
            // If we have a stale endtime, then discard it
            if (mEndTime < startTimeMillis) {
                mEndTime = INVALID_ENDTIME;
            }
            // If no valid endtime, then block and wait for the current
            // duration to expire. The wait ends when
            // onConditionsReceived is called
            if (mEndTime == INVALID_ENDTIME) {
                try {
                    // wait no more than DURATION_TEST_MAX_MS
                    wait(DURATION_TEST_MAX_MS);
                } catch (InterruptedException iex) {
                    Log.e(TAG, "waitForDuration", iex);

                    return -1;
                }
            }
            if (mEndTime == INVALID_ENDTIME) {
                Log.d(TAG, "waitForDuration found invalid endtime. Did you exceed the max duration (" + DURATION_TEST_MAX_MS + " ms)?");
                return -1;
            }

            Log.d(TAG, "waitForDuration returning endtime:" + mEndTime + " duration:" +  (mEndTime - startTimeMillis));
            final long duration = mEndTime - startTimeMillis;

            // Reset endtime to show that it's been consumed
            mEndTime = INVALID_ENDTIME;
            return duration;
        }

        /**
         * onConditionReceived: called when a condition is triggered
         * @param conditions - conditions that triggered
         * This is actually just the Alarm endtime that CountdownConditionProvider
         * previously submitted
         * @throws RemoteException
         */
        @Override
        public synchronized void onConditionsReceived(Condition[] conditions) throws RemoteException {
            // CountdownConditionProvider only triggers 1 condition at a time
            mEndTime = parseEndTime(conditions[0].id);
            notify();
        }

        private long parseEndTime(Uri conditionUri) {
            final List<String> pathSegments = conditionUri.getPathSegments();
            return Long.decode(pathSegments.get(pathSegments.size() - 1));
        }

        // Private method for debugging
        private void logConditions(Condition[] conditions) {
            for (int i = 0; i < conditions.length; i++) {
                Log.d(TAG, "condition[" + i + "]:" + conditions[i].id);
            }
        }
    }
}