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 #include <chrono> 16 #include "keyevent_tracker.h" 17 18 namespace OHOS::uitest { 19 const std::vector<int32_t> KeyeventTracker::COMBINATION_KET = { 20 MMI::KeyEvent::KEYCODE_CTRL_LEFT, 21 MMI::KeyEvent::KEYCODE_CTRL_RIGHT, 22 MMI::KeyEvent::KEYCODE_ALT_LEFT, 23 MMI::KeyEvent::KEYCODE_ALT_RIGHT, 24 MMI::KeyEvent::KEYCODE_SHIFT_LEFT, 25 MMI::KeyEvent::KEYCODE_SHIFT_RIGHT, 26 MMI::KeyEvent::KEYCODE_POWER 27 }; 28 29 const std::string KeyeventTracker::EVENT_TYPE = "key" ; 30 const std::string KeyeventTracker::NONE_COMBINATION_ERROR = 31 "Failed to obtain the combination_key when cout keyEvent."; 32 AddDownKeyEvent(KeyEventInfo & info)33 bool KeyeventTracker::AddDownKeyEvent(KeyEventInfo &info) 34 { 35 // 该按键是否已down 36 if (std::find(infos_.begin(), infos_.end(), info) != infos_.end()) { 37 return false; 38 } 39 infos_.push_back(info); 40 return true; 41 } 42 AddUpKeyEvent(KeyEventInfo & info)43 void KeyeventTracker::AddUpKeyEvent(KeyEventInfo &info) 44 { 45 KeyCodeDone(info.GetKeyCode()); 46 if (infos_.size() == 0) { 47 isCombination = false; 48 } 49 return ; 50 } 51 GetSnapshootKey(KeyEventInfo & info)52 KeyeventTracker KeyeventTracker::GetSnapshootKey(KeyEventInfo &info) 53 { 54 KeyeventTracker snapshootKeyTracker; 55 snapshootKeyTracker.infos_ = infos_; 56 snapshootKeyTracker.actionUpTime = info.GetActionTime(); 57 snapshootKeyTracker.isNeedRecord = true; 58 return snapshootKeyTracker; 59 } 60 KeyCodeDone(int32_t keyCode)61 void KeyeventTracker::KeyCodeDone(int32_t keyCode) 62 { 63 auto infoIt = std::find_if(infos_.begin(), infos_.end(), [keyCode](const KeyEventInfo& info) { 64 return info.GetKeyCode() == keyCode; 65 }); 66 if (infoIt != infos_.end()) { 67 infos_.erase(infoIt); 68 return; 69 } 70 LOG_E("keyCode:%{keyCode} did not received down event before the up event.", keyCode); 71 } 72 73 // cout WriteCombinationData(shared_ptr<mutex> & cout_lock)74 std::string KeyeventTracker::WriteCombinationData(shared_ptr<mutex> &cout_lock) 75 { 76 std::stringstream sout; 77 if (infos_.size()==0) { 78 LOG_E("Failed to obtain the combination_key when cout keyEvent."); 79 return NONE_COMBINATION_ERROR; 80 } 81 BuildEventItems(); 82 std::lock_guard<mutex> guard(*cout_lock); 83 for (size_t i = 0; i < INFO_SIZE - 1; i++) { 84 sout << eventItems[i] << ", "; 85 } 86 sout << eventItems[INFO_SIZE-1]; 87 std::cout << sout.str() << std::endl; 88 return sout.str(); 89 } WriteSingleData(KeyEventInfo & info,shared_ptr<mutex> & cout_lock)90 std::string KeyeventTracker::WriteSingleData(KeyEventInfo &info, shared_ptr<mutex> &cout_lock) 91 { 92 std::stringstream sout; 93 BuildEventItems(info); 94 std::lock_guard<mutex> guard(*cout_lock); 95 for (size_t i = 0; i < INFO_SIZE - 1; i++) { 96 sout << eventItems[i] << ", "; 97 } 98 sout << eventItems[INFO_SIZE-1]; 99 std::cout << sout.str() << std::endl; 100 return sout.str(); 101 } 102 103 // record.csv WriteCombinationData(ofstream & outFile,shared_ptr<mutex> & csv_lock,std::string savePath)104 nlohmann::json KeyeventTracker::WriteCombinationData(ofstream& outFile, shared_ptr<mutex> &csv_lock, std::string savePath) 105 { 106 auto data = nlohmann::json(); 107 if (infos_.size()==0) { 108 LOG_E("Failed to obtain the combination_key when save keyEvent into record.csv."); 109 data["ERROR_INFO"] = "Failed to obtain the combination_key when save keyEvent into record.csv."; 110 return data; 111 } 112 BuildEventItems(); 113 data["ActionStartTime"] = eventItems[CaseTypes::ACTION_START_TIME]; 114 data["ActionDurationTime"] = eventItems[CaseTypes::ACTION_DURATION_TIME]; 115 data["OP_TYPE"] = eventItems[CaseTypes::OP_TYPE]; 116 data["keyItemsCount"] = eventItems[CaseTypes::KEY_ITEMS_COUNT]; 117 data["KeyCode1"] = eventItems[CaseTypes::KEY_CODE1]; 118 data["KeyCode2"] = eventItems[CaseTypes::KEY_CODE2]; 119 data["KeyCode3"] = eventItems[CaseTypes::KEY_CODE3]; 120 if (savePath != "") { 121 data["FILEPATH"] = savePath; 122 } 123 std::lock_guard<mutex> guard(*csv_lock); 124 if (outFile.is_open()) { 125 outFile << data.dump() << std::endl; 126 } 127 return data; 128 } 129 WriteSingleData(KeyEventInfo & info,ofstream & outFile,shared_ptr<mutex> & csv_lock,std::string savePath)130 nlohmann::json KeyeventTracker::WriteSingleData(KeyEventInfo &info, ofstream &outFile, shared_ptr<mutex> &csv_lock, std::string savePath) 131 { 132 BuildEventItems(info); 133 auto data = nlohmann::json(); 134 data["ActionStartTime"] = eventItems[CaseTypes::ACTION_START_TIME]; 135 data["ActionDurationTime"] = eventItems[CaseTypes::ACTION_DURATION_TIME]; 136 data["OP_TYPE"] = eventItems[CaseTypes::OP_TYPE]; 137 data["keyItemsCount"] = eventItems[CaseTypes::KEY_ITEMS_COUNT]; 138 data["KeyCode1"] = eventItems[CaseTypes::KEY_CODE1]; 139 data["KeyCode2"] = eventItems[CaseTypes::KEY_CODE2]; 140 data["KeyCode3"] = eventItems[CaseTypes::KEY_CODE3]; 141 if (savePath != "") { 142 data["FILEPAHT"] = savePath; 143 } 144 std::lock_guard<mutex> guard(*csv_lock); 145 if (outFile.is_open()) { 146 outFile << data.dump() << std::endl; 147 } 148 return data; 149 } 150 BuildEventItems(KeyEventInfo & info)151 void KeyeventTracker::BuildEventItems(KeyEventInfo &info) 152 { 153 if (eventItems[0] != "-1") { 154 return ; 155 } 156 actionStartTime = infos_.size()==0?info.GetActionTime():infos_[0].GetActionTime(); 157 eventItems[CaseTypes::ACTION_START_TIME] = std::to_string(actionStartTime); 158 eventItems[CaseTypes::ACTION_DURATION_TIME] = std::to_string(actionUpTime - actionStartTime); 159 eventItems[CaseTypes::OP_TYPE] = EVENT_TYPE; 160 eventItems[CaseTypes::KEY_ITEMS_COUNT] = std::to_string(infos_.size()+1); 161 for (size_t i = 0; i < infos_.size() && i < MAX_COMBINATION_SIZE; i++) { 162 eventItems[CaseTypes::KEY_CODE1+i] = std::to_string(infos_[i].GetKeyCode()); 163 } 164 eventItems[CaseTypes::KEY_CODE1+infos_.size()] = std::to_string(info.GetKeyCode()); 165 } 166 BuildEventItems()167 void KeyeventTracker::BuildEventItems() 168 { 169 if (eventItems[0] != "-1" || infos_.size() == 0) { 170 return ; 171 } 172 actionStartTime = infos_[0].GetActionTime(); 173 eventItems[CaseTypes::ACTION_START_TIME] = std::to_string(actionStartTime); 174 eventItems[CaseTypes::ACTION_DURATION_TIME] = std::to_string(actionUpTime - actionStartTime); 175 eventItems[CaseTypes::OP_TYPE] = EVENT_TYPE; 176 eventItems[CaseTypes::KEY_ITEMS_COUNT] = std::to_string(infos_.size()); 177 for (size_t i = 0; i < infos_.size() && i < MAX_COMBINATION_SIZE; i++) { 178 eventItems[CaseTypes::KEY_CODE1+i] = std::to_string(infos_[i].GetKeyCode()); 179 } 180 } 181 PrintEventItems()182 void KeyeventTracker::PrintEventItems() 183 { 184 std::cout << "infos:" ; 185 for (size_t i = 0; i < infos_.size() ; i++) { 186 std::cout << std::to_string(infos_[i].GetKeyCode()) << ","; 187 } 188 std::cout << std::endl; 189 } 190 } // namespace OHOS::uitest