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