libaurum: refactor a flick command and implementation
[platform/core/uifw/aurum.git] / protocol / examples / python / testFeatures.py
1 from __future__ import print_function
2 from aurum_pb2 import *
3 from aurum_pb2_grpc import BootstrapStub
4 import logging
5 import grpc
6 import time
7
8 def findElementTest(stub):
9     response = stub.findElement(ReqFindElement(isClickable=True))
10     for el in response.elements:
11         return True
12     return False
13
14 def getValueTest(stub):
15     response = stub.findElement(ReqFindElement(textField='Widgets'))
16     print("els", response)
17     for el in response.elements:
18         response = stub.getValue(ReqGetValue(elementId=el.elementId))
19         return response.stringValue == 'Widgets'
20     return False
21
22 def setValueClearTest(stub):
23     response = stub.findElement(ReqFindElement(textField='Widgets'))
24     if len(response.elements) <= 0: return False
25     stub.click(ReqClick(type='ELEMENTID', elementId=response.elements[0].elementId))
26
27     def inScreen(size):
28         if size.x < 0: return False
29         if size.y < 0: return False
30         if size.x >= 360: return False
31         if size.y >= 360: return False
32         return True
33
34     for tryCnt in range(10):
35         stub.flick(ReqFlick(startPoint=Point(x=160, y=350), endPoint=Point(x=160, y=10), durationMs=500))
36         response = stub.findElement(ReqFindElement(textField='Entry/Editfield.*'))
37         if len(response.elements) <= 0: continue
38         targetObj = response.elements[0].elementId
39         response = stub.getSize(ReqGetSize(elementId=targetObj))
40         if inScreen(response.size):
41             stub.click(ReqClick(type='ELEMENTID', elementId=targetObj))
42             break
43
44     for tryCnt in range(10):
45         stub.flick(ReqFlick(startPoint=Point(x=160, y=350), endPoint=Point(x=160, y=10), durationMs=500))
46         response = stub.findElement(ReqFindElement(textField='Editable'))
47         if len(response.elements) <= 0: continue
48         targetObj = response.elements[0].elementId
49         response = stub.getSize(ReqGetSize(elementId=targetObj))
50         isShowing = stub.getAttribute(ReqGetAttribute(elementId=targetObj, attribute='SHOWING')).boolValue
51         if inScreen(response.size) or isShowing:
52             stub.click(ReqClick(type='ELEMENTID', elementId=targetObj))
53             break
54
55     response = stub.findElement(ReqFindElement(textField='editable'))
56     if len(response.elements) <= 0: return False
57     targetObj = response.elements[0].elementId
58
59     testString = 'set test string by calling SetValue Method'
60     stub.setValue(ReqSetValue(elementId=targetObj, stringValue=testString))
61     response = stub.getValue(ReqGetValue(elementId=targetObj))
62     if response.stringValue != testString:
63         return False
64
65     stub.clear(ReqClear(elementId=targetObj))
66
67     response = stub.getValue(ReqGetValue(elementId=targetObj))
68     if response.stringValue != 'editable':
69         return False
70
71     return True
72
73 def getSizeTest(stub):
74     response = stub.findElement(ReqFindElement(textField='Widgets'))
75     print("els", response)
76     for el in response.elements:
77         response = stub.getSize(ReqGetSize(elementId=el.elementId))
78         print(response)
79         return response.size.width + response.size.height > 0
80     return False
81
82 def getAttributeTest(stub):
83     response = stub.findElement(ReqFindElement(textField='Widgets'))
84     if len(response.elements) <= 0: return False
85
86     checkList = [
87             ['VISIBLE', True],
88             ['FOCUSABLE', True],
89             ['FOCUSED', False],
90             ['ENABLED', True],
91             ['CLICKABLE', True],
92             ['SCROLLABLE', False],
93             ['CHECKABLE', False],
94             ['CHECKED', False],
95             ['SELECTED', False],
96             ['SELECTABLE',True],
97             ['SHOWING', True],
98             ['ACTIVE', True],
99     ]
100     isFailed = False
101     for el in response.elements:
102         for attr in checkList:
103             if stub.getAttribute(ReqGetAttribute(elementId=el.elementId, attribute=attr[0])).boolValue != attr[1]:
104                 isFailed = True
105
106     if isFailed == True: return False
107
108     response = stub.findElement(ReqFindElement(textField='Internal Legacy'))
109     if len(response.elements) <= 0: return False
110
111     checkList = [
112             ['VISIBLE',     True],
113             ['FOCUSABLE',   True],
114             ['FOCUSED',     False],
115             ['ENABLED',     True],
116             ['CLICKABLE',   True],
117             ['SCROLLABLE',  False],
118             ['CHECKABLE',   False],
119             ['CHECKED',     False],
120             ['SELECTED',    False],
121             ['SELECTABLE',  True],
122             ['SHOWING',     False],
123             ['ACTIVE',      False],
124     ]
125     isFailed = False
126     for el in response.elements:
127         for attr in checkList:
128             if stub.getAttribute(ReqGetAttribute(elementId=el.elementId, attribute=attr[0])).boolValue != attr[1]:
129                 isFailed = True
130
131     return isFailed == False
132
133 def clickTest(stub):
134     response = stub.findElement(ReqFindElement(textField='Widgets'))
135     if len(response.elements) <= 0: return False
136
137     for el in response.elements:
138         stub.click(ReqClick(elementId=el.elementId, type='ELEMENTID'))
139
140     response = stub.findElement(ReqFindElement(textField='Box'))
141     if len(response.elements) <= 0: return False
142
143     for el in response.elements:
144         stub.click(ReqClick(coordination=Point(x=160, y=160), type='COORD'))
145
146     response = stub.findElement(ReqFindElement(textField='Vertical Box'))
147     if len(response.elements) <= 0: return False
148
149     return True
150
151 def longClickTest(stub):
152     response = stub.sendKey(ReqKey(type='HOME', actionType='STROKE'))
153     stub.click(ReqClick(coordination=Point(x=160, y=160), type='COORD'))
154     # TODO : find out something changed
155     return False
156
157 def flickTest(stub):
158     response = stub.findElement(ReqFindElement(textField='Widgets'))
159     if len(response.elements) <= 0:
160         return False
161
162     for el in response.elements:
163         print(el)
164         stub.click(ReqClick(elementId=el.elementId, type='ELEMENTID'))
165         break
166
167     response = stub.findElement(ReqFindElement(textField='Box', isShowing=True))
168     if len(response.elements) <= 0: return False
169     targetObj = response.elements[0].elementId
170
171     for tryCnt in range(10):
172         print('Flick to bottom to find "Spinner" item @ tries:', tryCnt)
173         stub.flick(ReqFlick(startPoint=Point(x=160, y=359), endPoint=Point(x=160, y=1), durationMs=350))
174         response = stub.findElement(ReqFindElement(textField='Spinner.*'))
175         time.sleep(0.01)
176         print(response)
177         if len(response.elements) > 0: return True
178
179     return False
180
181 def touchTest(stub):
182     stub.touchDown(ReqTouchDown(coordination=Point(x=160,y=330)))
183     for yy in range(330, 30, -10):
184         stub.touchMove(ReqTouchMove(coordination=Point(x=160,y=yy)))
185     stub.touchUp(ReqTouchUp(coordination=Point(x=160,y=30)))
186
187     return True
188
189
190 def get_file_chunks(filename):
191    CHUNK_SIZE = 1024 * 1024
192    with open(filename, 'rb') as f:
193        while True:
194            piece = f.read(CHUNK_SIZE)
195            if len(piece) == 0:
196                return
197            yield ReqInstallApp(package=piece)
198
199
200 def installAppTest(stub):
201     response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
202     if (response.isInstalled): return True
203
204     tpkFile = './org.tizen.uicomponents.arm.tpk'
205     binaryChunk = get_file_chunks(tpkFile)
206     response = stub.installApp(binaryChunk)
207
208     for waitCnt in range(10):
209         response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
210         print('tries:', waitCnt, 'isInstalled:', response.isInstalled)
211         time.sleep(1)
212         if response.isInstalled: return True
213     return False
214
215 def removeAppTest(stub):
216     response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
217     if (response.isInstalled): response = stub.removeApp(ReqRemoveApp(packageName='org.example.uicomponents'))
218     for waitCnt in range(10):
219         response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
220         print('tries:', waitCnt, 'isInstalled:', response.isInstalled)
221         time.sleep(1)
222         if response.isInstalled != True: return True
223     return False
224
225 def getAppInfoTest(stub):
226     return stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents')).isRunning
227
228 def launchAppTest(stub):
229     print('launch result', stub.launchApp(ReqLaunchApp(packageName='org.example.uicomponents')))
230
231     return stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents')).isRunning
232
233 def closeAppTest(stub):
234     print('close result',stub.closeApp(ReqCloseApp(packageName='org.example.uicomponents')))
235     return stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents')).isRunning != True
236
237 def sendKeyTest(stub):
238     response = stub.sendKey(ReqKey(type='POWER', actionType='STROKE'))
239     time.sleep(3)
240     response = stub.sendKey(ReqKey(type='POWER', actionType='STROKE'))
241     time.sleep(3)
242     response = stub.sendKey(ReqKey(type='BACK', actionType='STROKE'))
243     time.sleep(5)
244     response = stub.sendKey(ReqKey(type='WHEELUP', actionType='STROKE'))
245     time.sleep(0.3)
246     response = stub.sendKey(ReqKey(type='WHEELUP', actionType='STROKE'))
247     time.sleep(0.3)
248     response = stub.sendKey(ReqKey(type='WHEELDOWN', actionType='STROKE'))
249     time.sleep(0.3)
250     return True
251
252 def scrollToTest(stub):
253     print('scrollTo command not implemented')
254     return False
255
256 def getDeviceTimeTest(stub):
257     response1 = stub.getDeviceTime(ReqGetDeviceTime(type='WALLCLOCK'))
258     response2 = stub.getDeviceTime(ReqGetDeviceTime(type='WALLCLOCK'))
259     print(response1, response2)
260     return response2.timestampUTC > response1.timestampUTC;
261
262 def getLocationTest(stub):
263     response = stub.getLocation(ReqGetLocation())
264
265     if response.alt < 0: return False
266     if response.lat < 0: return False
267     return True
268
269 def defaultSetup(stub):
270     if stub.getAppInfo(ReqGetAppInfo(packageName='com.samsung.ui-widget-sample')).isRunning:
271         stub.closeApp(ReqCloseApp(packageName='com.samsung.ui-widget-sample'))
272
273     stub.launchApp(ReqLaunchApp(packageName='com.samsung.ui-widget-sample'))
274
275 def defaultTearDown(stub):
276     stub.closeApp(ReqCloseApp(packageName='com.samsung.ui-widget-sample'))
277
278 def runTest(stub, testFunc, setup=defaultSetup, tearDown=defaultTearDown, alwaySucceed=False):
279     print("Testing started :", testFunc)
280
281     setup(stub)
282     result = testFunc(stub)
283     tearDown(stub)
284
285     if alwaySucceed: return True
286     assert True == result
287
288 def runTestWithoutSetupAndTearDown(stub, testFunc, setup=defaultSetup, tearDown=defaultTearDown):
289     def Empty(stub):
290         pass
291
292     runTest(stub, testFunc, Empty, Empty)
293
294
295 def run():
296     with grpc.insecure_channel('127.0.0.1:50051') as channel:
297         stub = BootstrapStub(channel)
298
299         runTest(stub, getDeviceTimeTest)
300
301         runTest(stub, findElementTest)
302         runTest(stub, getValueTest)
303         runTest(stub, getSizeTest)
304         runTest(stub, getAttributeTest)
305         runTest(stub, clickTest)
306         runTest(stub, flickTest)
307         runTest(stub, touchTest)
308         runTest(stub, sendKeyTest)
309         runTest(stub, setValueClearTest)
310         runTestWithoutSetupAndTearDown(stub, installAppTest)
311         runTestWithoutSetupAndTearDown(stub, launchAppTest)
312         runTestWithoutSetupAndTearDown(stub, getAppInfoTest)
313         runTestWithoutSetupAndTearDown(stub, closeAppTest)
314         runTestWithoutSetupAndTearDown(stub, removeAppTest)
315
316         runTest(stub, longClickTest, alwaySucceed=True)
317         runTest(stub, getLocationTest, alwaySucceed=True)
318
319         runTest(stub, scrollToTest, alwaySucceed=True)
320
321 if __name__ == '__main__':
322     logging.basicConfig()
323     run()