• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "least_square_impl.h"
32 #include "touch_event.h"
33 #include "offset.h"
34 #include "velocity.h"
35 #include "velocity_tracker.h"
36 #include "ui_driver.h"
37 #include "ui_action.h"
38 #include "input_manager.h"
39 #include "i_input_event_consumer.h"
40 #include "pointer_event.h"
41 #include "widget_operator.h"
42 #include "window_operator.h"
43 #include "widget_selector.h"
44 #include "ui_model.h"
45 #include "find_widget.h"
46 
47 namespace OHOS::uitest {
48     static int g_touchTime;
49     static int TIMEINTERVAL = 5000;
50     static std::string g_recordMode = "";
51     class InputEventCallback : public MMI::IInputEventConsumer {
52     public:
53         void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override;
54         void HandleDownEvent(TouchEventInfo& event) const;
55         void HandleMoveEvent(const TouchEventInfo& event) const;
56         void HandleUpEvent(const TouchEventInfo& event) const;
57         void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent)58         void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override {}
59         static std::shared_ptr<InputEventCallback> GetPtr();
60     };
61 
62     class TestUtils {
63     public:
64         TestUtils() = default;
65         virtual ~TestUtils() = default;
split(const std::string & in,const std::string & delim)66         static std::vector<std::string> split(const std::string &in, const std::string &delim)
67         {
68             std::regex reg(delim);
69             std::vector<std::string> res = {
70                 std::sregex_token_iterator(in.begin(), in.end(), reg, -1), std::sregex_token_iterator()
71             };
72             return res;
73         };
74     };
75 
76     bool InitEventRecordFile();
77 
78     void RecordInitEnv(const std::string &modeOpt);
79 
80     class EventData {
81     public:
82         void WriteEventData(const VelocityTracker &velocityTracker, const std::string &actionType);
83         static void ReadEventLine();
84     private:
85         VelocityTracker v;
86         std::string action;
87     };
88 
89     class DataWrapper {
90     public:
91         template<typename Function>
ProcessData(Function userFunc)92         void ProcessData(Function userFunc)
93         {
94             std::lock_guard<std::mutex> lock(mut);
95             userFunc(data);
96         }
97     private:
98         EventData data;
99         UiDriver d;
100         std::mutex mut;
101     };
102 
103     class Timer {
104     public:
Timer()105         Timer(): expired(true), tryToExpire(false)
106         {}
~Timer()107         ~Timer()
108         {
109             Stop();
110         }
TimerFunc()111         static void TimerFunc()
112         {
113             int currentTime = GetCurrentMillisecond();
114             int diff = currentTime - g_touchTime;
115             if (diff >= TIMEINTERVAL) {
116                 std::cout << "No operation detected for 5 seconds, press ctrl + c to save this file?" << std::endl;
117             }
118         }
Start(int interval,std::function<void ()> task)119         void Start(int interval, std::function<void()> task)
120         {
121             if (expired == false) {
122                 return;
123             }
124             expired = false;
125             std::thread([this, interval, task]() {
126                 while (!tryToExpire) {
127                     std::this_thread::sleep_for(std::chrono::milliseconds(TIMEINTERVAL));
128                     task();
129                 }
130 
131                 {
132                     std::unique_lock<std::mutex> lk(index, std::try_to_lock);
133                     expired = true;
134                     expiredCond.notify_one();
135                 }
136             }).detach();
137         }
Stop()138         void Stop()
139         {
140             if (expired) {
141                 return;
142             }
143 
144             if (tryToExpire) {
145                 return;
146             }
147 
148             tryToExpire = true; // change this bool value to make timer while loop stop
149             {
150                 std::unique_lock<std::mutex> lk(index, std::try_to_lock);
151                 expiredCond.wait(lk, [this] {return expired == true; });
152 
153                 // Resets the timer
154                 if (expired == true) {
155                     tryToExpire = false;
156                 }
157             }
158         }
159 
160     private:
161         std::atomic<bool> expired; // timer stopped status
162         std::atomic<bool> tryToExpire; // timer is in stop process
163         std::mutex index;
164         std::condition_variable expiredCond;
165     };
166 } // namespace OHOS::uitest
167 #endif // UI_RECORD_H