1 /*
2 * Copyright (c) 2024 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 "update_engine_test.h"
16
17 #include <fstream>
18 #include <iostream>
19
20 #include "json/json.h"
21 #include "update_engine.h"
22 #include "intell_voice_manager.h"
23 #include "engine_host_manager.h"
24 #include "intell_voice_log.h"
25 #include "intell_voice_service_manager.h"
26 #include "silence_update_strategy.h"
27 #include "i_intell_voice_engine.h"
28 #include "history_info_mgr.h"
29
30 #define LOG_TAG "UpdateEngineTest"
31
32 #define INTELL_VOICE_BUILD_VARIANT_ROOT
33
34 using namespace testing;
35 using namespace testing::ext;
36 using namespace OHOS;
37 using namespace std;
38 using namespace OHOS::IntellVoiceTests;
39 using namespace OHOS::IntellVoice;
40 using namespace OHOS::IntellVoiceEngine;
41 using namespace OHOS::IntellVoiceUtils;
42 using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
43
44 #define private public
45
46 namespace OHOS {
47 namespace IntellVoiceTests {
48 class TestWptr : public RefBase {
49 public:
Func()50 void Func()
51 {
52 wptr<TestWptr> thisWptr(this);
53 std::thread([thisWptr]() {
54 INTELL_VOICE_LOG_INFO("start");
55 sleep(1);
56 sptr<TestWptr> thisSptr = thisWptr.promote();
57 if (thisSptr != nullptr) {
58 INTELL_VOICE_LOG_WARN("TestWptr not null");
59 } else {
60 INTELL_VOICE_LOG_WARN("TestWptr is null");
61 }
62 INTELL_VOICE_LOG_INFO("end");
63 }).detach();
64 }
65 };
66
SetUpTestCase(void)67 void UpdateEngineTest::SetUpTestCase(void)
68 {}
69
TearDownTestCase(void)70 void UpdateEngineTest::TearDownTestCase(void)
71 {}
72
SetUp(void)73 void UpdateEngineTest::SetUp(void)
74 {}
75
TearDown(void)76 void UpdateEngineTest::TearDown(void)
77 {}
78
79 /**
80 * @tc.name : Test Template UpdateEngineTest
81 * @tc.number: UpdateEngineTest_001
82 * @tc.desc : Test For SilenceUpdate
83 */
84 HWTEST_F(UpdateEngineTest, UpdateEngineTest_001, TestSize.Level1)
85 {
86 if (!EngineHostManager::GetInstance().Init()) {
87 INTELL_VOICE_LOG_ERROR("init engine host failed");
88 }
89 HistoryInfoMgr &historyInfoMgr = HistoryInfoMgr::GetInstance();
90 historyInfoMgr.SetStringKVPair(KEY_WAKEUP_VESRION, "0");
91 auto &mgr = ServiceManagerType::GetInstance();
92 mgr.HandleReleaseEngine(INTELL_VOICE_WAKEUP);
93 std::shared_ptr<SilenceUpdateStrategy> silenceStrategy = std::make_shared<SilenceUpdateStrategy>("");
94 if (silenceStrategy == nullptr) {
95 INTELL_VOICE_LOG_ERROR("silence strategy is nullptr");
96 }
97 std::shared_ptr<IUpdateStrategy> strategy = std::dynamic_pointer_cast<IUpdateStrategy>(silenceStrategy);
98 int ret = mgr.CreateUpdateEngineUntilTime(strategy);
99 sleep(5);
100 EXPECT_EQ(0, ret);
101 INTELL_VOICE_LOG_INFO("end");
102 }
103
104 /**
105 * @tc.name : Test Template UpdateEngineTest
106 * @tc.number: UpdateEngineTest_001
107 * @tc.desc : Test For Voiceprint Clone And ClearUserData
108 */
109 HWTEST_F(UpdateEngineTest, UpdateEngineTest_002, TestSize.Level1)
110 {
111 std::vector<WakeupSourceFile> cloneFileInfo;
112 int ret1 = IntellVoiceManager::GetInstance()->GetWakeupSourceFiles(cloneFileInfo);
113 sleep(2);
114 EXPECT_EQ(0, ret1);
115 INTELL_VOICE_LOG_INFO("GetWakeupSourceFiles end");
116
117 int ret2 = IntellVoiceManager::GetInstance()->ClearUserData();
118 sleep(2);
119 EXPECT_EQ(0, ret2);
120 INTELL_VOICE_LOG_INFO("ClearUserData end");
121
122 Json::Value wakeupInfo;
123 wakeupInfo["clone"] = "true";
124 wakeupInfo["clone_path"] = "/data/service/el0/intelligent_voice/wakeup/clone";
125 wakeupInfo["source_device"] = "BRA-AL00";
126 wakeupInfo["source_version"] = "3.0.0.28";
127 wakeupInfo["destination_device"] = "${result.substring(0, 8)}";
128 wakeupInfo["destination_version"] = "${result.slice(9)}";
129 wakeupInfo["bundle_name"] = "com.huawei.hmos.aibase";
130 wakeupInfo["ability_name"] = "WakeUpExtAbility";
131
132 cb_ = std::make_shared<IntellVoiceUpdateCallback>();
133 int ret3 = IntellVoiceManager::GetInstance()->EnrollWithWakeupFilesForResult(
134 cloneFileInfo, wakeupInfo.toStyledString(), cb_);
135 sleep(5);
136 EXPECT_EQ(0, ret3);
137 INTELL_VOICE_LOG_INFO("EnrollWithWakeupFilesForResult end");
138 }
139
140 /**
141 * @tc.name : Test Template UpdateEngineTest
142 * @tc.number: UpdateEngineTest_001
143 * @tc.desc : Test For Wptr
144 */
145 HWTEST_F(UpdateEngineTest, UpdateEngineTest_003, TestSize.Level1)
146 {
147 sptr<TestWptr> t = sptr<TestWptr>(new TestWptr());
148 t->Func();
149 t = nullptr;
150 ASSERT_EQ(nullptr, t);
151 }
152 } // namespace IntellVoiceTests
153 } // namespace OHOS