1 /*
2 * Copyright (c) 2021-2021 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 #define private public
18 #define protected public
19
20 #include "osal/utils/util.h"
21 #include "scene/player/internal/state_machine.h"
22 #include "scene/player/play_executor.h"
23
24 namespace OHOS {
25 namespace Media {
26 namespace Test {
27 class PlayExecutorStub : public PlayExecutor {
28 public:
PlayExecutorStub()29 PlayExecutorStub() noexcept : isLooping_(false), stateMachine_(nullptr)
30 {
31 }
SetLooping(bool looping)32 void SetLooping(bool looping)
33 {
34 isLooping_ = looping;
35 }
SetStateMachine(const StateMachine * stateMachine)36 void SetStateMachine(const StateMachine* stateMachine)
37 {
38 stateMachine_ = stateMachine;
39 }
DoSetSource(const std::shared_ptr<MediaSource> & source) const40 ErrorCode DoSetSource(const std::shared_ptr<MediaSource>& source) const override
41 {
42 return source ? ErrorCode::SUCCESS : ErrorCode::ERROR_INVALID_PARAMETER_VALUE;
43 }
DoOnReady()44 ErrorCode DoOnReady() override
45 {
46 return ErrorCode::SUCCESS;
47 }
DoOnComplete()48 ErrorCode DoOnComplete() override
49 {
50 if (!isLooping_ && stateMachine_) {
51 stateMachine_->SendEventAsync(Intent::STOP);
52 }
53 return ErrorCode::SUCCESS;
54 }
DoOnError(ErrorCode)55 ErrorCode DoOnError(ErrorCode) override
56 {
57 return ErrorCode::SUCCESS;
58 }
59
60 private:
61 bool isLooping_;
62 const StateMachine* stateMachine_;
63 };
64
65 PlayExecutorStub g_playExecutorStub;
66
67 class TestStateMachine : public ::testing::Test {
68 protected:
SetUp()69 void SetUp() override
70 {
71 g_playExecutorStub.SetStateMachine(&stateMachine);
72 stateMachine.Start();
73 }
74
TearDown()75 void TearDown() override
76 {
77 }
78
79 static StateMachine stateMachine;
80 };
81
82 StateMachine TestStateMachine::stateMachine(g_playExecutorStub);
83
TEST_F(TestStateMachine,test_init_state)84 TEST_F(TestStateMachine, test_init_state)
85 {
86 EXPECT_TRUE(stateMachine.GetCurrentState() == "InitState");
87 }
88
TEST_F(TestStateMachine,test_set_invalid_source)89 TEST_F(TestStateMachine, test_set_invalid_source)
90 {
91 EXPECT_TRUE(stateMachine.GetCurrentState() == "InitState");
92 EXPECT_EQ(ErrorCode::ERROR_INVALID_PARAMETER_TYPE, stateMachine.SendEvent(Intent::SET_SOURCE));
93 EXPECT_TRUE(stateMachine.GetCurrentState() == "InitState");
94 }
95
TEST_F(TestStateMachine,test_set_valid_source)96 TEST_F(TestStateMachine, test_set_valid_source)
97 {
98 EXPECT_TRUE(stateMachine.GetCurrentState() == "InitState");
99 auto source = std::make_shared<MediaSource>("FakeUri");
100 EXPECT_EQ(ErrorCode::SUCCESS, stateMachine.SendEvent(Intent::SET_SOURCE, source));
101 EXPECT_TRUE(stateMachine.GetCurrentState() == "PreparingState");
102 }
103
TEST_F(TestStateMachine,test_set_notify_error_in_preparing)104 TEST_F(TestStateMachine, test_set_notify_error_in_preparing)
105 {
106 EXPECT_TRUE(stateMachine.GetCurrentState() == "PreparingState");
107 EXPECT_EQ(ErrorCode::SUCCESS, stateMachine.SendEvent(Intent::NOTIFY_ERROR));
108 EXPECT_TRUE(stateMachine.GetCurrentState() == "InitState");
109 }
110
TEST_F(TestStateMachine,test_notify_ready)111 TEST_F(TestStateMachine, test_notify_ready)
112 {
113 EXPECT_TRUE(stateMachine.GetCurrentState() == "InitState");
114 auto source = std::make_shared<MediaSource>("FakeUri");
115 EXPECT_EQ(ErrorCode::SUCCESS, stateMachine.SendEvent(Intent::SET_SOURCE, source));
116 EXPECT_TRUE(stateMachine.GetCurrentState() == "PreparingState");
117 EXPECT_EQ(ErrorCode::SUCCESS, stateMachine.SendEvent(Intent::NOTIFY_READY));
118 EXPECT_TRUE(stateMachine.GetCurrentState() == "ReadyState");
119 }
120
TEST_F(TestStateMachine,test_play_after_ready)121 TEST_F(TestStateMachine, test_play_after_ready)
122 {
123 EXPECT_TRUE(stateMachine.GetCurrentState() == "ReadyState");
124 EXPECT_EQ(ErrorCode::SUCCESS, stateMachine.SendEvent(Intent::PLAY));
125 EXPECT_TRUE(stateMachine.GetCurrentState() == "PlayingState");
126 }
127
TEST_F(TestStateMachine,test_pause)128 TEST_F(TestStateMachine, test_pause)
129 {
130 EXPECT_TRUE(stateMachine.GetCurrentState() == "PlayingState");
131 EXPECT_EQ(ErrorCode::SUCCESS, stateMachine.SendEvent(Intent::PAUSE));
132 EXPECT_TRUE(stateMachine.GetCurrentState() == "PauseState");
133 }
134
TEST_F(TestStateMachine,test_play_after_pause)135 TEST_F(TestStateMachine, test_play_after_pause)
136 {
137 EXPECT_TRUE(stateMachine.GetCurrentState() == "PauseState");
138 EXPECT_EQ(ErrorCode::SUCCESS, stateMachine.SendEvent(Intent::PLAY));
139 EXPECT_TRUE(stateMachine.GetCurrentState() == "PlayingState");
140 }
141
TEST_F(TestStateMachine,test_play_complete_looping)142 TEST_F(TestStateMachine, test_play_complete_looping)
143 {
144 g_playExecutorStub.SetLooping(true);
145 EXPECT_TRUE(stateMachine.GetCurrentState() == "PlayingState");
146 EXPECT_EQ(ErrorCode::SUCCESS, stateMachine.SendEvent(Intent::NOTIFY_COMPLETE));
147 EXPECT_TRUE(stateMachine.GetCurrentState() == "PlayingState");
148 }
149
TEST_F(TestStateMachine,test_play_complete_nolooping)150 TEST_F(TestStateMachine, test_play_complete_nolooping)
151 {
152 g_playExecutorStub.SetLooping(false);
153 EXPECT_TRUE(stateMachine.GetCurrentState() == "PlayingState");
154 EXPECT_EQ(ErrorCode::SUCCESS, stateMachine.SendEvent(Intent::NOTIFY_COMPLETE));
155 OSAL::SleepFor(100);
156 EXPECT_TRUE(stateMachine.GetCurrentState() == "InitState");
157 }
158 } // namespace Test
159 } // namespace Media
160 } // namespace OHOS