1 /*
2 * Copyright (c) 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 <functional>
17 #include <gtest/gtest.h>
18
19 #include "work_info.h"
20 #include "work_scheduler_connection.h"
21 #include "work_sched_hilog.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24
25 using namespace testing::ext;
26 using namespace std;
27
28 namespace OHOS {
29 namespace WorkScheduler {
30 namespace {
31 const int32_t WORK_ID = 123;
32 }
33 class WorkSchedulerConnectionTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
TearDownTestCase()36 static void TearDownTestCase() {}
SetUp()37 void SetUp() {}
TearDown()38 void TearDown() {}
39 static std::shared_ptr<WorkSchedulerConnection> workSchedulerConnection_;
40 };
41
42 std::shared_ptr<WorkSchedulerConnection> WorkSchedulerConnectionTest::workSchedulerConnection_ = nullptr;
43
SetUpTestCase()44 void WorkSchedulerConnectionTest::SetUpTestCase()
45 {
46 std::shared_ptr<WorkInfo> workInfo = std::make_shared<WorkInfo>();
47 workInfo->workId_ = WORK_ID;
48 workInfo->bundleName_ = "com.unittest.bundleName";
49 workInfo->abilityName_ = "unittestAbility";
50
51 workSchedulerConnection_ = std::make_shared<WorkSchedulerConnection>(workInfo);
52 }
53
54 /**
55 * @tc.name: StopWork_001
56 * @tc.desc: Test WorkSchedulerConnection StopWork.
57 * @tc.type: FUNC
58 * @tc.require: https://gitee.com/openharmony/resourceschedule_work_scheduler/issues/ICBI5I
59 */
60 HWTEST_F(WorkSchedulerConnectionTest, StopWork_001, TestSize.Level2)
61 {
62 workSchedulerConnection_->StopWork();
63 EXPECT_TRUE(workSchedulerConnection_->proxy_ == nullptr);
64 }
65
66 /**
67 * @tc.name: StopWork_002
68 * @tc.desc: Test WorkSchedulerConnection StopWork.
69 * @tc.type: FUNC
70 * @tc.require: https://gitee.com/openharmony/resourceschedule_work_scheduler/issues/ICBI5I
71 */
72 HWTEST_F(WorkSchedulerConnectionTest, StopWork_002, TestSize.Level2)
73 {
74 sptr<ISystemAbilityManager> SystemAbilityManager =
75 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76 ASSERT_NE(SystemAbilityManager, nullptr);
77 sptr<IRemoteObject> remoteObject = SystemAbilityManager->GetSystemAbility(WORK_SCHEDULE_SERVICE_ID);
78 ASSERT_NE(remoteObject, nullptr);
79 AppExecFwk::ElementName element;
80 int32_t resultCode = 0;
81 workSchedulerConnection_->OnAbilityConnectDone(element, remoteObject, resultCode);
82 workSchedulerConnection_->StopWork();
83 EXPECT_FALSE(workSchedulerConnection_->proxy_ == nullptr);
84 }
85 }
86 }