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