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
18 #include "progress_thread.h"
19
20 using namespace testing::ext;
21 using namespace testing;
22
23 namespace OHOS::UpdateService {
24
25 Progress downloadProgress;
TestProgressCallback(const Progress & progress)26 void TestProgressCallback(const Progress &progress)
27 {
28 downloadProgress = progress;
29 }
30
31 class StreamProgressThreadTest : public testing::Test {
32 public:
SetupTestCase(void)33 static void SetupTestCase(void) {};
TearDownTestCase(void)34 static void TearDownTestCase(void) {};
SetUp()35 void SetUp() final {};
TearDown()36 void TearDown() final {};
37 };
38
39 HWTEST_F(StreamProgressThreadTest, StartDownloadTest, TestSize.Level1)
40 {
41 StreamProgressThread thread(TestProgressCallback);
42
43 std::string url = "http://file-examples.com/wp-content/uploads/2017/02/file-example_txt-download.txt";
44 int64_t fileSize = 2250;
45 int64_t recordPoint = 0;
46
47 // 调用 StartDownload
48 int32_t result = thread.StartDownload(url, fileSize, recordPoint);
49 ASSERT_EQ(result, 0); // 假设 StartDownload 返回 0 表示成功
50 thread.StopDownload();
51 }
52
53 HWTEST_F(StreamProgressThreadTest, WriteFuncTest, TestSize.Level1)
54 {
55 StreamProgressThread thread(TestProgressCallback);
56
57 // 模拟写入数据
58 uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
59 size_t size = sizeof(data);
60 size_t nmemb = 1;
61
62 // 调用 WriteFunc
63 size_t result = StreamProgressThread::WriteFunc(data, size, nmemb, &thread);
64 ASSERT_EQ(result, size);
65 }
66
67 HWTEST_F(StreamProgressThreadTest, DownloadProgressTest, TestSize.Level1)
68 {
69 StreamProgressThread thread(TestProgressCallback);
70
71 // 模拟进度更新参数
72 double dlTotal = 1024;
73 double dlNow = 512;
74 double ulTotal = 0;
75 double ulNow = 0;
76
77 // 调用 DownloadProgress
78 int32_t result = StreamProgressThread::DownloadProgress(&thread, dlTotal, dlNow, ulTotal, ulNow);
79 ASSERT_EQ(result, 0);
80 }
81 } // namespace OHOS::UpdateService