• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <gtest/gtest.h>
17 #include "firmware_stream_installer_install.h"
18 
19 using namespace testing::ext;
20 using namespace testing;
21 
22 namespace OHOS::UpdateService {
23 
24 UpgradeStatus g_status;
25 bool g_result;
OnFirmwareProgressImpl(const FirmwareComponent & component)26 void OnFirmwareProgressImpl(const FirmwareComponent &component)
27 {
28 }
29 
OnFirmwareEventImpl(bool result,const ErrorMessage & errMsg,UpgradeStatus status)30 void OnFirmwareEventImpl(bool result, const ErrorMessage &errMsg, UpgradeStatus status)
31 {
32     g_result = result;
33     g_status = status;
34 }
35 
OnFirmwareStatusImpl(UpgradeStatus status)36 void OnFirmwareStatusImpl(UpgradeStatus status)
37 {
38     g_status = status;
39 }
40 
41 class StreamInstallerInstallTest : public ::testing::Test {
42 protected:
SetUp()43     void SetUp() override
44     {
45         installer = std::make_unique<StreamInstallerInstall>();
46     }
47 
48     std::unique_ptr<StreamInstallerInstall> installer;
49 };
50 
51 // 测试 StartInstall 函数
52 HWTEST_F(StreamInstallerInstallTest, StartInstallSuccess, TestSize.Level1)
53 {
54     FirmwareComponent component;
55     component.url = "http://file-examples.com/wp-content/uploads/2017/02/file-example_txt-download.txt";
56     component.size = 2250;
57     component.status = UpgradeStatus::INIT;
58     component.progress = 0;
59     component.recordPoint = 0;
60 
61     FirmwareInstallCallback callback;
62     callback.onFirmwareProgress = OnFirmwareProgressImpl;
63     callback.onFirmwareEvent = OnFirmwareEventImpl;
64     callback.onFirmwareStatus = OnFirmwareStatusImpl;
65 
66     // 创建组件列表
67     std::vector<FirmwareComponent> componentList = {component};
68 
69     // 调用 StartInstall
70     installer->StartInstall(componentList, callback);
71     ASSERT_EQ(g_result, true);
72 }
73 
74 HWTEST_F(StreamInstallerInstallTest, StartInstallFailure, TestSize.Level1)
75 {
76     // 创建空组件列表
77     std::vector<FirmwareComponent> emptyList;
78 
79     FirmwareInstallCallback callback;
80     callback.onFirmwareProgress = OnFirmwareProgressImpl;
81     callback.onFirmwareEvent = OnFirmwareEventImpl;
82     callback.onFirmwareStatus = OnFirmwareStatusImpl;
83 
84     // 调用 StartInstall
85     installer->StartInstall(emptyList, callback);
86 
87     ASSERT_EQ(g_result, false);
88 }
89 
90 } // namespace OHOS::UpdateService