1 /*
2 * Copyright (c) 2024 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 "conditions/group_listener.h"
20 #include "work_scheduler_service.h"
21 #include "work_queue_manager.h"
22
23 using namespace OHOS::AppExecFwk;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace WorkScheduler {
28
29 const std::string WORKSCHEDULER_SERVICE_NAME = "WorkSchedulerService";
30
31 class GroupListenerTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
TearDownTestCase()34 static void TearDownTestCase() {};
SetUp()35 void SetUp() {};
TearDown()36 void TearDown() {};
37 static std::shared_ptr<WorkQueueManager> workQueueManager_;
38 static std::shared_ptr<GroupListener> groupListener_;
39 };
40
41 std::shared_ptr<WorkQueueManager> GroupListenerTest::workQueueManager_ = nullptr;
42 std::shared_ptr<GroupListener> GroupListenerTest::groupListener_ = nullptr;
43
SetUpTestCase()44 void GroupListenerTest::SetUpTestCase()
45 {
46 std::shared_ptr<WorkSchedulerService> workSchedulerService_ = std::make_shared<WorkSchedulerService>();
47 workQueueManager_ = std::make_shared<WorkQueueManager>(workSchedulerService_);
48 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_ = AppExecFwk::EventRunner::Create(WORKSCHEDULER_SERVICE_NAME,
49 AppExecFwk::ThreadMode::FFRT);
50 groupListener_ = std::make_shared<GroupListener>(workQueueManager_, eventRunner_);
51 }
52
53 /**
54 * @tc.name: OnConditionChanged_001
55 * @tc.desc: Test GroupListener OnConditionChanged.
56 * @tc.type: FUNC
57 * @tc.require: IB7RQR
58 */
59 HWTEST_F(GroupListenerTest, OnConditionChanged_001, TestSize.Level1)
60 {
61 groupListener_->Start();
62 int32_t newGroup = 10;
63 int32_t userId = 100;
64 std::string bundleName = "com.ohos.sceneboard";
65 workQueueManager_->OnConditionChanged(WorkCondition::Type::GROUP,
66 std::make_shared<DetectorValue>(newGroup, userId, true, bundleName));
67 bool ret = groupListener_->Stop();
68 EXPECT_TRUE(ret);
69 }
70 }
71 }