1 /*
2  * Copyright (c) 2023 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 "system_ability_definition.h"
20 #include "iservice_registry.h"
21 #include "work_scheduler_proxy.h"
22 #include "work_sched_hilog.h"
23 #include "ffrt.h"
24 
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace WorkScheduler {
29 
30 class WorkSchedulerProxyTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
TearDownTestCase()33     static void TearDownTestCase() {}
SetUp()34     void SetUp() {}
TearDown()35     void TearDown() {}
36     static std::shared_ptr<WorkSchedulerProxy> workSchedulerProxy_;
37 };
38 
39 std::shared_ptr<WorkSchedulerProxy> WorkSchedulerProxyTest::workSchedulerProxy_ = nullptr;
40 ffrt::mutex mutexLock;
41 
SetUpTestCase()42 void WorkSchedulerProxyTest::SetUpTestCase()
43 {
44     std::lock_guard<ffrt::mutex> lock(mutexLock);
45     sptr<ISystemAbilityManager> SystemAbilityManager =
46         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
47     if (SystemAbilityManager == nullptr) {
48         return;
49     }
50     sptr<IRemoteObject> remoteObject = SystemAbilityManager->GetSystemAbility(WORK_SCHEDULE_SERVICE_ID);
51     if (remoteObject == nullptr) {
52         return;
53     }
54     workSchedulerProxy_ = std::make_shared<WorkSchedulerProxy>(remoteObject);
55 }
56 
57 /**
58  * @tc.name: OnWorkStart_001
59  * @tc.desc: Test WorkSchedulerProxy OnWorkStart.
60  * @tc.type: FUNC
61  * @tc.require: #I9HP1I
62  */
63 HWTEST_F(WorkSchedulerProxyTest, OnWorkStart_001, TestSize.Level1)
64 {
65     WorkInfo workInfo;
66     workSchedulerProxy_->OnWorkStart(workInfo);
67     EXPECT_TRUE(workInfo.GetBundleName().empty());
68 }
69 
70 /**
71  * @tc.name: OnWorkStop_001
72  * @tc.desc: Test WorkSchedulerProxy OnWorkStop.
73  * @tc.type: FUNC
74  * @tc.require: #I9HP1I
75  */
76 HWTEST_F(WorkSchedulerProxyTest, OnWorkStop_001, TestSize.Level1)
77 {
78     WorkInfo workInfo;
79     workSchedulerProxy_->OnWorkStop(workInfo);
80     EXPECT_TRUE(workInfo.GetBundleName().empty());
81 }
82 }
83 }