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 #include <gtest/gtest.h>
16 
17 #include "circle_stream_buffer.h"
18 #include "key_event_napi.h"
19 #include "stream_buffer.h"
20 
21 namespace OHOS {
22 namespace MMI {
23 namespace {
24 using namespace testing::ext;
25 } // namespace
26 class CircleStreamBufferTest : public testing::Test {
27 public:
SetUpTestCase(void)28     static void SetUpTestCase(void) {}
TearDownTestCase(void)29     static void TearDownTestCase(void) {}
30 };
31 
32 /**
33  * @tc.name:CircleStreamBufferTest_CopyDataToBegin_001
34  * @tc.desc:Test the funcation CopyDataToBegin
35  * @tc.type: FUNC
36  * @tc.require:
37  */
HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CopyDataToBegin_001, TestSize.Level1)38 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CopyDataToBegin_001, TestSize.Level1)
39 {
40     CircleStreamBuffer circleStreamBuffer;
41     StreamBuffer streamBuffer;
42     streamBuffer.wPos_ = 10;
43     streamBuffer.rPos_ = 5;
44     ASSERT_NO_FATAL_FAILURE(circleStreamBuffer.CopyDataToBegin());
45     streamBuffer.rPos_ = -5;
46     ASSERT_NO_FATAL_FAILURE(circleStreamBuffer.CopyDataToBegin());
47     streamBuffer.wPos_ = 5;
48     streamBuffer.rPos_ = 10;
49     ASSERT_NO_FATAL_FAILURE(circleStreamBuffer.CopyDataToBegin());
50 }
51 
52 /**
53  * @tc.name:CircleStreamBufferTest_CheckWrite_001
54  * @tc.desc:Test the funcation CheckWrite
55  * @tc.type: FUNC
56  * @tc.require:
57  */
HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CheckWrite_001, TestSize.Level1)58 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CheckWrite_001, TestSize.Level1)
59 {
60     CircleStreamBuffer circleStreamBuffer;
61     StreamBuffer streamBuffer;
62     size_t size = 4;
63     streamBuffer.wPos_ = 20000;
64     streamBuffer.rPos_ = 1;
65     bool ret = circleStreamBuffer.CheckWrite(size);
66     ASSERT_TRUE(ret);
67     streamBuffer.rPos_ = -10;
68     ret = circleStreamBuffer.CheckWrite(size);
69     ASSERT_TRUE(ret);
70     streamBuffer.wPos_ = 100;
71     ret = circleStreamBuffer.CheckWrite(size);
72     ASSERT_TRUE(ret);
73 }
74 
75 /**
76  * @tc.name:CircleStreamBufferTest_Write_001
77  * @tc.desc:Test the funcation Write
78  * @tc.type: FUNC
79  * @tc.require:
80  */
HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_Write_001, TestSize.Level1)81 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_Write_001, TestSize.Level1)
82 {
83     CircleStreamBuffer circleStreamBuffer;
84     StreamBuffer streamBuffer;
85     const char *buf = "1234#";
86     size_t size = 6;
87     streamBuffer.wPos_ = 30000;
88     streamBuffer.rPos_ = 8;
89     bool ret = circleStreamBuffer.Write(buf, size);
90     ASSERT_TRUE(ret);
91     streamBuffer.rPos_ = -11;
92     ret = circleStreamBuffer.Write(buf, size);
93     ASSERT_TRUE(ret);
94     streamBuffer.wPos_ = 200;
95     ret = circleStreamBuffer.Write(buf, size);
96     ASSERT_TRUE(ret);
97 }
98 
99 /**
100  * @tc.name:CircleStreamBufferTest_CreateKeyEvent_001
101  * @tc.desc:Test the funcation CreateKeyEvent
102  * @tc.type: FUNC
103  * @tc.require:
104  */
HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CreateKeyEvent_001, TestSize.Level1)105 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CreateKeyEvent_001, TestSize.Level1)
106 {
107     KeyEventNapi napi;
108     napi_env env = nullptr;
109     std::shared_ptr<KeyEvent> in = KeyEvent::Create();
110     ASSERT_NE(in, nullptr);
111     napi_value out = nullptr;
112     napi_status status = napi.CreateKeyEvent(env, in, out);
113     ASSERT_NE(status, napi_ok);
114 }
115 
116 /**
117  * @tc.name:CircleStreamBufferTest_GetKeyEvent_001
118  * @tc.desc:Test the funcation GetKeyEvent
119  * @tc.type: FUNC
120  * @tc.require:
121  */
HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_GetKeyEvent_001, TestSize.Level1)122 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_GetKeyEvent_001, TestSize.Level1)
123 {
124     KeyEventNapi napi;
125     napi_env env = nullptr;
126     napi_value in = nullptr;
127     std::shared_ptr<KeyEvent> out = KeyEvent::Create();
128     ASSERT_NE(out, nullptr);
129     napi_status status = napi.GetKeyEvent(env, in, out);
130     ASSERT_NE(status, napi_ok);
131 }
132 
133 /**
134  * @tc.name:CircleStreamBufferTest_CreateKeyItem_001
135  * @tc.desc:Test the funcation CreateKeyItem
136  * @tc.type: FUNC
137  * @tc.require:
138  */
HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CreateKeyItem_001, TestSize.Level1)139 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_CreateKeyItem_001, TestSize.Level1)
140 {
141     KeyEventNapi napi;
142     napi_env env = nullptr;
143     KeyEvent::KeyItem item;
144     std::optional<KeyEvent::KeyItem> in;
145     item.SetKeyCode(2018);
146     item.SetPressed(false);
147     in.emplace(item);
148     napi_value out = nullptr;
149     napi_status status = napi.CreateKeyItem(env, in, out);
150     ASSERT_NE(status, napi_ok);
151 }
152 
153 /**
154  * @tc.name:CircleStreamBufferTest_GetKeyItem_001
155  * @tc.desc:Test the funcation GetKeyItem
156  * @tc.type: FUNC
157  * @tc.require:
158  */
HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_GetKeyItem_001, TestSize.Level1)159 HWTEST_F(CircleStreamBufferTest, CircleStreamBufferTest_GetKeyItem_001, TestSize.Level1)
160 {
161     KeyEventNapi napi;
162     napi_env env = nullptr;
163     napi_value in = nullptr;
164     KeyEvent::KeyItem out;
165     out.SetKeyCode(2024);
166     out.SetPressed(false);
167     napi_status status = napi.GetKeyItem(env, in, out);
168     ASSERT_EQ(status, napi_ok);
169 }
170 } // namespace MMI
171 } // namespace OHOS
172