1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <gtest/gtest.h>
16 #include <unistd.h>
17 #include <thread>
18 #include <mutex>
19 #include <condition_variable>
20 #include <cstdio>
21
22 #include "intell_voice_log.h"
23 #include "i_intell_voice_engine.h"
24 #include "i_intell_voice_service.h"
25 #include "engine_callback_inner.h"
26 #include "engine_event_callback.h"
27 #include "wait_for_result.h"
28 #include "intell_voice_manager.h"
29
30 #define LOG_TAG "ClientTest"
31
32 using namespace OHOS::IntellVoiceTests;
33 using namespace OHOS::IntellVoice;
34 using namespace OHOS::IntellVoiceEngine;
35 using namespace OHOS;
36 using namespace testing::ext;
37
38 static sptr<IIntellVoiceService> g_sProxy = nullptr;
39
40 class ClientTest : public testing::Test {
41 public:
42 static void SetUpTestCase(void);
43 static void TearDownTestCase(void);
44 void SetUp();
45 void TearDown();
46 std::shared_ptr<EngineEventCallback> cb_ = nullptr;
47 };
48
SetUpTestCase(void)49 void ClientTest::SetUpTestCase(void)
50 {
51 INTELL_VOICE_LOG_INFO("hello harmony OS!\n");
52 }
53
TearDownTestCase(void)54 void ClientTest::TearDownTestCase(void)
55 {
56 }
57
SetUp(void)58 void ClientTest::SetUp(void)
59 {
60 }
61
TearDown(void)62 void ClientTest::TearDown(void)
63 {
64 }
65
66 HWTEST_F(ClientTest, ClientUtils, TestSize.Level1)
67 {
68 WaitForResult waitForResult;
69
70 sptr<IIntellVoiceEngine> engine;
71 IntellVoiceManager::GetInstance()->CreateIntellVoiceEngine(INTELL_VOICE_ENROLL, engine);
72 ASSERT_NE(engine, nullptr);
73
74 cb_ = std::make_shared<EngineEventCallback>(engine, &waitForResult);
75 ASSERT_NE(cb_, nullptr);
76
77 sptr<EngineCallbackInner> callback = new (std::nothrow) EngineCallbackInner(cb_);
78 ASSERT_NE(callback, nullptr);
79
80 sptr<IRemoteObject> callbackObj = callback->AsObject();
81 ASSERT_NE(callbackObj, nullptr);
82
83 engine->SetCallback(callbackObj);
84
85 IntellVoiceEngineInfo info = {};
86 info.wakeupPhrase = "\xE5\xB0\x8F\xE8\x89\xBA\xE5\xB0\x8F\xE8\x89\xBA";
87 info.isPcmFromExternal = true;
88 info.minBufSize = 1280;
89 info.sampleChannels = 1;
90 info.bitsPerSample = 16;
91 info.sampleRate = 16000;
92
93 engine->Attach(info);
94
95 waitForResult.Wait();
96
97 engine->Detach();
98
99 INTELL_VOICE_LOG_INFO("end\n");
100 }
101