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
18 #include <atomic>
19 #include <chrono>
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include <thread>
24 #include <tuple>
25 #include <unordered_map>
26 #include <vector>
27
28 #include "common.h"
29 #include "gmock/gmock.h"
30 #include "log.h"
31 #include "request_preload.h"
32
33 using namespace testing::ext;
34 using namespace OHOS::Request;
35
36 class PreloadProgress : public testing::Test {
37 public:
38 void SetUp();
39 };
40
SetUp(void)41 void PreloadProgress::SetUp(void)
42 {
43 // input testcase setup step,setup invoked before each testcases
44 testing::UnitTest *test = testing::UnitTest::GetInstance();
45 ASSERT_NE(test, nullptr);
46 const testing::TestInfo *testInfo = test->current_test_info();
47 ASSERT_NE(testInfo, nullptr);
48 string testCaseName = string(testInfo->name());
49 REQUEST_HILOGI("[SetUp] %{public}s start", testCaseName.c_str());
50 GTEST_LOG_(INFO) << testCaseName.append(" start");
51 }
52
53 static std::string TEST_URL_0 = "https://www.gitee.com/tiga-ultraman/downloadTests/releases/download/v1.01/"
54 "test.txt";
55 static std::string TEST_URL_1 = "https://www.w3cschool.cn/statics/demosource/movie.mp4";
56 static std::string TEST_URL_2 = "https://www.baidu.com";
57 static std::string TEST_URL_3 = "https://vd4.bdstatic.com/mda-pm7bte3t6fs50rsh/sc/cae_h264/"
58 "1702057792414494257/"
59 "mda-pm7bte3t6fs50rsh.mp4?v_from_s=bdapp-author-nanjing";
60
61 constexpr size_t SLEEP_INTERVAL = 100;
62
DownloadProgressTest(std::string url)63 void DownloadProgressTest(std::string url)
64 {
65 Preload::GetInstance()->Remove(url);
66 auto flagS = std::make_shared<std::atomic_bool>(false);
67 auto flagF = std::make_shared<std::atomic_bool>(false);
68 auto flagC = std::make_shared<std::atomic_bool>(false);
69 auto flagTot = std::make_shared<std::atomic_uint64_t>(0);
70 auto flagCur = std::make_shared<std::atomic_uint64_t>(0);
71 auto flagP = std::make_shared<std::atomic_bool>(true);
72 auto callback = PreloadCallback{
73 .OnSuccess = [flagS](const std::shared_ptr<Data> &&data, const std::string &taskId) { flagS->store(true); },
74 .OnCancel = [flagC]() { flagC->store(true); },
75 .OnFail = [flagF](const PreloadError &error, const std::string &taskId) { flagF->store(true); },
76 .OnProgress =
77 [flagCur, flagTot, flagP](uint64_t current, uint64_t total) {
78 if (flagCur->load() > current) {
79 flagP->store(false);
80 }
81 if (flagTot->load() > total) {
82 flagP->store(false);
83 }
84 flagCur->store(current);
85 flagTot->store(total);
86 },
87 };
88 auto handle = Preload::GetInstance()->load(url, std::make_unique<PreloadCallback>(callback));
89 while (!handle->IsFinish()) {
90 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_INTERVAL));
91 }
92 EXPECT_FALSE(flagF->load());
93 EXPECT_FALSE(flagC->load());
94
95 EXPECT_TRUE(flagP->load());
96 EXPECT_TRUE(flagS->load());
97
98 EXPECT_EQ(flagCur->load(), flagTot->load());
99 Preload::GetInstance()->Remove(url);
100 }
101
102 /**
103 * @tc.name: OnProgressAddCallback_2
104 * @tc.desc: Test Add callback for same url after progress - post-progress loading
105 * @tc.precon: NA
106 * @tc.step: 1. Remove test URL from preload manager
107 * 2. Create first callback and load URL
108 * 3. Wait for download completion with progress tracking
109 * 4. Create second callback and load same URL
110 * 5. Verify second callback uses cached data
111 * @tc.expect: Second callback completes immediately using cached data
112 * @tc.type: FUNC
113 * @tc.require: issueNumber
114 * @tc.level: Level 1
115 */
116
117 HWTEST_F(PreloadProgress, OnProgressTest, TestSize.Level1)
118 {
119 DownloadProgressTest(TEST_URL_0);
120 DownloadProgressTest(TEST_URL_1);
121 DownloadProgressTest(TEST_URL_2);
122 DownloadProgressTest(TEST_URL_3);
123 }
124