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 "event_publisher.h"
20 #include "work_sched_hilog.h"
21
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace WorkScheduler {
26
27 class EventPublisherTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
TearDownTestCase()30 static void TearDownTestCase() {}
SetUp()31 void SetUp() {}
TearDown()32 void TearDown() {}
33 static std::shared_ptr<EventPublisher> eventPublisher_;
34 };
35
36 std::shared_ptr<EventPublisher> EventPublisherTest::eventPublisher_ = nullptr;
37
SetUpTestCase()38 void EventPublisherTest::SetUpTestCase()
39 {
40 eventPublisher_ = std::make_shared<EventPublisher>();
41 }
42
43 /**
44 * @tc.name: publishEvent_001
45 * @tc.desc: Test EventPublisher PublishEvent.
46 * @tc.type: FUNC
47 * @tc.require: I8GHCL
48 */
49 HWTEST_F(EventPublisherTest, publishEvent_001, TestSize.Level1)
50 {
51 std::string result;
52 std::string eventType;
53 std::string eventValue;
54 eventPublisher_->PublishEvent(result, eventType, eventValue);
55 EXPECT_EQ(result, std::string("dump -d need right params."));
56 }
57
58 HWTEST_F(EventPublisherTest, Dump_001, TestSize.Level1)
59 {
60 std::vector<std::pair<string, string>> infos = {
61 {"event", "info"},
62 {"network", "wifi"},
63 {"network", "disconnect"},
64 {"network", "invalid"},
65 {"charging", "usb"},
66 {"charging", "ac"},
67 {"charging", "wireless"},
68 {"charging", "none"},
69 {"charging", "invalid"},
70 {"storage", "low"},
71 {"storage", "ok"},
72 {"storage", "invalid"},
73 {"batteryStatus", "low"},
74 {"batteryStatus", "ok"},
75 {"batteryStatus", "invalid"},
76 };
77 for (auto it : infos) {
78 std::string result;
79 std::string eventType = it.first;
80 std::string eventValue = it.second;
81 eventPublisher_->Dump(result, eventType, eventValue);
82 WS_HILOGI("%{public}s", result.c_str());
83 EXPECT_EQ(!result.empty(), true);
84 }
85 }
86 }
87 }