1 /* 2 * Copyright (c) 2022 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 UI_RECORD_H 17 #define UI_RECORD_H 18 #include <fstream> 19 #include <regex> 20 #include <iostream> 21 #include <unistd.h> 22 #include <dirent.h> 23 #include <sys/stat.h> 24 #include <typeinfo> 25 #include <string> 26 #include <vector> 27 #include <cmath> 28 #include <map> 29 #include <thread> 30 #include <mutex> 31 #include <condition_variable> 32 #include "least_square_impl.h" 33 #include "touch_event.h" 34 #include "offset.h" 35 #include "velocity.h" 36 #include "velocity_tracker.h" 37 #include "keyevent_tracker.h" 38 #include "ui_driver.h" 39 #include "ui_action.h" 40 #include "input_manager.h" 41 #include "i_input_event_consumer.h" 42 #include "pointer_event.h" 43 #include "widget_operator.h" 44 #include "window_operator.h" 45 #include "widget_selector.h" 46 #include "ui_model.h" 47 #include "find_widget.h" 48 #include "pointer_tracker.h" 49 #include "pointer_info.h" 50 #include "input_manager.h" 51 52 namespace OHOS::uitest { 53 struct RecordOption { 54 bool saveLayout = false; 55 bool terminalCout = true; 56 bool saveWidget = true; 57 }; 58 class InputEventCallback : public MMI::IInputEventConsumer { 59 public: 60 void RecordInitEnv(const RecordOption &option); 61 bool InitReportFolder(); 62 bool InitEventRecordFile(); 63 bool dealSpecialKey(std::shared_ptr<MMI::KeyEvent> keyEvent) const; 64 void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override; 65 void HandleDownEvent(TouchEventInfo& event) const; 66 void HandleMoveEvent(const TouchEventInfo& event) const; 67 void HandleUpEvent(const TouchEventInfo& event) const; 68 void WriteDataAndFindWidgets(const TouchEventInfo& event) const; 69 void WritePointerInfo() const; 70 void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override; 71 void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override; 72 void SubscribeMonitorInit(); 73 void KeyEventSubscribeTemplate(SubscribeKeyevent& subscribeKeyevent); 74 void SubscribeMonitorCancel(); 75 void TimerReprintClickFunction(); 76 void TimerTouchCheckFunction(); 77 void FindWidgetsandWriteData(); 78 void DoAbcCallBack(nlohmann::json jsonData) const; SetAbcCallBack(std::function<void (nlohmann::json)> handler)79 void SetAbcCallBack(std::function<void(nlohmann::json)> handler) 80 { 81 abcCallBack = handler; 82 } GetAbcCallBack()83 auto GetAbcCallBack() 84 { 85 return abcCallBack; 86 } 87 void WriteLayout(nlohmann::json layout) const; 88 void OnInputEventUp(std::shared_ptr<MMI::KeyEvent> keyEvent, KeyEventInfo &info) const; 89 void OnInputEventDown(std::shared_ptr<MMI::KeyEvent> keyEvent, KeyEventInfo &info) const; 90 static constexpr int TIMEINTERVAL = 5000; 91 static constexpr int KEY_DOWN_DURATION = 0; 92 static const std::string DEFAULT_DIR; 93 mutable volatile int touchTime = 0; 94 mutable volatile bool isLastClick_ = false; 95 mutable shared_ptr<mutex> cout_lock = make_shared<std::mutex>(); 96 mutable shared_ptr<mutex> csv_lock = make_shared<std::mutex>(); 97 mutable std::mutex g_clickMut; 98 mutable std::condition_variable clickCon; 99 mutable volatile bool findWidgetsAllow_ = false; 100 mutable std::mutex widgetsMut; 101 mutable std::condition_variable widgetsCon; 102 bool stopFlag = false; 103 std::mutex timerMut; 104 std::condition_variable timerCon; 105 private: 106 shared_ptr<queue<std::string>> eventQueue_; 107 RecordOption recordMode; 108 std::string filePath; 109 WidgetSelector selector = WidgetSelector(); 110 vector<std::unique_ptr<Widget>> rev; 111 std::function<void(nlohmann::json)> abcCallBack = nullptr; 112 mutable std::ofstream outFile; 113 mutable UiDriver driver = UiDriver(); 114 mutable PointerTracker pointerTracker_; 115 mutable KeyeventTracker keyeventTracker_; 116 }; 117 118 class TestUtils { 119 public: 120 TestUtils() = default; 121 virtual ~TestUtils() = default; split(const std::string & in,const std::string & delim)122 static std::vector<std::string> split(const std::string &in, const std::string &delim) 123 { 124 std::regex reg(delim); 125 std::vector<std::string> res = { 126 std::sregex_token_iterator(in.begin(), in.end(), reg, -1), std::sregex_token_iterator() 127 }; 128 return res; 129 }; 130 }; 131 132 class EventData { 133 public: 134 void WriteEventData(const VelocityTracker &velocityTracker, const std::string &actionType) const; 135 static void ReadEventLine(); 136 private: 137 static std::string defaultDir; 138 }; 139 140 class DataWrapper { 141 public: 142 template<typename Function> ProcessData(Function userFunc)143 void ProcessData(Function userFunc) 144 { 145 std::lock_guard<std::mutex> lock(mut); 146 userFunc(data); 147 } 148 private: 149 EventData data; 150 UiDriver d; 151 std::mutex mut; 152 }; 153 int32_t UiDriverRecordStart(RecordOption modeOpt); 154 int32_t UiDriverRecordStart(std::function<void(nlohmann::json)> handler, std::string modeOpt); 155 int32_t UiDriverRecordStartTemplate(RecordOption modeOpt); 156 void UiDriverRecordStop(); 157 } // namespace OHOS::uitest 158 #endif // UI_RECORD_H