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