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 <thread>
18
19 #include "pasteboard_deduplicate_memory.h"
20 #include "pasteboard_error.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 using namespace testing::ext;
25
26 class PasteboardDeduplicateMemoryTest : public testing::Test {
27 public:
28 static void SetUpTestCase(void);
29 static void TearDownTestCase(void);
30 void SetUp();
31 void TearDown();
32 };
33
SetUpTestCase(void)34 void PasteboardDeduplicateMemoryTest::SetUpTestCase(void)
35 {
36 }
37
TearDownTestCase(void)38 void PasteboardDeduplicateMemoryTest::TearDownTestCase(void)
39 {
40 }
41
SetUp(void)42 void PasteboardDeduplicateMemoryTest::SetUp(void)
43 {
44 }
45
TearDown(void)46 void PasteboardDeduplicateMemoryTest::TearDown(void)
47 {
48 }
49
50 struct RadarReportIdentity {
51 pid_t pid;
52 PasteboardError errorCode;
53 };
54
operator ==(const RadarReportIdentity & lhs,const RadarReportIdentity & rhs)55 bool operator==(const RadarReportIdentity &lhs, const RadarReportIdentity &rhs)
56 {
57 return lhs.pid == rhs.pid && lhs.errorCode == rhs.errorCode;
58 }
59
60 /**
61 * @tc.name: TestIsDuplicate001
62 * @tc.desc: should return false when first called IsDuplicate
63 * should return true when called IsDuplicate with same params within expirationMilliSeconds
64 * @tc.type: FUNC
65 */
66 HWTEST_F(PasteboardDeduplicateMemoryTest, TestIsDuplicate001, TestSize.Level0)
67 {
68 int64_t expirationMilliSeconds = 1000;
69 DeduplicateMemory<RadarReportIdentity> reportMemory(expirationMilliSeconds);
70
71 bool isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
72 EXPECT_FALSE(isDuplicate);
73
74 isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
75 EXPECT_TRUE(isDuplicate);
76
77 isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
78 EXPECT_TRUE(isDuplicate);
79 }
80
81 /**
82 * @tc.name: TestIsDuplicate002
83 * @tc.desc: should return false when first called IsDuplicate
84 * should return false when called IsDuplicate with same params after expirationMilliSeconds
85 * @tc.type: FUNC
86 */
87 HWTEST_F(PasteboardDeduplicateMemoryTest, TestIsDuplicate002, TestSize.Level0)
88 {
89 int64_t expirationMilliSeconds = 900;
90 DeduplicateMemory<RadarReportIdentity> reportMemory(expirationMilliSeconds);
91
92 bool isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
93 EXPECT_FALSE(isDuplicate);
94
95 std::this_thread::sleep_for(std::chrono::seconds(1));
96 isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
97 EXPECT_FALSE(isDuplicate);
98
99 std::this_thread::sleep_for(std::chrono::seconds(1));
100 isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
101 EXPECT_FALSE(isDuplicate);
102 }
103
104 /**
105 * @tc.name: TestIsDuplicate003
106 * @tc.desc: should return false when first called IsDuplicate
107 * should return true when called IsDuplicate with same params within expirationMilliSeconds
108 * should return false when called IsDuplicate with same params after expirationMilliSeconds
109 * @tc.type: FUNC
110 */
111 HWTEST_F(PasteboardDeduplicateMemoryTest, TestIsDuplicate003, TestSize.Level0)
112 {
113 int64_t expirationMilliSeconds = 1100;
114 DeduplicateMemory<RadarReportIdentity> reportMemory(expirationMilliSeconds);
115
116 bool isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
117 EXPECT_FALSE(isDuplicate);
118
119 std::this_thread::sleep_for(std::chrono::seconds(1));
120 isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
121 EXPECT_TRUE(isDuplicate);
122
123 std::this_thread::sleep_for(std::chrono::seconds(1));
124 isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
125 EXPECT_FALSE(isDuplicate);
126 }
127
128 /**
129 * @tc.name: TestIsDuplicate004
130 * @tc.desc: should return false when first called IsDuplicate
131 * should return true when called IsDuplicate with same params within expirationMilliSeconds
132 * should return false when called IsDuplicate with different params within expirationMilliSeconds
133 * @tc.type: FUNC
134 */
135 HWTEST_F(PasteboardDeduplicateMemoryTest, TestIsDuplicate004, TestSize.Level0)
136 {
137 int64_t expirationMilliSeconds = 1100;
138 DeduplicateMemory<RadarReportIdentity> reportMemory(expirationMilliSeconds);
139
140 bool isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
141 EXPECT_FALSE(isDuplicate);
142
143 std::this_thread::sleep_for(std::chrono::seconds(1));
144 isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_PARAM_ERROR});
145 EXPECT_TRUE(isDuplicate);
146
147 isDuplicate = reportMemory.IsDuplicate({.pid = 1, .errorCode = PasteboardError::INVALID_DATA_ERROR});
148 EXPECT_FALSE(isDuplicate);
149 }
150 } // namespace MiscServices
151 } // namespace OHOS
152