1 /*
2 * Copyright (c) 2024-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 "dsched_continue_state_test.h"
17
18 #include "dsched_continue.h"
19 #include "dtbschedmgr_log.h"
20 #include "test_log.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace DistributedSchedule {
27 namespace {
28 const std::string BASEDIR = "/data/service/el1/public/database/DistributedSchedule";
29 const int32_t WAITTIME = 2000;
30 }
31
32 //DSchedContinueStateMachineTest
SetUpTestCase()33 void DSchedContinueStateMachineTest::SetUpTestCase()
34 {
35 mkdir(BASEDIR.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
36 int32_t subServiceType = 0;
37 int32_t direction = 0;
38 sptr<IRemoteObject> callback = nullptr;
39 DSchedContinueInfo continueInfo;
40 dContinue_ = std::make_shared<DSchedContinue>(subServiceType, direction,
41 callback, continueInfo);
42 dContinue_->Init();
43 stateMachineTest_ = std::make_shared<DSchedContinueStateMachine>(dContinue_);
44 DTEST_LOG << "DSchedContinueStateMachineTest::SetUpTestCase" << std::endl;
45 }
46
TearDownTestCase()47 void DSchedContinueStateMachineTest::TearDownTestCase()
48 {
49 (void)remove(BASEDIR.c_str());
50 DTEST_LOG << "DSchedContinueStateMachineTest::TearDownTestCase" << std::endl;
51 }
52
TearDown()53 void DSchedContinueStateMachineTest::TearDown()
54 {
55 usleep(WAITTIME);
56 DTEST_LOG << "DSchedContinueStateMachineTest::TearDown" << std::endl;
57 }
58
SetUp()59 void DSchedContinueStateMachineTest::SetUp()
60 {
61 DTEST_LOG << "DSchedContinueStateMachineTest::SetUp" << std::endl;
62 }
63
ExecuteQuickStartSuccess()64 int32_t DSchedContinue::ExecuteQuickStartSuccess()
65 {
66 return ERR_OK;
67 }
68
ExecuteQuickStartFailed(int32_t result)69 int32_t DSchedContinue::ExecuteQuickStartFailed(int32_t result)
70 {
71 return ERR_OK;
72 }
73
74 /**
75 * @tc.name: Execute_001
76 * @tc.desc: Execute
77 * @tc.type: FUNC
78 */
79 HWTEST_F(DSchedContinueStateMachineTest, Execute_001, TestSize.Level3)
80 {
81 DTEST_LOG << "DSchedContinueStateMachineTest Execute_001 begin" << std::endl;
82 ASSERT_NE(stateMachineTest_, nullptr);
83 AppExecFwk::InnerEvent *event = nullptr;
__anon9d6dc7410202(AppExecFwk::InnerEvent *event) 84 auto destructor = [](AppExecFwk::InnerEvent *event) {
85 if (event != nullptr) {
86 delete event;
87 }
88 };
89 stateMachineTest_->currentState_ = nullptr;
90 int32_t ret = stateMachineTest_->Execute(AppExecFwk::InnerEvent::Pointer(event, destructor));
91 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
92 DTEST_LOG << "DSchedContinueStateMachineTest Execute_001 end" << std::endl;
93 }
94
95 /**
96 * @tc.name: UpdateState_001
97 * @tc.desc: UpdateState
98 * @tc.type: FUNC
99 */
100 HWTEST_F(DSchedContinueStateMachineTest, UpdateState_001, TestSize.Level3)
101 {
102 DTEST_LOG << "DSchedContinueStateMachineTest UpdateState_001 begin" << std::endl;
103 ASSERT_NE(stateMachineTest_, nullptr);
104 DSchedContinueStateType stateType = DSCHED_CONTINUE_SOURCE_START_STATE;
105 stateMachineTest_->UpdateState(stateType);
106 EXPECT_NE(stateMachineTest_->currentState_, nullptr);
107 DTEST_LOG << "DSchedContinueStateMachineTest UpdateState_001 end" << std::endl;
108 }
109
110 /**
111 * @tc.name: UpdateState_002
112 * @tc.desc: UpdateState
113 * @tc.type: FUNC
114 */
115 HWTEST_F(DSchedContinueStateMachineTest, UpdateState_002, TestSize.Level3)
116 {
117 DTEST_LOG << "DSchedContinueStateMachineTest UpdateState_002 begin" << std::endl;
118 ASSERT_NE(stateMachineTest_, nullptr);
119 DSchedContinueStateType stateType = DSCHED_CONTINUE_ABILITY_STATE;
120 stateMachineTest_->currentState_ = stateMachineTest_->CreateState(stateType);
121 stateMachineTest_->UpdateState(stateType);
122 EXPECT_NE(stateMachineTest_->currentState_, nullptr);
123 DTEST_LOG << "DSchedContinueStateMachineTest UpdateState_002 end" << std::endl;
124 }
125
126 /**
127 * @tc.name: CreateState_001
128 * @tc.desc: CreateState
129 * @tc.type: FUNC
130 */
131 HWTEST_F(DSchedContinueStateMachineTest, CreateState_001, TestSize.Level3)
132 {
133 DTEST_LOG << "DSchedContinueStateMachineTest CreateState_001 begin" << std::endl;
134 ASSERT_NE(stateMachineTest_, nullptr);
135 std::shared_ptr<DSchedContinueState> state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SOURCE_START_STATE);
136 EXPECT_TRUE(state != nullptr);
137
138 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_ABILITY_STATE);
139 EXPECT_TRUE(state != nullptr);
140
141 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SOURCE_WAIT_END_STATE);
142 EXPECT_TRUE(state != nullptr);
143
144 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SOURCE_END_STATE);
145 EXPECT_TRUE(state != nullptr);
146
147 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SINK_START_STATE);
148 EXPECT_TRUE(state != nullptr);
149
150 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_DATA_STATE);
151 EXPECT_TRUE(state != nullptr);
152
153 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SINK_WAIT_END_STATE);
154 EXPECT_TRUE(state != nullptr);
155
156 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SINK_END_STATE);
157 EXPECT_TRUE(state != nullptr);
158
159 state = stateMachineTest_->CreateState(static_cast<DSchedContinueStateType>(-1));
160 EXPECT_TRUE(state == nullptr);
161 DTEST_LOG << "DSchedContinueStateMachineTest CreateState_001 end" << std::endl;
162 }
163
164 /**
165 * @tc.name: GetStateType_001
166 * @tc.desc: GetStateType
167 * @tc.type: FUNC
168 */
169 HWTEST_F(DSchedContinueStateMachineTest, GetStateType_001, TestSize.Level3)
170 {
171 DTEST_LOG << "DSchedContinueStateMachineTest GetStateType_001 begin" << std::endl;
172 ASSERT_NE(stateMachineTest_, nullptr);
173 stateMachineTest_->currentState_ = nullptr;
174 DSchedContinueStateType ret = stateMachineTest_->GetStateType();
175 EXPECT_EQ(ret, DSCHED_CONTINUE_SOURCE_START_STATE);
176 DTEST_LOG << "DSchedContinueStateMachineTest GetStateType_001 end" << std::endl;
177 }
178 }
179 }
180