• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <limits>
18 #include <test_header.h>
19 
20 #include "hgm_test_base.h"
21 #include "hgm_voter.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 std::vector<std::string> voters = { "voter1", "voter2" };
30 }
31 
32 class DeliverVoteTest : public HgmTestBase {
33 public:
SetUpTestCase()34     static void SetUpTestCase()
35     {
36         HgmTestBase::SetUpTestCase();
37     }
TearDownTestCase()38     static void TearDownTestCase() {}
SetUp()39     void SetUp() {}
TearDown()40     void TearDown() {}
41 };
42 
43 /**
44  * @tc.name  : DeliverVote
45  * @tc.number: DeliverVoteTest_001
46  * @tc.desc  : When there are no voters, DeliverVote should return false
47  */
48 HWTEST_F(DeliverVoteTest, DeliverVoteTest_001, Function | SmallTest | Level0) {
49     auto voter = std::make_shared<HgmVoter>(voters);
50     VoteInfo voteInfo;
51     voteInfo.voterName = "nonexistentVoter";
52     voteInfo.pid = 123;
53     voteInfo.min = 1;
54     voteInfo.max = 100;
55 
56     bool result = voter->DeliverVote(voteInfo, true);
57     EXPECT_FALSE(result);
58 }
59 
60 /**
61  * @tc.name  : DeliverVote
62  * @tc.number: DeliverVoteTest_002
63  * @tc.desc  : When pid is 0 and eventStatus is false,
64 DeliverVote should clear vec and return true
65  */
66 HWTEST_F(DeliverVoteTest, DeliverVoteTest_002, Function | SmallTest | Level0) {
67     auto voter = std::make_shared<HgmVoter>(voters);
68     VoteInfo voteInfo;
69     voteInfo.voterName = "voter1";
70     voteInfo.pid = 0;
71     voteInfo.min = 1;
72     voteInfo.max = 100;
73 
74     voter->voteRecord_["voter1"].first.push_back(voteInfo);
75 
76     bool result = voter->DeliverVote(voteInfo, false);
77     EXPECT_TRUE(result);
78     EXPECT_TRUE(voter->voteRecord_["voter1"].first.empty());
79 }
80 
81 /**
82  * @tc.name  : DeliverVote
83  * @tc.number: DeliverVoteTest_003
84  * @tc.desc  : When eventStatus is false and pid matches,
85 DeliverVote should delete the matching record and return true
86  */
87 HWTEST_F(DeliverVoteTest, DeliverVoteTest_003, Function | SmallTest | Level0) {
88     auto voter = std::make_shared<HgmVoter>(voters);
89     VoteInfo voteInfo;
90     voteInfo.voterName = "voter1";
91     voteInfo.pid = 123;
92     voteInfo.min = 1;
93     voteInfo.max = 100;
94 
95     voter->voteRecord_["voter1"].first.push_back(voteInfo);
96 
97     bool result = voter->DeliverVote(voteInfo, false);
98     EXPECT_TRUE(result);
99     EXPECT_TRUE(voter->voteRecord_["voter1"].first.empty());
100 }
101 
102 /**
103  * @tc.name  : DeliverVote
104  * @tc.number: DeliverVoteTest_004
105  * @tc.desc  : When eventStatus is true and pid matches,
106 DeliverVote should modify the matched record and return true.
107  */
108 HWTEST_F(DeliverVoteTest, DeliverVoteTest_004, Function | SmallTest | Level0) {
109     auto voter = std::make_shared<HgmVoter>(voters);
110     VoteInfo voteInfo;
111     voteInfo.voterName = "voter1";
112     voteInfo.pid = 123;
113     voteInfo.min = 1;
114     voteInfo.max = 100;
115 
116     voter->voteRecord_["voter1"].first.push_back(voteInfo);
117     bool result = voter->DeliverVote(voteInfo, true);
118     EXPECT_FALSE(result);
119 
120     voteInfo.min = 1;
121     voteInfo.max = 120;
122     result = voter->DeliverVote(voteInfo, true);
123     EXPECT_TRUE(result);
124     EXPECT_EQ(voter->voteRecord_["voter1"].first.size(), 1);
125     EXPECT_EQ(voter->voteRecord_["voter1"].first[0].min, 1);
126     EXPECT_EQ(voter->voteRecord_["voter1"].first[0].max, 120);
127 
128     voteInfo.min = 20;
129     voteInfo.max = 100;
130     result = voter->DeliverVote(voteInfo, true);
131     EXPECT_TRUE(result);
132     EXPECT_EQ(voter->voteRecord_["voter1"].first.size(), 1);
133     EXPECT_EQ(voter->voteRecord_["voter1"].first[0].min, 20);
134     EXPECT_EQ(voter->voteRecord_["voter1"].first[0].max, 100);
135 
136     voteInfo.min = 20;
137     voteInfo.max = 120;
138     result = voter->DeliverVote(voteInfo, true);
139     EXPECT_TRUE(result);
140     EXPECT_EQ(voter->voteRecord_["voter1"].first.size(), 1);
141     EXPECT_EQ(voter->voteRecord_["voter1"].first[0].min, 20);
142     EXPECT_EQ(voter->voteRecord_["voter1"].first[0].max, 120);
143 }
144 
145 /**
146  * @tc.name  : DeliverVote
147  * @tc.number: DeliverVoteTest_005
148  * @tc.desc  : When eventStatus is true and pid does not match,
149 DeliverVote should add a new record and return true
150  */
151 HWTEST_F(DeliverVoteTest, DeliverVoteTest_005, Function | SmallTest | Level0) {
152     auto voter = std::make_shared<HgmVoter>(voters);
153     VoteInfo voteInfo;
154     voteInfo.voterName = "voter1";
155     voteInfo.pid = 456;
156     voteInfo.min = 1;
157     voteInfo.max = 100;
158 
159     bool result = voter->DeliverVote(voteInfo, true);
160     EXPECT_TRUE(result);
161     EXPECT_EQ(voter->voteRecord_["voter1"].first.size(), 1);
162     EXPECT_EQ(voter->voteRecord_["voter1"].first[0].pid, 456);
163 }
164 } // namespace Rosen
165 } // namespace OHOS