1 /*
2 * Copyright (c) 2021-2022 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 "event_handler.h"
19 #include "event_queue.h"
20 #include "event_runner.h"
21 #include "inner_event.h"
22
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AppExecFwk;
26
27 namespace {
28 const uint32_t EVENT_ID = 0;
29 const uint32_t EVENT_PARAM = 10000000;
30 const uint32_t DELAY_TIME = 10;
31 const std::string THREAD_NAME_TEST1 = "threadNameTest1";
32 const std::string THREAD_NAME_TEST2 = " ";
33 const std::string THREAD_NAME_TEST3 = "@#¥#3243adsafdf_中文";
34 const std::string THREAD_NAME_TEST4 = std::to_string(0);
35 const std::string THREAD_NAME_TEST5 = std::to_string(0) + "@#¥#3243adsafdf_中文";
36 } // namespace
37
38 class EmsEventQueueSystemTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 };
45
SetUpTestCase(void)46 void EmsEventQueueSystemTest::SetUpTestCase(void)
47 {}
48
TearDownTestCase(void)49 void EmsEventQueueSystemTest::TearDownTestCase(void)
50 {}
51
SetUp(void)52 void EmsEventQueueSystemTest::SetUp(void)
53 {}
54
TearDown(void)55 void EmsEventQueueSystemTest::TearDown(void)
56 {}
57
58 /*
59 * @tc.name: IsQueueEmpty001
60 * @tc.desc: check when queue is empty IsQueueEmpty return true
61 * @tc.type: FUNC
62 */
63 HWTEST_F(EmsEventQueueSystemTest, IsQueueEmpty001, TestSize.Level1)
64 {
65 std::shared_ptr<EventQueue> queue = std::make_shared<EventQueue>();
66 bool ret = queue->IsQueueEmpty();
67 EXPECT_TRUE(ret);
68 }
69
70 /*
71 * @tc.name: IsQueueEmpty002
72 * @tc.desc: check when queue is not empty has low event IsQueueEmpty return false
73 * @tc.type: FUNC
74 */
75 HWTEST_F(EmsEventQueueSystemTest, IsQueueEmpty002, TestSize.Level1)
76 {
77 auto runner = EventRunner::Create(true);
78 auto handler = std::make_shared<EventHandler>(runner);
79 auto event = InnerEvent::Get(EVENT_ID, EVENT_PARAM);
80
81 /**
82 * @tc.steps: step1. send event and IsQueueEmpty
83 * @tc.expected: step1. when queue is not empty has low event IsQueueEmpty return false
84 */
85 handler->SendEvent(event, DELAY_TIME, EventQueue::Priority::LOW);
86 std::shared_ptr<EventQueue> queue = std::make_shared<EventQueue>();
87 bool ret = queue->IsQueueEmpty();
88 EXPECT_TRUE(ret);
89 }
90
91 /*
92 * @tc.name: IsQueueEmpty003
93 * @tc.desc: check when queue is not empty has idle event IsQueueEmpty return false
94 * @tc.type: FUNC
95 */
96 HWTEST_F(EmsEventQueueSystemTest, IsQueueEmpty003, TestSize.Level1)
97 {
98 auto runner = EventRunner::Create(true);
99 auto handler = std::make_shared<EventHandler>(runner);
100 auto event = InnerEvent::Get(EVENT_ID, EVENT_PARAM);
101 handler->SendEvent(event, DELAY_TIME, EventQueue::Priority::IMMEDIATE);
102 std::shared_ptr<EventQueue> queue = std::make_shared<EventQueue>();
103 bool ret = queue->IsQueueEmpty();
104 EXPECT_TRUE(ret);
105 }
106
107 /*
108 * @tc.name: IsIdle001
109 * @tc.desc: check when queue is not empty has low event IsQueueEmpty return false
110 * @tc.type: FUNC
111 */
112 HWTEST_F(EmsEventQueueSystemTest, IsIdle001, TestSize.Level1)
113 {
114 std::shared_ptr<EventQueue> queue = std::make_shared<EventQueue>();
115 bool ret = queue->IsIdle();
116 EXPECT_TRUE(ret);
117 }
118
119 /*
120 * @tc.name: HasInnerEvent
121 * @tc.desc: check when queue is not empty has low event IsQueueEmpty return false
122 * @tc.type: FUNC
123 */
124 HWTEST_F(EmsEventQueueSystemTest, HasInnerEvent001, TestSize.Level1)
125 {
126 auto runner = EventRunner::Create(true);
127 auto handler = std::make_shared<EventHandler>(runner);
128 std::shared_ptr<EventQueue> queue = std::make_shared<EventQueue>();
129 bool ret = queue->HasInnerEvent(handler, EVENT_ID);
130 EXPECT_FALSE(ret);
131 }