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/storage_listener.h"
20 #include "work_scheduler_service.h"
21 #include "work_queue_manager.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace WorkScheduler {
29 class StorageListenerTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
TearDownTestCase()32 static void TearDownTestCase() {};
SetUp()33 void SetUp() {};
TearDown()34 void TearDown() {};
35 static std::shared_ptr<WorkQueueManager> workQueueManager_;
36 static std::shared_ptr<StorageListener> storageListener_;
37 };
38
39 std::shared_ptr<WorkQueueManager> StorageListenerTest::workQueueManager_ = nullptr;
40 std::shared_ptr<StorageListener> StorageListenerTest::storageListener_ = nullptr;
41
SetUpTestCase()42 void StorageListenerTest::SetUpTestCase()
43 {
44 std::shared_ptr<WorkSchedulerService> workSchedulerService_ = std::make_shared<WorkSchedulerService>();
45 workQueueManager_ = std::make_shared<WorkQueueManager>(workSchedulerService_);
46 storageListener_ = std::make_shared<StorageListener>(workQueueManager_);
47 }
48
49 /**
50 * @tc.name: OnConditionChanged_001
51 * @tc.desc: Test StorageListener OnConditionChanged.
52 * @tc.type: FUNC
53 * @tc.require: IB7RQR
54 */
55 HWTEST_F(StorageListenerTest, OnConditionChanged_001, TestSize.Level1)
56 {
57 storageListener_->Start();
58 EventFwk::CommonEventData data;
59 storageListener_->commonEventSubscriber->OnReceiveEvent(data);
60 bool ret = storageListener_->Stop();
61 EXPECT_TRUE(ret);
62 }
63
64 /**
65 * @tc.name: OnConditionChanged_002
66 * @tc.desc: Test StorageListener OnConditionChanged.
67 * @tc.type: FUNC
68 * @tc.require: IB7RQR
69 */
70 HWTEST_F(StorageListenerTest, OnConditionChanged_002, TestSize.Level1)
71 {
72 storageListener_->Start();
73 EventFwk::CommonEventData data;
74 EventFwk::Want want;
75 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_LOW);
76 data.SetWant(want);
77 storageListener_->commonEventSubscriber->OnReceiveEvent(data);
78 bool ret = storageListener_->Stop();
79 EXPECT_TRUE(ret);
80 }
81
82 /**
83 * @tc.name: OnConditionChanged_003
84 * @tc.desc: Test StorageListener OnConditionChanged.
85 * @tc.type: FUNC
86 * @tc.require: IB7RQR
87 */
88 HWTEST_F(StorageListenerTest, OnConditionChanged_003, TestSize.Level1)
89 {
90 storageListener_->Start();
91 EventFwk::CommonEventData data;
92 EventFwk::Want want;
93 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_OK);
94 data.SetWant(want);
95 storageListener_->commonEventSubscriber->OnReceiveEvent(data);
96 bool ret = storageListener_->Stop();
97 EXPECT_TRUE(ret);
98 }
99
100 /**
101 * @tc.name: OnConditionChanged_004
102 * @tc.desc: Test StorageListener OnConditionChanged.
103 * @tc.type: FUNC
104 * @tc.require: IB7RQR
105 */
106 HWTEST_F(StorageListenerTest, OnConditionChanged_004, TestSize.Level1)
107 {
108 storageListener_->Start();
109 EventFwk::CommonEventData data;
110 EventFwk::Want want;
111 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_FULL);
112 data.SetWant(want);
113 storageListener_->commonEventSubscriber->OnReceiveEvent(data);
114 bool ret = storageListener_->Stop();
115 EXPECT_TRUE(ret);
116 }
117 }
118 }