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 class InputEventCallback : public MMI::IInputEventConsumer { 54 public: 55 void RecordInitEnv(const std::string &modeOpt); 56 bool InitReportFolder(); 57 bool InitEventRecordFile(); 58 bool dealSpecialKey(std::shared_ptr<MMI::KeyEvent> keyEvent) const; 59 void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override; 60 void HandleDownEvent(TouchEventInfo& event) const; 61 void HandleMoveEvent(const TouchEventInfo& event) const; 62 void HandleUpEvent(const TouchEventInfo& event) const; 63 void WriteDataAndFindWidgets(const TouchEventInfo& event) const; 64 void WritePointerInfo() const; 65 void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override; 66 void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override; 67 void SubscribeMonitorInit(); 68 void KeyEventSubscribeTemplate(SubscribeKeyevent& subscribeKeyevent); 69 void SubscribeMonitorCancel(); 70 void TimerReprintClickFunction(); 71 void TimerTouchCheckFunction(); 72 void FindWidgetsandWriteData(); 73 void DoAbcCallBack(nlohmann::json jsonData) const; SetAbcCallBack(std::function<void (nlohmann::json)> handler)74 void SetAbcCallBack(std::function<void(nlohmann::json)> handler) 75 { 76 abcCallBack = handler; 77 } GetAbcCallBack()78 auto GetAbcCallBack() 79 { 80 return abcCallBack; 81 } 82 83 static constexpr int TIMEINTERVAL = 5000; 84 static constexpr int KEY_DOWN_DURATION = 0; 85 static const std::string DEFAULT_DIR; 86 mutable volatile int touchTime = 0; 87 mutable volatile bool isLastClick_ = false; 88 mutable shared_ptr<mutex> cout_lock = make_shared<std::mutex>(); 89 mutable shared_ptr<mutex> csv_lock = make_shared<std::mutex>(); 90 mutable std::mutex g_clickMut; 91 mutable std::condition_variable clickCon; 92 mutable volatile bool findWidgetsAllow_ = false; 93 mutable std::mutex widgetsMut; 94 mutable std::condition_variable widgetsCon; 95 bool stopFlag = false; 96 std::mutex timerMut; 97 std::condition_variable timerCon; 98 private: 99 shared_ptr<queue<std::string>> eventQueue_; 100 std::string recordMode = ""; 101 std::string filePath; 102 WidgetSelector selector = WidgetSelector(); 103 vector<std::unique_ptr<Widget>> rev; 104 std::function<void(nlohmann::json)> abcCallBack = nullptr; 105 mutable std::ofstream outFile; 106 mutable UiDriver driver = UiDriver(); 107 mutable PointerTracker pointerTracker_; 108 mutable KeyeventTracker keyeventTracker_; 109 }; 110 111 class TestUtils { 112 public: 113 TestUtils() = default; 114 virtual ~TestUtils() = default; split(const std::string & in,const std::string & delim)115 static std::vector<std::string> split(const std::string &in, const std::string &delim) 116 { 117 std::regex reg(delim); 118 std::vector<std::string> res = { 119 std::sregex_token_iterator(in.begin(), in.end(), reg, -1), std::sregex_token_iterator() 120 }; 121 return res; 122 }; 123 }; 124 125 class EventData { 126 public: 127 void WriteEventData(const VelocityTracker &velocityTracker, const std::string &actionType) const; 128 static void ReadEventLine(); 129 private: 130 static std::string defaultDir; 131 }; 132 133 class DataWrapper { 134 public: 135 template<typename Function> ProcessData(Function userFunc)136 void ProcessData(Function userFunc) 137 { 138 std::lock_guard<std::mutex> lock(mut); 139 userFunc(data); 140 } 141 private: 142 EventData data; 143 UiDriver d; 144 std::mutex mut; 145 }; 146 int32_t UiDriverRecordStart(std::string modeOpt); 147 int32_t UiDriverRecordStart(std::function<void(nlohmann::json)> handler, std::string modeOpt); 148 int32_t UiDriverRecordStartTemplate(std::string modeOpt); 149 void UiDriverRecordStop(); 150 } // namespace OHOS::uitest 151 #endif // UI_RECORD_H