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
16 #include <gtest/gtest.h>
17 #include <pthread.h>
18 #include <thread>
19 #include <string>
20 #include "script_instruction.h"
21 #include "script/script_unittest.h"
22 #include "applypatch/update_progress.h"
23 #include "script_instruction/script_instruction_unittest.h"
24
25 using namespace testing::ext;
26 using namespace Updater;
27 using namespace Uscript;
28
29
30 namespace OHOS {
31 class UpdateProgressTest : public testing::Test {
32 public:
SetUpTestCase(void)33 static void SetUpTestCase(void) {}
TearDownTestCase(void)34 static void TearDownTestCase(void) {}
SetUp()35 void SetUp() {}
TearDown()36 void TearDown() {}
TestBody()37 void TestBody() {}
38 };
39
40 class UTestPostProgressEnv : public UTestScriptEnv {
41 public:
UTestPostProgressEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager)42 explicit UTestPostProgressEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager) : UTestScriptEnv(pkgManager)
43 {}
44 ~UTestPostProgressEnv() = default;
45
PostMessage(const std::string &cmd, std::string content)46 virtual void PostMessage(const std::string &cmd, std::string content)
47 {
48 message_ = cmd + " " + content;
49 }
GetPostMessage()50 std::string GetPostMessage()
51 {
52 return message_;
53 }
54 private:
55 std::string message_ {};
56 };
57
HWTEST_F(UpdateProgressTest, UpdateProgressTest01, TestSize.Level1)58 HWTEST_F(UpdateProgressTest, UpdateProgressTest01, TestSize.Level1)
59 {
60 Hpackage::TestScriptPkgManager pkgManager;
61 UTestPostProgressEnv env {&pkgManager};
62 pthread_t thread;
63 EXPECT_EQ(CreateProgressThread(&env, thread), 0);
64 float progress = 1.0f;
65 SetUpdateProgress(progress);
66 std::this_thread::sleep_for(std::chrono::milliseconds(500));
67 EXPECT_EQ(env.GetPostMessage(), env.GetPostMessage());
68 SetProgressExitFlag(thread);
69 }
70 }