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;
__anon7def61230202(AppExecFwk::InnerEvent *event) 84 auto destructor = [](AppExecFwk::InnerEvent *event) {
85 if (event != nullptr) {
86 delete event;
87 event = nullptr;
88 }
89 };
90 stateMachineTest_->currentState_ = nullptr;
91 int32_t ret = stateMachineTest_->Execute(AppExecFwk::InnerEvent::Pointer(event, destructor));
92 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
93 DTEST_LOG << "DSchedContinueStateMachineTest Execute_001 end" << std::endl;
94 }
95
96 /**
97 * @tc.name: UpdateState_001
98 * @tc.desc: UpdateState
99 * @tc.type: FUNC
100 */
101 HWTEST_F(DSchedContinueStateMachineTest, UpdateState_001, TestSize.Level3)
102 {
103 DTEST_LOG << "DSchedContinueStateMachineTest UpdateState_001 begin" << std::endl;
104 ASSERT_NE(stateMachineTest_, nullptr);
105 DSchedContinueStateType stateType = DSCHED_CONTINUE_SOURCE_START_STATE;
106 stateMachineTest_->UpdateState(stateType);
107 EXPECT_NE(stateMachineTest_->currentState_, nullptr);
108 DTEST_LOG << "DSchedContinueStateMachineTest UpdateState_001 end" << std::endl;
109 }
110
111 /**
112 * @tc.name: UpdateState_002
113 * @tc.desc: UpdateState
114 * @tc.type: FUNC
115 */
116 HWTEST_F(DSchedContinueStateMachineTest, UpdateState_002, TestSize.Level3)
117 {
118 DTEST_LOG << "DSchedContinueStateMachineTest UpdateState_002 begin" << std::endl;
119 ASSERT_NE(stateMachineTest_, nullptr);
120 DSchedContinueStateType stateType = DSCHED_CONTINUE_ABILITY_STATE;
121 stateMachineTest_->currentState_ = stateMachineTest_->CreateState(stateType);
122 stateMachineTest_->UpdateState(stateType);
123 EXPECT_NE(stateMachineTest_->currentState_, nullptr);
124 DTEST_LOG << "DSchedContinueStateMachineTest UpdateState_002 end" << std::endl;
125 }
126
127 /**
128 * @tc.name: CreateState_001
129 * @tc.desc: CreateState
130 * @tc.type: FUNC
131 */
132 HWTEST_F(DSchedContinueStateMachineTest, CreateState_001, TestSize.Level3)
133 {
134 DTEST_LOG << "DSchedContinueStateMachineTest CreateState_001 begin" << std::endl;
135 ASSERT_NE(stateMachineTest_, nullptr);
136 std::shared_ptr<DSchedContinueState> state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SOURCE_START_STATE);
137 EXPECT_TRUE(state != nullptr);
138
139 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_ABILITY_STATE);
140 EXPECT_TRUE(state != nullptr);
141
142 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SOURCE_WAIT_END_STATE);
143 EXPECT_TRUE(state != nullptr);
144
145 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SOURCE_END_STATE);
146 EXPECT_TRUE(state != nullptr);
147
148 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SINK_START_STATE);
149 EXPECT_TRUE(state != nullptr);
150
151 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_DATA_STATE);
152 EXPECT_TRUE(state != nullptr);
153
154 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SINK_WAIT_END_STATE);
155 EXPECT_TRUE(state != nullptr);
156
157 state = stateMachineTest_->CreateState(DSCHED_CONTINUE_SINK_END_STATE);
158 EXPECT_TRUE(state != nullptr);
159
160 state = stateMachineTest_->CreateState(static_cast<DSchedContinueStateType>(-1));
161 EXPECT_TRUE(state == nullptr);
162 DTEST_LOG << "DSchedContinueStateMachineTest CreateState_001 end" << std::endl;
163 }
164
165 /**
166 * @tc.name: GetStateType_001
167 * @tc.desc: GetStateType
168 * @tc.type: FUNC
169 */
170 HWTEST_F(DSchedContinueStateMachineTest, GetStateType_001, TestSize.Level3)
171 {
172 DTEST_LOG << "DSchedContinueStateMachineTest GetStateType_001 begin" << std::endl;
173 ASSERT_NE(stateMachineTest_, nullptr);
174 stateMachineTest_->currentState_ = nullptr;
175 DSchedContinueStateType ret = stateMachineTest_->GetStateType();
176 EXPECT_EQ(ret, DSCHED_CONTINUE_SOURCE_START_STATE);
177 DTEST_LOG << "DSchedContinueStateMachineTest GetStateType_001 end" << std::endl;
178 }
179 }
180 }
181