• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <fstream>
17 #include <filesystem>
18 #include <gtest/gtest.h>
19 #include <iostream>
20 
21 #include "mmi_log.h"
22 #include "key_map_manager.h"
23 #include "key_map_manager_mock.h"
24 #include "config_policy_utils.h"
25 
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "KeyMapManagerTest"
28 #undef MAX_PATH_LEN
29 #define MAX_PATH_LEN              256
30 
31 namespace OHOS {
32 namespace MMI {
33 
34 namespace {
35 using namespace testing::ext;
36 using namespace testing;
37 } // namespace
38 
39 class KeyMapManagerTest : public testing::Test {
40 public:
SetUpTestCase(void)41     static void SetUpTestCase(void) {}
TearDownTestCase(void)42     static void TearDownTestCase(void) {}
43 };
44 
45 /**
46  * @tc.name: KeyMapManagerTest_GetProFilePath_001
47  * @tc.desc: Test the funcation GetProFilePath
48  * @tc.type: FUNC
49  * @tc.require:
50  */
51 HWTEST_F(KeyMapManagerTest, KeyMapManagerTest_GetProFilePath_001, TestSize.Level1)
52 {
53     CALL_DEBUG_ENTER;
54     KeyMgrMock mocker;
55     static char testFile[] = "example.pro";
56     EXPECT_CALL(mocker, GetProFileAbsPath)
57         .WillRepeatedly(Return(testFile));
58 
59     std::ofstream ofs("example.pro");
60     EXPECT_TRUE(ofs.is_open());
61     ofs << "KEY_BTN_1 123 1111\n" << std::endl;
62     KeyMapManager KeyMapManager;
63     std::string testPathSuffix = "example";
64     std::string result = KeyMapManager.GetProFilePath(testPathSuffix);
65     std::string expectReutrn = "example.pro";
66     EXPECT_STREQ(result.c_str(), expectReutrn.c_str());
67 
68     if (std::remove(testFile) == 0) {
69         std::cout << "test file removed success" << std::endl;
70     } else {
71         std::cerr << "test file removed fail" << std::endl;
72     }
73 }
74 
75 /**
76  * @tc.name: KeyMapManagerTest_GetProFilePath_002
77  * @tc.desc: Test the funcation GetProFilePath
78  * @tc.type: FUNC
79  * @tc.require:
80  */
81 HWTEST_F(KeyMapManagerTest, KeyMapManagerTest_GetProFilePath_002, TestSize.Level1)
82 {
83     CALL_DEBUG_ENTER;
84     KeyMgrMock mocker;
85     static char testFile[] = "example.pro";
86     char testFile1[MAX_PATH_LEN] = { };
87     char * testFile2 = nullptr;
88     char testFile3[MAX_PATH_LEN + 2] = { };
89     for (int i = 0; i < MAX_PATH_LEN + 1; ++i) {
90         testFile3[i] = 'A';
91     }
92     EXPECT_CALL(mocker, GetProFileAbsPath)
93         .WillOnce(Return(testFile1))
94         .WillOnce(Return(testFile2))
95         .WillOnce(Return(testFile3));;
96 
97     std::ofstream ofs("example.pro");
98     EXPECT_TRUE(ofs.is_open());
99     ofs << "KEY_BRL_DOT10 506 3210 HOS_KEY_BRL_DOT10\n" << std::endl;
100     KeyMapManager KeyMapManager;
101     std::string testPathSuffix = "example";
102     std::string result = KeyMapManager.GetProFilePath(testPathSuffix);
103     std::string expectReutrn = "/vendor/etc/keymap/example.pro";
104     EXPECT_STREQ(result.c_str(), expectReutrn.c_str());
105 
106     result = KeyMapManager.GetProFilePath(testPathSuffix);
107     EXPECT_STREQ(result.c_str(), expectReutrn.c_str());
108 
109     result = KeyMapManager.GetProFilePath(testPathSuffix);
110     EXPECT_STREQ(result.c_str(), expectReutrn.c_str());
111 
112     if (std::remove(testFile) == 0) {
113         std::cout << "test file removed success" << std::endl;
114     } else {
115         std::cerr << "test file removed fail" << std::endl;
116     }
117 }
118 
119 } // namespace MMI
120 } // namespace OHOS
121