1 /*
2  * Copyright (c) 2022 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 "avplayback_state.h"
19 
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace AVSession {
24 class AVPlaybackStateTest : public testing::Test {
25 public:
26     static void SetUpTestCase();
27     static void TearDownTestCase();
28     void SetUp() override;
29     void TearDown() override;
30 };
31 
SetUpTestCase()32 void AVPlaybackStateTest::SetUpTestCase()
33 {}
34 
TearDownTestCase()35 void AVPlaybackStateTest::TearDownTestCase()
36 {}
37 
SetUp()38 void AVPlaybackStateTest::SetUp()
39 {}
40 
TearDown()41 void AVPlaybackStateTest::TearDown()
42 {}
43 
44 /**
45 * @tc.name: SetState001
46 * @tc.desc: Setting State with empty constructor
47 * @tc.type: FUNC
48 * @tc.require: AR000H31JM
49 */
50 HWTEST_F(AVPlaybackStateTest, SetState001, TestSize.Level1)
51 {
52     auto *avp = new (std::nothrow) AVPlaybackState();
53     EXPECT_NE(avp, nullptr);
54     avp->SetState(0);
55     avp->SetSpeed(3.0);
56     avp->SetLoopMode(1);
57     avp->SetBufferedTime(40);
58     avp->SetPosition({10, 10});
59     avp->SetFavorite(true);
60     avp->SetActiveItemId(7);
61     auto *parcel = new (std::nothrow) OHOS::Parcel();
62     EXPECT_NE(parcel, nullptr);
63     bool boo = avp->Marshalling(*parcel);
64     EXPECT_EQ(boo, true);
65 }
66 
67 /**
68 * @tc.name: IsValid001
69 * @tc.desc: Return is avplaybackstate IsValid success
70 * @tc.type: FUNC
71 * @tc.require: I5YMXD
72 */
73 HWTEST_F(AVPlaybackStateTest, IsValid001, TestSize.Level1)
74 {
75     AVPlaybackState avPlaybackState;
76     avPlaybackState.SetState(1);
77     avPlaybackState.SetSpeed(3.0);
78     avPlaybackState.SetLoopMode(1);
79     avPlaybackState.SetBufferedTime(40);
80     avPlaybackState.SetPosition({10, 10});
81     avPlaybackState.SetFavorite(true);
82     avPlaybackState.SetActiveItemId(7);
83 
84     EXPECT_EQ(avPlaybackState.IsValid(), true);
85 }
86 
87 /**
88 * @tc.name: IsValid002
89 * @tc.desc: Return is avplaybackstate IsValid failed
90 * @tc.type: FUNC
91 * @tc.require: I5YMXD
92 */
93 HWTEST_F(AVPlaybackStateTest, IsValid002, TestSize.Level1)
94 {
95     AVPlaybackState avPlaybackState;
96     avPlaybackState.SetState(-1);
97     avPlaybackState.SetSpeed(3.0);
98     avPlaybackState.SetLoopMode(1);
99     avPlaybackState.SetBufferedTime(40);
100     avPlaybackState.SetPosition({10, 10});
101     avPlaybackState.SetFavorite(true);
102     avPlaybackState.SetActiveItemId(7);
103 
104     EXPECT_EQ(avPlaybackState.IsValid(), false);
105 }
106 
107 /**
108 * @tc.name: GetState001
109 * @tc.desc: Getting state after using parcel to set
110 * @tc.type: FUNC
111 * @tc.require: AR000H31JM
112 */
113 HWTEST_F(AVPlaybackStateTest, GetState001, TestSize.Level1)
114 {
115     auto *parcel = new (std::nothrow) OHOS::Parcel();
116     EXPECT_NE(parcel, nullptr);
117     AVPlaybackState::PlaybackStateMaskType mask;
118     mask.set();
119     parcel->WriteString(mask.to_string());
120     parcel->WriteInt32(0);
121     parcel->WriteDouble(3.0);
122     parcel->WriteInt64(30);
123     parcel->WriteInt64(3);
124     parcel->WriteInt64(3);
125     parcel->WriteInt32(3);
126     parcel->WriteBool(true);
127     parcel->WriteInt32(7);
128     parcel->WriteInt32(0);
129     parcel->WriteInt32(0);
130     parcel->WriteBool(false);
131     parcel->WriteInt32(0);
132     parcel->WriteInt32(0);
133     parcel->WriteInt32(0);
134 
135     AVPlaybackState *result = AVPlaybackState::Unmarshalling(*parcel);
136     ASSERT_NE(result, nullptr);
137     EXPECT_EQ(result->GetFavorite(), true);
138 }
139 
140 /**
141 * @tc.name: GetState002
142 * @tc.desc: Getting state after using marshalling to set
143 * @tc.type: FUNC
144 * @tc.require: AR000H31JM
145 */
146 HWTEST_F(AVPlaybackStateTest, GetState002, TestSize.Level1)
147 {
148     auto *avp = new (std::nothrow) AVPlaybackState();
149     EXPECT_NE(avp, nullptr);
150     avp->SetSpeed(3.0);
151     auto *parcel = new (std::nothrow) OHOS::Parcel();
152     EXPECT_NE(parcel, nullptr);
153     bool boo = avp->Marshalling(*parcel);
154     ASSERT_EQ(boo, true);
155     AVPlaybackState *result = AVPlaybackState::Unmarshalling(*parcel);
156     ASSERT_NE(result, nullptr);
157     EXPECT_EQ(result->GetSpeed(), 3.0);
158 }
159 
160 /**
161 * @tc.name: GetMask001
162 * @tc.desc: Return is avplaybackstate GetMask success
163 * @tc.type: FUNC
164 * @tc.require: I5YMXD
165 */
166 HWTEST_F(AVPlaybackStateTest, GetMask001, TestSize.Level1)
167 {
168     AVPlaybackState avPlaybackState;
169     avPlaybackState.SetState(0);
170     avPlaybackState.SetSpeed(3.0);
171     avPlaybackState.SetLoopMode(1);
172     avPlaybackState.SetBufferedTime(40);
173     avPlaybackState.SetPosition({10, 10});
174     avPlaybackState.SetFavorite(true);
175     avPlaybackState.SetActiveItemId(7);
176 
177     EXPECT_EQ(avPlaybackState.GetMask(), 0b1111111);
178 }
179 
180 /**
181 * @tc.name: CopyToByMask001
182 * @tc.desc: Return is avplaybackstate CopyToByMask success
183 * @tc.type: FUNC
184 * @tc.require: I5YMXD
185 */
186 HWTEST_F(AVPlaybackStateTest, CopyToByMask001, TestSize.Level1)
187 {
188     AVPlaybackState stateOut;
189     stateOut.SetSpeed(3.0);
190     AVPlaybackState::PlaybackStateMaskType mask = stateOut.GetMask();
191 
192     AVPlaybackState stateTest;
193     stateTest.SetSpeed(3.0);
194     auto ret = stateTest.CopyToByMask(mask, stateOut);
195     EXPECT_EQ(ret, true);
196 }
197 
198 /**
199 * @tc.name: CopyToByMask002
200 * @tc.desc: Return is avplaybackstate CopyToByMask failed
201 * @tc.type: FUNC
202 * @tc.require: I5YMXD
203 */
204 HWTEST_F(AVPlaybackStateTest, CopyToByMask002, TestSize.Level1)
205 {
206     AVPlaybackState stateOut;
207     AVPlaybackState::PlaybackStateMaskType mask = stateOut.GetMask();
208 
209     AVPlaybackState stateTest;
210     auto ret = stateTest.CopyToByMask(mask, stateOut);
211     EXPECT_EQ(ret, false);
212 }
213 
214 /**
215 * @tc.name: CopyFrom001
216 * @tc.desc: Return is avplaybackstate CopyFrom success
217 * @tc.type: FUNC
218 * @tc.require: I5YMXD
219 */
220 HWTEST_F(AVPlaybackStateTest, CopyFrom001, TestSize.Level1)
221 {
222     AVPlaybackState stateOut;
223     stateOut.SetSpeed(3.0);
224 
225     AVPlaybackState stateTest;
226     auto ret = stateTest.CopyFrom(stateOut);
227     EXPECT_EQ(stateTest.GetSpeed(), 3.0);
228     EXPECT_EQ(ret, true);
229 }
230 
231 /**
232 * @tc.name: CopyFrom002
233 * @tc.desc: Return is avplaybackstate CopyFrom failed
234 * @tc.type: FUNC
235 * @tc.require: I5YMXD
236 */
237 HWTEST_F(AVPlaybackStateTest, CopyFrom002, TestSize.Level1)
238 {
239     AVPlaybackState stateOut;
240 
241     AVPlaybackState stateTest;
242     auto ret = stateTest.CopyFrom(stateOut);
243     EXPECT_EQ(ret, false);
244 }
245 } // namespace AVSession
246 } // namespace OHOS
247