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)104 nlohmann::json KeyeventTracker::WriteCombinationData(ofstream& outFile, shared_ptr<mutex> &csv_lock) 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 std::lock_guard<mutex> guard(*csv_lock); 121 if (outFile.is_open()) { 122 outFile << data.dump() << std::endl; 123 } 124 return data; 125 } 126 WriteSingleData(KeyEventInfo & info,ofstream & outFile,shared_ptr<mutex> & csv_lock)127 nlohmann::json KeyeventTracker::WriteSingleData(KeyEventInfo &info, ofstream &outFile, shared_ptr<mutex> &csv_lock) 128 { 129 BuildEventItems(info); 130 auto data = nlohmann::json(); 131 data["ActionStartTime"] = eventItems[CaseTypes::ACTION_START_TIME]; 132 data["ActionDurationTime"] = eventItems[CaseTypes::ACTION_DURATION_TIME]; 133 data["OP_TYPE"] = eventItems[CaseTypes::OP_TYPE]; 134 data["keyItemsCount"] = eventItems[CaseTypes::KEY_ITEMS_COUNT]; 135 data["KeyCode1"] = eventItems[CaseTypes::KEY_CODE1]; 136 data["KeyCode2"] = eventItems[CaseTypes::KEY_CODE2]; 137 data["KeyCode3"] = eventItems[CaseTypes::KEY_CODE3]; 138 std::lock_guard<mutex> guard(*csv_lock); 139 if (outFile.is_open()) { 140 outFile << data.dump() << std::endl; 141 } 142 return data; 143 } 144 BuildEventItems(KeyEventInfo & info)145 void KeyeventTracker::BuildEventItems(KeyEventInfo &info) 146 { 147 if (eventItems[0] != "-1") { 148 return ; 149 } 150 actionStartTime = infos_.size()==0?info.GetActionTime():infos_[0].GetActionTime(); 151 eventItems[CaseTypes::ACTION_START_TIME] = std::to_string(actionStartTime); 152 eventItems[CaseTypes::ACTION_DURATION_TIME] = std::to_string(actionUpTime - actionStartTime); 153 eventItems[CaseTypes::OP_TYPE] = EVENT_TYPE; 154 eventItems[CaseTypes::KEY_ITEMS_COUNT] = std::to_string(infos_.size()+1); 155 for (size_t i = 0; i < infos_.size() && i < MAX_COMBINATION_SIZE; i++) { 156 eventItems[CaseTypes::KEY_CODE1+i] = std::to_string(infos_[i].GetKeyCode()); 157 } 158 eventItems[CaseTypes::KEY_CODE1+infos_.size()] = std::to_string(info.GetKeyCode()); 159 } 160 BuildEventItems()161 void KeyeventTracker::BuildEventItems() 162 { 163 if (eventItems[0] != "-1" || infos_.size() == 0) { 164 return ; 165 } 166 actionStartTime = infos_[0].GetActionTime(); 167 eventItems[CaseTypes::ACTION_START_TIME] = std::to_string(actionStartTime); 168 eventItems[CaseTypes::ACTION_DURATION_TIME] = std::to_string(actionUpTime - actionStartTime); 169 eventItems[CaseTypes::OP_TYPE] = EVENT_TYPE; 170 eventItems[CaseTypes::KEY_ITEMS_COUNT] = std::to_string(infos_.size()); 171 for (size_t i = 0; i < infos_.size() && i < MAX_COMBINATION_SIZE; i++) { 172 eventItems[CaseTypes::KEY_CODE1+i] = std::to_string(infos_[i].GetKeyCode()); 173 } 174 } 175 PrintEventItems()176 void KeyeventTracker::PrintEventItems() 177 { 178 std::cout << "infos:" ; 179 for (size_t i = 0; i < infos_.size() ; i++) { 180 std::cout << std::to_string(infos_[i].GetKeyCode()) << ","; 181 } 182 std::cout << std::endl; 183 } 184 } // namespace OHOS::uitest