• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "script_context.h"
19 #include "script_instruction.h"
20 #include "script_instruction_unittest.h"
21 #include "script_instructionhelper.h"
22 #include "script_updateprocesser.h"
23 #include "script_manager_impl.h"
24 #include "script/script_unittest.h"
25 
26 using namespace Uscript;
27 using namespace BasicInstruction;
28 using namespace Hpackage;
29 using namespace testing::ext;
30 
31 namespace {
32 class UTestProcessorScriptEnv : public UTestScriptEnv {
33 public:
UTestProcessorScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager)34     explicit UTestProcessorScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager) : UTestScriptEnv(pkgManager)
35     {}
36     ~UTestProcessorScriptEnv() = default;
37 
PostMessage(const std::string & cmd,std::string content)38     virtual void PostMessage(const std::string &cmd, std::string content)
39     {
40         message_ = cmd + " " + content;
41     }
GetPostMessage()42     std::string GetPostMessage()
43     {
44         return message_;
45     }
46 private:
47     std::string message_ {};
48 };
49 
50 class TestPkgManager : public TestScriptPkgManager {
51 public:
ExtractFile(const std::string & fileId,StreamPtr output)52     int32_t ExtractFile(const std::string &fileId, StreamPtr output) override
53     {
54         return 0;
55     }
GetFileInfo(const std::string & fileId)56     const FileInfo *GetFileInfo(const std::string &fileId) override
57     {
58         static FileInfo fileInfo {};
59         if (fileId == "script") {
60             return &fileInfo;
61         }
62         return nullptr;
63     }
64 };
65 
66 class UpdateProcesserInstructionUnittest : public ::testing::Test {
67 public:
UpdateProcesserInstructionUnittest()68     UpdateProcesserInstructionUnittest() {}
~UpdateProcesserInstructionUnittest()69     ~UpdateProcesserInstructionUnittest() {}
TestUpdateProcesserSetProcess() const70     void TestUpdateProcesserSetProcess() const
71     {
72         {
73             TestPkgManager pkgManager;
74             UTestProcessorScriptEnv env {&pkgManager};
75             ScriptManagerImpl scriptManager {&env};
76             ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager);
77             UScriptInstructionContext context {};
78             context.AddInputParam(std::make_shared<IntegerValue>(1));
79             auto instruction = std::make_unique<UScriptInstructionSetProcess>();
80             EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM);
81             std::vector<UScriptValuePtr> output = context.GetOutVar();
82             EXPECT_EQ(output.size(), 0);
83             ScriptInstructionHelper::ReleaseBasicInstructionHelper();
84         }
85         {
86             TestPkgManager pkgManager;
87             UTestProcessorScriptEnv env {&pkgManager};
88             ScriptManagerImpl scriptManager {&env};
89             ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager);
90             UScriptInstructionContext context {};
91             constexpr float progress = 1.0f;
92             context.AddInputParam(std::make_shared<FloatValue>(progress));
93             auto instruction = std::make_unique<UScriptInstructionSetProcess>();
94             EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS);
95             std::vector<UScriptValuePtr> output = context.GetOutVar();
96             EXPECT_EQ(output.size(), 0);
97             std::stringstream ss;
98             ss << "set_progress " << progress;
99             EXPECT_EQ(env.GetPostMessage(), ss.str());
100             ScriptInstructionHelper::ReleaseBasicInstructionHelper();
101         }
102     }
TestUpdateProcesserShowProcess() const103     void TestUpdateProcesserShowProcess() const
104     {
105         {
106             TestPkgManager pkgManager;
107             UTestProcessorScriptEnv env {&pkgManager};
108             ScriptManagerImpl scriptManager {&env};
109             ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager);
110             {
111                 UScriptInstructionContext context {};
112                 context.AddInputParam(std::make_shared<IntegerValue>(1));
113                 auto instruction = std::make_unique<UScriptInstructionShowProcess>();
114                 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM);
115                 std::vector<UScriptValuePtr> output = context.GetOutVar();
116                 EXPECT_EQ(output.size(), 0);
117             }
118             {
119                 UScriptInstructionContext context {};
120                 context.AddInputParam(std::make_shared<FloatValue>(1));
121                 context.AddInputParam(std::make_shared<IntegerValue>(1));
122                 auto instruction = std::make_unique<UScriptInstructionShowProcess>();
123                 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM);
124                 std::vector<UScriptValuePtr> output = context.GetOutVar();
125                 EXPECT_EQ(output.size(), 0);
126             }
127             {
128                 UScriptInstructionContext context {};
129                 constexpr float start = 1.0f;
130                 constexpr float end = 2.0f;
131                 context.AddInputParam(std::make_shared<FloatValue>(start));
132                 context.AddInputParam(std::make_shared<FloatValue>(end));
133                 auto instruction = std::make_unique<UScriptInstructionShowProcess>();
134                 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS);
135                 std::vector<UScriptValuePtr> output = context.GetOutVar();
136                 EXPECT_EQ(output.size(), 0);
137                 std::stringstream ss;
138                 ss << "show_progress " << start << "," << end;
139                 EXPECT_EQ(env.GetPostMessage(), ss.str());
140             }
141             ScriptInstructionHelper::ReleaseBasicInstructionHelper();
142         }
143     }
TestUpdateProcesserPrint() const144     void TestUpdateProcesserPrint() const
145     {
146         TestPkgManager pkgManager;
147         UTestProcessorScriptEnv env {&pkgManager};
148         ScriptManagerImpl scriptManager {&env};
149         ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager);
150         {
151             UScriptInstructionContext context {};
152             context.AddInputParam(std::make_shared<IntegerValue>(1));
153             auto instruction = std::make_unique<UScriptInstructionUiPrint>();
154             EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM);
155             std::vector<UScriptValuePtr> output = context.GetOutVar();
156             EXPECT_EQ(output.size(), 0);
157         }
158         {
159             UScriptInstructionContext context {};
160             constexpr auto content = "content";
161             context.AddInputParam(std::make_shared<StringValue>(content));
162             auto instruction = std::make_unique<UScriptInstructionUiPrint>();
163             EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS);
164             std::vector<UScriptValuePtr> output = context.GetOutVar();
165             EXPECT_EQ(output.size(), 0);
166             std::stringstream ss;
167             ss << "ui_log " << content;
168             EXPECT_EQ(env.GetPostMessage(), ss.str());
169         }
170         ScriptInstructionHelper::ReleaseBasicInstructionHelper();
171     }
172 protected:
SetUp()173     void SetUp() {}
TearDown()174     void TearDown() {}
TestBody()175     void TestBody() {}
176 };
177 
178 HWTEST_F(UpdateProcesserInstructionUnittest, TestUpdateProcesserSetProcess, TestSize.Level1)
179 {
180     UpdateProcesserInstructionUnittest {}.TestUpdateProcesserSetProcess();
181 }
182 
183 HWTEST_F(UpdateProcesserInstructionUnittest, TestUpdateProcesserShowProcess, TestSize.Level1)
184 {
185     UpdateProcesserInstructionUnittest {}.TestUpdateProcesserShowProcess();
186 }
187 
188 HWTEST_F(UpdateProcesserInstructionUnittest, TestUpdateProcesserPrint, TestSize.Level1)
189 {
190     UpdateProcesserInstructionUnittest {}.TestUpdateProcesserPrint();
191 }
192 }