1 /* 2 * Copyright (c) 2022 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 16 #include <gtest/gtest.h> 17 #include <vector> 18 #include <cmath> 19 #include "script_context.h" 20 #include "script_instruction.h" 21 #include "script_instruction_unittest.h" 22 #include "script_instructionhelper.h" 23 #include "script_updateprocesser.h" 24 #include "script_manager_impl.h" 25 #include "script/script_unittest.h" 26 #include "thread_pool.h" 27 28 using namespace Uscript; 29 using namespace BasicInstruction; 30 using namespace Hpackage; 31 using namespace testing::ext; 32 33 namespace { 34 class UTestProcessorScriptEnv : public UTestScriptEnv { 35 public: UTestProcessorScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager)36 explicit UTestProcessorScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager) : UTestScriptEnv(pkgManager) 37 {} 38 ~UTestProcessorScriptEnv() = default; 39 PostMessage(const std::string & cmd,std::string content)40 virtual void PostMessage(const std::string &cmd, std::string content) 41 { 42 message_ = cmd + " " + content; 43 } GetPostMessage()44 std::string GetPostMessage() 45 { 46 return message_; 47 } 48 private: 49 std::string message_ {}; 50 }; 51 52 class TestPkgManager : public TestScriptPkgManager { 53 public: ExtractFile(const std::string & fileId,StreamPtr output)54 int32_t ExtractFile(const std::string &fileId, StreamPtr output) override 55 { 56 return 0; 57 } GetFileInfo(const std::string & fileId)58 const FileInfo *GetFileInfo(const std::string &fileId) override 59 { 60 static FileInfo fileInfo {}; 61 if (fileId == "script") { 62 return &fileInfo; 63 } 64 return nullptr; 65 } 66 }; 67 68 class UpdateProcesserInstructionUnittest : public ::testing::Test { 69 public: UpdateProcesserInstructionUnittest()70 UpdateProcesserInstructionUnittest() {} ~UpdateProcesserInstructionUnittest()71 ~UpdateProcesserInstructionUnittest() {} TestUpdateProcesserSetProcess() const72 void TestUpdateProcesserSetProcess() const 73 { 74 { 75 TestPkgManager pkgManager; 76 UTestProcessorScriptEnv env {&pkgManager}; 77 ScriptManagerImpl scriptManager {&env}; 78 ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 79 UScriptInstructionContext context {}; 80 context.AddInputParam(std::make_shared<IntegerValue>(1)); 81 auto instruction = std::make_unique<UScriptInstructionSetProcess>(); 82 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM); 83 std::vector<UScriptValuePtr> output = context.GetOutVar(); 84 EXPECT_EQ(output.size(), 0); 85 ScriptInstructionHelper::ReleaseBasicInstructionHelper(); 86 } 87 { 88 TestPkgManager pkgManager; 89 UTestProcessorScriptEnv env {&pkgManager}; 90 ScriptManagerImpl scriptManager {&env}; 91 ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 92 UScriptInstructionContext context {}; 93 constexpr float progress = 1.0f; 94 context.AddInputParam(std::make_shared<FloatValue>(progress)); 95 auto instruction = std::make_unique<UScriptInstructionSetProcess>(); 96 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 97 std::vector<UScriptValuePtr> output = context.GetOutVar(); 98 EXPECT_EQ(output.size(), 0); 99 std::stringstream ss; 100 ss << "set_progress " << progress; 101 EXPECT_EQ(env.GetPostMessage(), ss.str()); 102 ScriptInstructionHelper::ReleaseBasicInstructionHelper(); 103 } 104 } TestUpdateProcesserShowProcess() const105 void TestUpdateProcesserShowProcess() const 106 { 107 { 108 TestPkgManager pkgManager; 109 UTestProcessorScriptEnv env {&pkgManager}; 110 ScriptManagerImpl scriptManager {&env}; 111 ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 112 { 113 UScriptInstructionContext context {}; 114 context.AddInputParam(std::make_shared<IntegerValue>(1)); 115 auto instruction = std::make_unique<UScriptInstructionShowProcess>(); 116 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM); 117 std::vector<UScriptValuePtr> output = context.GetOutVar(); 118 EXPECT_EQ(output.size(), 0); 119 } 120 { 121 UScriptInstructionContext context {}; 122 context.AddInputParam(std::make_shared<FloatValue>(1)); 123 context.AddInputParam(std::make_shared<IntegerValue>(1)); 124 auto instruction = std::make_unique<UScriptInstructionShowProcess>(); 125 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM); 126 std::vector<UScriptValuePtr> output = context.GetOutVar(); 127 EXPECT_EQ(output.size(), 0); 128 } 129 { 130 UScriptInstructionContext context {}; 131 constexpr float start = 1.0f; 132 constexpr float end = 2.0f; 133 context.AddInputParam(std::make_shared<FloatValue>(start)); 134 context.AddInputParam(std::make_shared<FloatValue>(end)); 135 auto instruction = std::make_unique<UScriptInstructionShowProcess>(); 136 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 137 std::vector<UScriptValuePtr> output = context.GetOutVar(); 138 EXPECT_EQ(output.size(), 0); 139 std::stringstream ss; 140 ss << "show_progress " << start << "," << end; 141 EXPECT_EQ(env.GetPostMessage(), ss.str()); 142 } 143 ScriptInstructionHelper::ReleaseBasicInstructionHelper(); 144 } 145 } TestUpdateProcesserPrint() const146 void TestUpdateProcesserPrint() const 147 { 148 TestPkgManager pkgManager; 149 UTestProcessorScriptEnv env {&pkgManager}; 150 ScriptManagerImpl scriptManager {&env}; 151 ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 152 { 153 UScriptInstructionContext context {}; 154 context.AddInputParam(std::make_shared<IntegerValue>(1)); 155 auto instruction = std::make_unique<UScriptInstructionUiPrint>(); 156 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM); 157 std::vector<UScriptValuePtr> output = context.GetOutVar(); 158 EXPECT_EQ(output.size(), 0); 159 } 160 { 161 UScriptInstructionContext context {}; 162 constexpr auto content = "content"; 163 context.AddInputParam(std::make_shared<StringValue>(content)); 164 auto instruction = std::make_unique<UScriptInstructionUiPrint>(); 165 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 166 std::vector<UScriptValuePtr> output = context.GetOutVar(); 167 EXPECT_EQ(output.size(), 0); 168 std::stringstream ss; 169 ss << "ui_log " << content; 170 EXPECT_EQ(env.GetPostMessage(), ss.str()); 171 } 172 ScriptInstructionHelper::ReleaseBasicInstructionHelper(); 173 } TestUpdateProcesserSetProportion() const174 void TestUpdateProcesserSetProportion() const 175 { 176 TestPkgManager pkgManager; 177 UTestProcessorScriptEnv env {&pkgManager}; 178 ScriptManagerImpl scriptManager {&env}; 179 ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 180 { 181 UScriptInstructionContext context {}; 182 context.AddInputParam(std::make_shared<IntegerValue>(1)); 183 auto instruction = std::make_unique<UScriptInstructionSetProportion>(); 184 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM); 185 std::vector<UScriptValuePtr> output = context.GetOutVar(); 186 EXPECT_EQ(output.size(), 0); 187 bool ret = fabs(GetScriptProportion() - 1.0f) < 1e-6; 188 EXPECT_TRUE(ret); 189 } 190 { 191 UScriptInstructionContext context {}; 192 context.AddInputParam(std::make_shared<FloatValue>(0.5f)); 193 auto instruction = std::make_unique<UScriptInstructionSetProportion>(); 194 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 195 std::vector<UScriptValuePtr> output = context.GetOutVar(); 196 EXPECT_EQ(output.size(), 0); 197 bool ret = fabs(GetScriptProportion() - 0.5f) < 1e-6; 198 EXPECT_TRUE(ret); 199 } 200 ScriptInstructionHelper::ReleaseBasicInstructionHelper(); 201 } 202 protected: SetUp()203 void SetUp() {} TearDown()204 void TearDown() {} TestBody()205 void TestBody() {} 206 }; 207 208 HWTEST_F(UpdateProcesserInstructionUnittest, TestUpdateProcesserSetProcess, TestSize.Level1) 209 { 210 UpdateProcesserInstructionUnittest {}.TestUpdateProcesserSetProcess(); 211 } 212 213 HWTEST_F(UpdateProcesserInstructionUnittest, TestUpdateProcesserShowProcess, TestSize.Level1) 214 { 215 UpdateProcesserInstructionUnittest {}.TestUpdateProcesserShowProcess(); 216 } 217 218 HWTEST_F(UpdateProcesserInstructionUnittest, TestUpdateProcesserPrint, TestSize.Level1) 219 { 220 UpdateProcesserInstructionUnittest {}.TestUpdateProcesserPrint(); 221 } 222 HWTEST_F(UpdateProcesserInstructionUnittest, TestUpdateProcesserSetProportion, TestSize.Level1) 223 { 224 UpdateProcesserInstructionUnittest {}.TestUpdateProcesserSetProportion(); 225 } 226 }