2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <app_manager_extension.h>
19 #include <tzplatform_config.h>
20 #include <system_info.h>
22 #include <gtest/gtest.h>
25 #include "test_util.h"
28 SttTestUtility::SttTestUtility()
31 mCurrentState = STT_STATE_CREATED;
32 mErrorOccurred = false;
33 mResultReceived = false;
34 mErrorMessage = nullptr;
42 SttTestUtility::~SttTestUtility()
48 mErrorMessage = nullptr;
55 void SttTestUtility::StateChangedCallback(stt_h tts, stt_state_e previous, stt_state_e current, void *user_data)
57 auto instance = reinterpret_cast<SttTestUtility *>(user_data);
58 instance->mCurrentState = current;
61 void SttTestUtility::ErrorCallback(stt_h stt, stt_error_e reason, void *user_data)
63 auto instance = reinterpret_cast<SttTestUtility *>(user_data);
64 instance->mErrorOccurred = true;
66 char *errorMessage = nullptr;
67 ASSERT_EQ(stt_get_error_message(instance->mHandle, &errorMessage), STT_ERROR_NONE);
69 free(instance->mErrorMessage);
70 instance->mErrorMessage = errorMessage;
73 void SttTestUtility::RecognitionResultCallback(stt_h stt, stt_result_event_e event, const char** data, int data_count, const char* msg, void *user_data)
75 auto instance = reinterpret_cast<SttTestUtility *>(user_data);
76 instance->mResultReceived = true;
80 void SttTestUtility::TerminateCurrentEngine()
82 char* engineId = vconf_get_str(VCONFKEY_STT_ENGINE_DEFAULT);
84 app_context_h context = nullptr;
85 app_manager_get_app_context(engineId, &context);
88 ASSERT_NE(context, nullptr);
90 EXPECT_EQ(app_manager_terminate_app(context), APP_MANAGER_ERROR_NONE);
91 EXPECT_EQ(app_context_destroy(context), APP_MANAGER_ERROR_NONE);
94 void SttTestUtility::WaitUntilEngineTerminated(int duration)
96 auto engineChcker = [](void) {
97 bool is_running = false;
99 char* engineId = vconf_get_str(VCONFKEY_STT_ENGINE_DEFAULT);
100 app_manager_is_running(engineId, &is_running);
106 WaitCondition(engineChcker, duration);
109 bool SttTestUtility::IsFeatureSupported()
111 bool isSttSupported = false;
112 if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool("http://tizen.org/feature/speech.recognition", &isSttSupported)) {
116 bool isMicSupported = false;
117 if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool("http://tizen.org/feature/microphone", &isMicSupported)) {
121 return isSttSupported && isMicSupported;
124 void SttTestUtility::GetTestPCMData()
126 static const char* PCM_PATH = tzplatform_mkpath(tzplatform_getid("TZ_SYS_RO_APP"), "/org.tizen.stt-unittests/res/test_pcm.dat");
128 FILE* fp_in = fopen(PCM_PATH, "rb");
129 if (fp_in == nullptr) {
133 fseek(fp_in, 0, SEEK_END);
134 long size = ftell(fp_in);
135 fseek(fp_in, 0, SEEK_SET);
141 char* data = reinterpret_cast<char *>(calloc(sizeof(char), size));
147 size_t read_size = fread(data, sizeof(char), size, fp_in);
150 if (read_size <= 0) {
159 void SttTestUtility::CreateHandle()
161 if (IsFeatureSupported() == false) {
166 stt_create(&mHandle);
167 ASSERT_NE(mHandle, nullptr);
169 EXPECT_EQ(stt_set_state_changed_cb(mHandle, StateChangedCallback, this), STT_ERROR_NONE);
170 EXPECT_EQ(stt_set_error_cb(mHandle, ErrorCallback, this), STT_ERROR_NONE);
173 void SttTestUtility::DestroyHandle()
175 if (nullptr != mHandle) {
176 EXPECT_EQ(stt_destroy(mHandle), STT_ERROR_NONE);
181 void SttTestUtility::SetTestMode()
183 stt_state_e state = STT_STATE_CREATED;
184 ASSERT_EQ(stt_get_state(mHandle, &state), STT_ERROR_NONE);
185 ASSERT_EQ(state, STT_STATE_READY);
186 EXPECT_EQ(stt_set_private_data(mHandle, "stt_verification", "true"), STT_ERROR_NONE);
189 void SttTestUtility::UnsetTestMode()
191 stt_state_e state = STT_STATE_CREATED;
192 stt_get_state(mHandle, &state);
193 if (STT_STATE_READY != state) {
197 stt_set_private_data(mHandle, "stt_verification", "false");
200 bool SttTestUtility::IsStateChanged(stt_state_e targetState, int duration)
202 auto stateChecker = std::bind([](SttTestUtility *instance, stt_state_e target) {
203 return target == instance->mCurrentState;
204 }, this, targetState);
206 return WaitCondition(stateChecker, duration);
209 bool SttTestUtility::IsErrorOccurring(int duration)
211 auto errorChecker = std::bind([](SttTestUtility *instance) {
212 return instance->mErrorOccurred;
215 return WaitCondition(errorChecker, duration);
218 bool SttTestUtility::IsResultReceived(int duration)
220 auto resultChecker = std::bind([](SttTestUtility *instance) {
221 return instance->mResultReceived;
224 return WaitCondition(resultChecker, duration);
227 bool SttTestUtility::Prepare()
229 stt_state_e state = STT_STATE_CREATED;
230 stt_get_state(mHandle, &state);
231 if (STT_STATE_CREATED != state) {
235 stt_prepare(mHandle);
236 return IsStateChanged(STT_STATE_READY, 5);
239 bool SttTestUtility::Unprepare()
241 stt_state_e state = STT_STATE_CREATED;
242 stt_get_state(mHandle, &state);
243 if (STT_STATE_READY != state) {
247 stt_unprepare(mHandle);
248 return IsStateChanged(STT_STATE_CREATED, 5);
251 bool SttTestUtility::Start(const char* language, const char* type)
253 EXPECT_EQ(stt_start(mHandle, language, type), STT_ERROR_NONE);
254 return IsStateChanged(STT_STATE_RECORDING, 5);
257 bool SttTestUtility::Stop()
259 EXPECT_EQ(stt_stop(mHandle), STT_ERROR_NONE);
260 return IsStateChanged(STT_STATE_PROCESSING, 5);
263 bool SttTestUtility::StartAudioStreaming(const char* language, const char* type)
265 EXPECT_EQ(stt_start_audio_streaming(mHandle, language, type), STT_ERROR_NONE);
266 return IsStateChanged(STT_STATE_RECORDING, 5);
269 bool SttTestUtility::StopAudioStreaming()
271 EXPECT_EQ(stt_stop_audio_streaming(mHandle), STT_ERROR_NONE);
272 return IsStateChanged(STT_STATE_PROCESSING, 5);
275 bool SttTestUtility::Cancel()
277 stt_state_e state = STT_STATE_CREATED;
278 stt_get_state(mHandle, &state);
279 if (STT_STATE_READY == state || STT_STATE_CREATED == state) {
283 EXPECT_EQ(stt_cancel(mHandle), STT_ERROR_NONE);
284 return IsStateChanged(STT_STATE_READY, 5);
288 bool SttTestUtility::WaitCondition(std::function<bool(void)> checker, int duration)
290 auto mainLoopQuitTimer = ecore_timer_add(static_cast<double>(duration), [](void *data) -> Eina_Bool {
291 ecore_main_loop_quit();
295 auto checkerTimer = ecore_timer_add(0.0, [](void *data) -> Eina_Bool {
296 auto &IsConditionSatisfied = *reinterpret_cast<std::function<bool()> *>(data);
297 if (false == IsConditionSatisfied()) {
301 ecore_main_loop_quit();
305 if (nullptr == mainLoopQuitTimer || nullptr == checkerTimer) {
309 ecore_main_loop_begin();
311 ecore_timer_del(mainLoopQuitTimer);
312 mainLoopQuitTimer = nullptr;
314 ecore_timer_del(checkerTimer);
315 checkerTimer = nullptr;