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 <gtest/gtest.h>
17
18 #include "common_event_adapter.h"
19 #include "common_event_observer.h"
20 #include "devicestatus_define.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "CommonEventAdapterTest"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 using namespace testing::ext;
29 namespace {
30 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
31 std::shared_ptr<ICommonEventObserver> g_observer { nullptr };
32 } // namespace
33
34 class CommonEventAdapterTest : public testing::Test {
35 public:
36 void SetUp();
37 void TearDown();
38 static void SetUpTestCase();
39 };
40
SetUpTestCase()41 void CommonEventAdapterTest::SetUpTestCase() {}
42
SetUp()43 void CommonEventAdapterTest::SetUp() {}
44
TearDown()45 void CommonEventAdapterTest::TearDown()
46 {
47 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
48 }
49
50 /**
51 * @tc.name: CommonEventAdapterTest
52 * @tc.desc: CommonEventAdapterTest001
53 * @tc.type: FUNC
54 * @tc.require:
55 */
56 HWTEST_F(CommonEventAdapterTest, CommonEventAdapterTest001, TestSize.Level1)
57 {
58 CALL_TEST_DEBUG;
59 CommonEventAdapter commonEvent;
60 int32_t ret = commonEvent.AddObserver(g_observer);
61 EXPECT_EQ(ret, RET_ERR);
62 }
63
64 /**
65 * @tc.name: CommonEventAdapterTest
66 * @tc.desc: CommonEventAdapterTest002
67 * @tc.type: FUNC
68 * @tc.require:
69 */
70 HWTEST_F(CommonEventAdapterTest, CommonEventAdapterTest002, TestSize.Level1)
71 {
72 CALL_TEST_DEBUG;
73 std::string commonevent = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON;
74 g_observer = CommonEventObserver::CreateCommonEventObserver(
__anon87b87c290202(const std::string &event) 75 [this] (const std::string &event) {
76 return ;
77 }
78 );
79 ASSERT_NE(g_observer, nullptr);
80 OHOS::AAFwk::Want want;
81 EventFwk::CommonEventData event;
82 ASSERT_NO_FATAL_FAILURE(g_observer->OnReceiveEvent(event));
83 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
84 event.SetWant(want);
85 ASSERT_NO_FATAL_FAILURE(g_observer->OnReceiveEvent(event));
86 }
87 } // namespace DeviceStatus
88 } // namespace Msdp
89 } // namespace OHOS