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