• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef KEYEVENT_TRACKER_H
17 #define KEYEVENT_TRACKER_H
18 #include <regex>
19 #include <cmath>
20 #include <chrono>
21 #include <iostream>
22 #include <queue>
23 #include <fstream>
24 #include <sstream>
25 #include <nlohmann/json.hpp>
26 #include "utils.h"
27 #include "key_event.h"
28 #include "key_option.h"
29 
30 namespace OHOS::uitest {
31     using namespace std;
32 
33     class KeyEventInfo {
34     public:
35         KeyEventInfo() = default;
36         ~KeyEventInfo() = default;
37 
SetActionTime(int64_t time)38         void SetActionTime(int64_t time)
39         {
40             time_ = time;
41         }
GetActionTime()42         int64_t GetActionTime() const
43         {
44             return time_;
45         }
SetKeyCode(int32_t keyCode)46         void SetKeyCode(int32_t keyCode)
47         {
48             keyCode_ = keyCode;
49         }
GetKeyCode()50         int32_t GetKeyCode() const
51         {
52             return keyCode_;
53         }
54         bool operator ==(const KeyEventInfo &info) const
55         {
56             return keyCode_ == info.GetKeyCode();
57         }
58     private:
59         int64_t time_;
60         int32_t keyCode_;
61     };
62     class KeyeventTracker {
63     public:
64         enum CaseTypes : uint8_t {
65             ACTION_START_TIME = 0, ACTION_DURATION_TIME, OP_TYPE, KEY_ITEMS_COUNT, KEY_CODE1, KEY_CODE2, KEY_CODE3
66         };
67         static const std::vector<int32_t> COMBINATION_KET;
68         static const std::string EVENT_TYPE;
69         static const std::string NONE_COMBINATION_ERROR;
70         static const int MAX_COMBINATION_SIZE = 3;
71         static const int INFO_SIZE = 7;
72     public:
73         KeyeventTracker() = default;
74         ~KeyeventTracker() = default;
75 
IsNeedRecord()76         bool IsNeedRecord() const
77         {
78             return isNeedRecord;
79         }
80 
SetNeedRecord(bool needRecord)81         void SetNeedRecord(bool needRecord)
82         {
83             isNeedRecord = needRecord;
84         }
85 
IsCombination()86         bool IsCombination() const
87         {
88             return isCombination;
89         }
90 
91         // 判断是否为组合按键中的特殊按键
IsCombinationKey(int32_t keyCode)92         static bool IsCombinationKey(int32_t keyCode)
93         {
94             auto it = std::find(COMBINATION_KET.begin(), COMBINATION_KET.end(), keyCode);
95             if (it != COMBINATION_KET.end()) {
96                 return true;
97             }
98             return false;
99         };
100 
GetInfos()101         std::vector<KeyEventInfo> GetInfos() const
102         {
103             return infos_;
104         }
105 
106         bool AddDownKeyEvent(KeyEventInfo &info);
107         bool AddCancelKeyEvent(KeyEventInfo &info);
108         void AddUpKeyEvent(KeyEventInfo &info);
109         KeyeventTracker GetSnapshootKey(KeyEventInfo &info);
110         // cout
111         std::string WriteCombinationData(shared_ptr<mutex> &cout_lock);
112         std::string WriteSingleData(KeyEventInfo &info, shared_ptr<mutex> &cout_lock);
113         // record.csv
114         nlohmann::json WriteCombinationData(ofstream &outFile, shared_ptr<mutex> &csv_lock);
115         nlohmann::json WriteSingleData(KeyEventInfo &info, ofstream &outFile, shared_ptr<mutex> &csv_lock);
116 
117         void PrintEventItems();
118 
119     private:
120         void KeyCodeDone(int32_t keyCode);
121         void KeyCodeCancel(int32_t keyCode);
122         void BuildEventItems();
123         void BuildEventItems(KeyEventInfo &info);
124     private:
125         std::vector<KeyEventInfo> infos_;
126         bool isNeedRecord = false;
127         bool isCombination = false;
128         int64_t actionStartTime = 0;
129         int64_t actionUpTime = 0;
130         // starttime/duration/eventtype/keycount/k1/k2/k3
131         std::string eventItems[INFO_SIZE] = {"-1"};
132     };
133 
134     struct SubscribeKeyevent {
135         int32_t keyCode;
136         bool isDown;
137         int32_t subId;
138     };
139 } // namespace OHOS::uitest
140 #endif // KEYEVENT_TRACKER_H