summaryrefslogtreecommitdiff
path: root/soundtrigger/2.1/vts/functional/VtsHalSoundtriggerV2_1TargetTest.cpp
blob: 7f06ed91994d3c6fb81d64dc4d686a2d3794eaec (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Copyright (C) 2018 The Android Open Source 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.
 */

#define LOG_TAG "SoundTriggerHidlHalTest"
#include <stdlib.h>
#include <time.h>

#include <condition_variable>
#include <mutex>

#include <android/log.h>
#include <cutils/native_handle.h>
#include <gtest/gtest.h>
#include <hidl/GtestPrinter.h>
#include <hidl/ServiceManagement.h>
#include <log/log.h>

#include <android/hardware/audio/common/2.0/types.h>
#include <android/hardware/soundtrigger/2.0/ISoundTriggerHw.h>
#include <android/hardware/soundtrigger/2.0/types.h>
#include <android/hardware/soundtrigger/2.1/ISoundTriggerHw.h>
#include <android/hidl/allocator/1.0/IAllocator.h>
#include <hidlmemory/mapping.h>

#define SHORT_TIMEOUT_PERIOD (1)

using ::android::sp;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::audio::common::V2_0::AudioDevice;
using ::android::hardware::soundtrigger::V2_0::PhraseRecognitionExtra;
using ::android::hardware::soundtrigger::V2_0::RecognitionMode;
using ::android::hardware::soundtrigger::V2_0::SoundModelHandle;
using ::android::hardware::soundtrigger::V2_0::SoundModelType;
using V2_0_ISoundTriggerHw = ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw;
using V2_0_ISoundTriggerHwCallback =
    ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
using ::android::hardware::soundtrigger::V2_1::ISoundTriggerHw;
using ::android::hardware::soundtrigger::V2_1::ISoundTriggerHwCallback;
using ::android::hidl::allocator::V1_0::IAllocator;
using ::android::hidl::memory::V1_0::IMemory;

/**
 * Test code uses this class to wait for notification from callback.
 */
class Monitor {
   public:
    Monitor() : mCount(0) {}

    /**
     * Adds 1 to the internal counter and unblocks one of the waiting threads.
     */
    void notify() {
        std::unique_lock<std::mutex> lock(mMtx);
        mCount++;
        mCv.notify_one();
    }

    /**
     * Blocks until the internal counter becomes greater than 0.
     *
     * If notified, this method decreases the counter by 1 and returns true.
     * If timeout, returns false.
     */
    bool wait(int timeoutSeconds) {
        auto deadline = std::chrono::system_clock::now() + std::chrono::seconds(timeoutSeconds);
        std::unique_lock<std::mutex> lock(mMtx);
        if (!mCv.wait_until(lock, deadline, [& count = mCount] { return count > 0; })) {
            return false;
        }
        mCount--;
        return true;
    }

   private:
    std::mutex mMtx;
    std::condition_variable mCv;
    int mCount;
};

// The main test class for Sound Trigger HIDL HAL.
class SoundTriggerHidlTest : public ::testing::TestWithParam<std::string> {
   public:
    virtual void SetUp() override {
        mSoundTriggerHal = ISoundTriggerHw::getService(GetParam());
        ASSERT_NE(nullptr, mSoundTriggerHal.get());
        mCallback = new SoundTriggerHwCallback(*this);
        ASSERT_NE(nullptr, mCallback.get());
    }

    static void SetUpTestCase() { srand(1234); }

    class SoundTriggerHwCallback : public ISoundTriggerHwCallback {
       private:
        SoundTriggerHidlTest& mParent;

       public:
        SoundTriggerHwCallback(SoundTriggerHidlTest& parent) : mParent(parent) {}

        Return<void> recognitionCallback(const V2_0_ISoundTriggerHwCallback::RecognitionEvent& event
                                             __unused,
                                         int32_t cookie __unused) override {
            ALOGI("%s", __FUNCTION__);
            return Void();
        };

        Return<void> phraseRecognitionCallback(
            const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent& event __unused,
            int32_t cookie __unused) override {
            ALOGI("%s", __FUNCTION__);
            return Void();
        };

        Return<void> soundModelCallback(const V2_0_ISoundTriggerHwCallback::ModelEvent& event,
                                        int32_t cookie __unused) override {
            ALOGI("%s", __FUNCTION__);
            mParent.lastModelEvent_2_0 = event;
            mParent.monitor.notify();
            return Void();
        }

        Return<void> recognitionCallback_2_1(const ISoundTriggerHwCallback::RecognitionEvent& event
                                                 __unused,
                                             int32_t cookie __unused) override {
            ALOGI("%s", __FUNCTION__);
            return Void();
        }

        Return<void> phraseRecognitionCallback_2_1(
            const ISoundTriggerHwCallback::PhraseRecognitionEvent& event __unused,
            int32_t cookie __unused) override {
            ALOGI("%s", __FUNCTION__);
            return Void();
        }

        Return<void> soundModelCallback_2_1(const ISoundTriggerHwCallback::ModelEvent& event,
                                            int32_t cookie __unused) {
            ALOGI("%s", __FUNCTION__);
            mParent.lastModelEvent = event;
            mParent.monitor.notify();
            return Void();
        }
    };

    virtual void TearDown() override {}

    Monitor monitor;
    // updated by soundModelCallback()
    V2_0_ISoundTriggerHwCallback::ModelEvent lastModelEvent_2_0;
    // updated by soundModelCallback_2_1()
    ISoundTriggerHwCallback::ModelEvent lastModelEvent;

   protected:
    sp<ISoundTriggerHw> mSoundTriggerHal;
    sp<SoundTriggerHwCallback> mCallback;
};

/**
 * Test ISoundTriggerHw::getProperties() method
 *
 * Verifies that:
 *  - the implementation implements the method
 *  - the method returns 0 (no error)
 *  - the implementation supports at least one sound model and one key phrase
 *  - the implementation supports at least VOICE_TRIGGER recognition mode
 */
TEST_P(SoundTriggerHidlTest, GetProperties) {
    ISoundTriggerHw::Properties halProperties;
    Return<void> hidlReturn;
    int ret = -ENODEV;

    hidlReturn = mSoundTriggerHal->getProperties([&](int rc, auto res) {
        ret = rc;
        halProperties = res;
    });

    EXPECT_TRUE(hidlReturn.isOk());
    EXPECT_EQ(0, ret);
    EXPECT_GT(halProperties.maxSoundModels, 0u);
    EXPECT_GT(halProperties.maxKeyPhrases, 0u);
    EXPECT_NE(0u, (halProperties.recognitionModes & (uint32_t)RecognitionMode::VOICE_TRIGGER));
}

/**
 * Test ISoundTriggerHw::loadPhraseSoundModel() method
 *
 * Verifies that:
 *  - the implementation implements the method
 *  - the implementation returns an error when passed a malformed sound model
 *
 * There is no way to verify that implementation actually can load a sound model because each
 * sound model is vendor specific.
 */
TEST_P(SoundTriggerHidlTest, LoadInvalidModelFail) {
    Return<void> hidlReturn;
    int ret = -ENODEV;
    V2_0_ISoundTriggerHw::PhraseSoundModel model;
    SoundModelHandle handle;

    model.common.type = SoundModelType::UNKNOWN;

    hidlReturn =
        mSoundTriggerHal->loadPhraseSoundModel(model, mCallback, 0, [&](int32_t retval, auto res) {
            ret = retval;
            handle = res;
        });

    EXPECT_TRUE(hidlReturn.isOk());
    EXPECT_NE(0, ret);
    EXPECT_FALSE(monitor.wait(SHORT_TIMEOUT_PERIOD));
}

/**
 * Test ISoundTriggerHw::loadPhraseSoundModel_2_1() method
 *
 * Verifies that:
 *  - the implementation implements the method
 *  - the implementation returns an error when passed a malformed sound model
 *
 * There is no way to verify that implementation actually can load a sound model because each
 * sound model is vendor specific.
 */
TEST_P(SoundTriggerHidlTest, LoadInvalidModelFail_2_1) {
    Return<void> hidlReturn;
    int ret = -ENODEV;
    ISoundTriggerHw::PhraseSoundModel model;
    SoundModelHandle handle;

    model.common.header.type = SoundModelType::UNKNOWN;

    hidlReturn = mSoundTriggerHal->loadPhraseSoundModel_2_1(model, mCallback, 0,
                                                            [&](int32_t retval, auto res) {
                                                                ret = retval;
                                                                handle = res;
                                                            });

    EXPECT_TRUE(hidlReturn.isOk());
    EXPECT_NE(0, ret);
    EXPECT_FALSE(monitor.wait(SHORT_TIMEOUT_PERIOD));
}

/**
 * Test ISoundTriggerHw::loadSoundModel() method
 *
 * Verifies that:
 *  - the implementation returns an error when passed an empty sound model
 */
TEST_P(SoundTriggerHidlTest, LoadEmptyGenericSoundModelFail) {
    int ret = -ENODEV;
    V2_0_ISoundTriggerHw::SoundModel model;
    SoundModelHandle handle = 0;

    model.type = SoundModelType::GENERIC;

    Return<void> loadReturn =
        mSoundTriggerHal->loadSoundModel(model, mCallback, 0, [&](int32_t retval, auto res) {
            ret = retval;
            handle = res;
        });

    EXPECT_TRUE(loadReturn.isOk());
    EXPECT_NE(0, ret);
    EXPECT_FALSE(monitor.wait(SHORT_TIMEOUT_PERIOD));
}

/**
 * Test ISoundTriggerHw::loadSoundModel() method
 *
 * Verifies that:
 *  - the implementation returns error when passed a sound model with random data.
 */
TEST_P(SoundTriggerHidlTest, LoadGenericSoundModelFail) {
    int ret = -ENODEV;
    V2_0_ISoundTriggerHw::SoundModel model;
    SoundModelHandle handle = 0;

    model.type = SoundModelType::GENERIC;
    model.data.resize(100);
    for (auto& d : model.data) {
        d = rand();
    }

    Return<void> loadReturn =
        mSoundTriggerHal->loadSoundModel(model, mCallback, 0, [&](int32_t retval, auto res) {
            ret = retval;
            handle = res;
        });

    EXPECT_TRUE(loadReturn.isOk());
    EXPECT_NE(0, ret);
    EXPECT_FALSE(monitor.wait(SHORT_TIMEOUT_PERIOD));
}

/**
 * Test ISoundTriggerHw::loadSoundModel_2_1() method
 *
 * Verifies that:
 *  - the implementation returns error when passed a sound model with random data.
 */
TEST_P(SoundTriggerHidlTest, LoadEmptyGenericSoundModelFail_2_1) {
    int ret = -ENODEV;
    ISoundTriggerHw::SoundModel model;
    SoundModelHandle handle = 0;

    model.header.type = SoundModelType::GENERIC;

    Return<void> loadReturn =
        mSoundTriggerHal->loadSoundModel_2_1(model, mCallback, 0, [&](int32_t retval, auto res) {
            ret = retval;
            handle = res;
        });

    EXPECT_TRUE(loadReturn.isOk());
    EXPECT_NE(0, ret);
    EXPECT_FALSE(monitor.wait(SHORT_TIMEOUT_PERIOD));
}

/**
 * Test ISoundTriggerHw::loadSoundModel_2_1() method
 *
 * Verifies that:
 *  - the implementation returns error when passed a sound model with random data.
 */
TEST_P(SoundTriggerHidlTest, LoadGenericSoundModelFail_2_1) {
    int ret = -ENODEV;
    ISoundTriggerHw::SoundModel model;
    SoundModelHandle handle = 0;

    model.header.type = SoundModelType::GENERIC;
    sp<IAllocator> ashmem = IAllocator::getService("ashmem");
    ASSERT_NE(nullptr, ashmem.get());
    hidl_memory hmemory;
    int size = 100;
    Return<void> allocReturn = ashmem->allocate(size, [&](bool success, const hidl_memory& m) {
        ASSERT_TRUE(success);
        hmemory = m;
    });
    sp<IMemory> memory = ::android::hardware::mapMemory(hmemory);
    ASSERT_NE(nullptr, memory.get());
    memory->update();
    for (uint8_t *p = static_cast<uint8_t*>(static_cast<void*>(memory->getPointer())); size >= 0;
         p++, size--) {
        *p = rand();
    }

    Return<void> loadReturn =
        mSoundTriggerHal->loadSoundModel_2_1(model, mCallback, 0, [&](int32_t retval, auto res) {
            ret = retval;
            handle = res;
        });

    EXPECT_TRUE(loadReturn.isOk());
    EXPECT_NE(0, ret);
    EXPECT_FALSE(monitor.wait(SHORT_TIMEOUT_PERIOD));
}

/**
 * Test ISoundTriggerHw::unloadSoundModel() method
 *
 * Verifies that:
 *  - the implementation implements the method
 *  - the implementation returns an error when called without a valid loaded sound model
 *
 */
TEST_P(SoundTriggerHidlTest, UnloadModelNoModelFail) {
    Return<int32_t> hidlReturn(0);
    SoundModelHandle halHandle = 0;

    hidlReturn = mSoundTriggerHal->unloadSoundModel(halHandle);

    EXPECT_TRUE(hidlReturn.isOk());
    EXPECT_NE(0, hidlReturn);
}

/**
 * Test ISoundTriggerHw::startRecognition() method
 *
 * Verifies that:
 *  - the implementation implements the method
 *  - the implementation returns an error when called without a valid loaded sound model
 *
 * There is no way to verify that implementation actually starts recognition because no model can
 * be loaded.
 */
TEST_P(SoundTriggerHidlTest, StartRecognitionNoModelFail) {
    Return<int32_t> hidlReturn(0);
    SoundModelHandle handle = 0;
    PhraseRecognitionExtra phrase;
    V2_0_ISoundTriggerHw::RecognitionConfig config;

    config.captureHandle = 0;
    config.captureDevice = AudioDevice::IN_BUILTIN_MIC;
    phrase.id = 0;
    phrase.recognitionModes = (uint32_t)RecognitionMode::VOICE_TRIGGER;
    phrase.confidenceLevel = 0;

    config.phrases.setToExternal(&phrase, 1);

    hidlReturn = mSoundTriggerHal->startRecognition(handle, config, mCallback, 0);

    EXPECT_TRUE(hidlReturn.isOk());
    EXPECT_NE(0, hidlReturn);
}

/**
 * Test ISoundTriggerHw::startRecognition_2_1() method
 *
 * Verifies that:
 *  - the implementation implements the method
 *  - the implementation returns an error when called without a valid loaded sound model
 *
 * There is no way to verify that implementation actually starts recognition because no model can
 * be loaded.
 */
TEST_P(SoundTriggerHidlTest, StartRecognitionNoModelFail_2_1) {
    Return<int32_t> hidlReturn(0);
    SoundModelHandle handle = 0;
    PhraseRecognitionExtra phrase;
    ISoundTriggerHw::RecognitionConfig config;

    config.header.captureHandle = 0;
    config.header.captureDevice = AudioDevice::IN_BUILTIN_MIC;
    phrase.id = 0;
    phrase.recognitionModes = (uint32_t)RecognitionMode::VOICE_TRIGGER;
    phrase.confidenceLevel = 0;

    config.header.phrases.setToExternal(&phrase, 1);

    hidlReturn = mSoundTriggerHal->startRecognition_2_1(handle, config, mCallback, 0);

    EXPECT_TRUE(hidlReturn.isOk());
    EXPECT_NE(0, hidlReturn);
}

/**
 * Test ISoundTriggerHw::stopRecognition() method
 *
 * Verifies that:
 *  - the implementation implements the method
 *  - the implementation returns an error when called without an active recognition running
 *
 */
TEST_P(SoundTriggerHidlTest, StopRecognitionNoAStartFail) {
    Return<int32_t> hidlReturn(0);
    SoundModelHandle handle = 0;

    hidlReturn = mSoundTriggerHal->stopRecognition(handle);

    EXPECT_TRUE(hidlReturn.isOk());
    EXPECT_NE(0, hidlReturn);
}

/**
 * Test ISoundTriggerHw::stopAllRecognitions() method
 *
 * Verifies that:
 *  - the implementation implements this optional method or indicates it is not supported by
 *  returning -ENOSYS
 */
TEST_P(SoundTriggerHidlTest, stopAllRecognitions) {
    Return<int32_t> hidlReturn(0);

    hidlReturn = mSoundTriggerHal->stopAllRecognitions();

    EXPECT_TRUE(hidlReturn.isOk());
    EXPECT_TRUE(hidlReturn == 0 || hidlReturn == -ENOSYS);
}

INSTANTIATE_TEST_SUITE_P(
        PerInstance, SoundTriggerHidlTest,
        testing::ValuesIn(android::hardware::getAllHalInstanceNames(ISoundTriggerHw::descriptor)),
        android::hardware::PrintInstanceNameToString);