bootstrap: Fix dumpObject not work issue
[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     ]
99     isFailed = False
100     for el in response.elements:
101         for attr in checkList:
102             if stub.getAttribute(ReqGetAttribute(elementId=el.elementId, attribute=attr[0])).boolValue != attr[1]:
103                 isFailed = True
104
105     if isFailed == True: return False
106
107     response = stub.findElement(ReqFindElement(textField='Internal Legacy'))
108     if len(response.elements) <= 0: return False
109
110     checkList = [
111             ['VISIBLE',     True],
112             ['FOCUSABLE',   True],
113             ['FOCUSED',     False],
114             ['ENABLED',     True],
115             ['CLICKABLE',   True],
116             ['SCROLLABLE',  False],
117             ['CHECKABLE',   False],
118             ['CHECKED',     False],
119             ['SELECTED',    False],
120             ['SELECTABLE',  True],
121             ['SHOWING',     False],
122     ]
123     isFailed = False
124     for el in response.elements:
125         for attr in checkList:
126             if stub.getAttribute(ReqGetAttribute(elementId=el.elementId, attribute=attr[0])).boolValue != attr[1]:
127                 isFailed = True
128
129     return isFailed == False
130
131 def clickTest(stub):
132     response = stub.findElement(ReqFindElement(textField='Widgets'))
133     if len(response.elements) <= 0: return False
134
135     for el in response.elements:
136         stub.click(ReqClick(elementId=el.elementId, type='ELEMENTID'))
137
138     response = stub.findElement(ReqFindElement(textField='Box'))
139     if len(response.elements) <= 0: return False
140
141     for el in response.elements:
142         stub.click(ReqClick(coordination=Point(x=160, y=160), type='COORD'))
143
144     response = stub.findElement(ReqFindElement(textField='Vertical Box'))
145     if len(response.elements) <= 0: return False
146
147     return True
148
149 def longClickTest(stub):
150     response = stub.sendKey(ReqKey(type='HOME', actionType='STROKE'))
151     stub.click(ReqClick(coordination=Point(x=160, y=160), type='COORD'))
152     # TODO : find out something changed
153     return False
154
155 def flickTest(stub):
156     response = stub.findElement(ReqFindElement(textField='Widgets'))
157     if len(response.elements) <= 0:
158         return False
159
160     for el in response.elements:
161         print(el)
162         stub.click(ReqClick(elementId=el.elementId, type='ELEMENTID'))
163         break
164
165     response = stub.findElement(ReqFindElement(textField='Box', isShowing=True))
166     if len(response.elements) <= 0: return False
167     targetObj = response.elements[0].elementId
168
169     for tryCnt in range(10):
170         print('Flick to bottom to find "Spinner" item @ tries:', tryCnt)
171         stub.flick(ReqFlick(startPoint=Point(x=160, y=359), endPoint=Point(x=160, y=1), durationMs=350))
172         response = stub.findElement(ReqFindElement(textField='Spinner.*'))
173         time.sleep(0.01)
174         print(response)
175         if len(response.elements) > 0: return True
176
177     return False
178
179 def touchTest(stub):
180     res = stub.touchDown(ReqTouchDown(coordination=Point(x=160,y=330)))
181     print(res)
182     seq = res.seqId
183     if seq < 0: return False
184     for yy in range(330, 30, -10):
185         stub.touchMove(ReqTouchMove(coordination=Point(x=160,y=yy), seqId=seq))
186     stub.touchUp(ReqTouchUp(coordination=Point(x=160,y=30), seqId=seq))
187
188     return True
189
190
191 def get_file_chunks(filename):
192    CHUNK_SIZE = 1024 * 1024
193    with open(filename, 'rb') as f:
194        while True:
195            piece = f.read(CHUNK_SIZE)
196            if len(piece) == 0:
197                return
198            yield ReqInstallApp(package=piece)
199
200
201 def installAppTest(stub):
202     response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
203     if (response.isInstalled): return True
204
205     tpkFile = './org.tizen.uicomponents.arm.tpk'
206     binaryChunk = get_file_chunks(tpkFile)
207     response = stub.installApp(binaryChunk)
208
209     for waitCnt in range(10):
210         response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
211         print('tries:', waitCnt, 'isInstalled:', response.isInstalled)
212         time.sleep(1)
213         if response.isInstalled: return True
214     return False
215
216 def removeAppTest(stub):
217     response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
218     if (response.isInstalled): response = stub.removeApp(ReqRemoveApp(packageName='org.example.uicomponents'))
219     for waitCnt in range(10):
220         response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
221         print('tries:', waitCnt, 'isInstalled:', response.isInstalled)
222         time.sleep(1)
223         if response.isInstalled != True: return True
224     return False
225
226 def getAppInfoTest(stub):
227     return stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents')).isRunning
228
229 def launchAppTest(stub):
230     print('launch result', stub.launchApp(ReqLaunchApp(packageName='org.example.uicomponents')))
231
232     return stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents')).isRunning
233
234 def closeAppTest(stub):
235     print('close result',stub.closeApp(ReqCloseApp(packageName='org.example.uicomponents')))
236     return stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents')).isRunning != True
237
238 def sendKeyTest(stub):
239     response = stub.sendKey(ReqKey(type='POWER', actionType='STROKE'))
240     time.sleep(3)
241     response = stub.sendKey(ReqKey(type='POWER', actionType='STROKE'))
242     time.sleep(3)
243     response = stub.sendKey(ReqKey(type='BACK', actionType='STROKE'))
244     time.sleep(5)
245     response = stub.sendKey(ReqKey(type='WHEELUP', actionType='STROKE'))
246     time.sleep(0.3)
247     response = stub.sendKey(ReqKey(type='WHEELUP', actionType='STROKE'))
248     time.sleep(0.3)
249     response = stub.sendKey(ReqKey(type='WHEELDOWN', actionType='STROKE'))
250     time.sleep(0.3)
251     return True
252
253 def scrollToTest(stub):
254     print('scrollTo command not implemented')
255     return False
256
257 def getDeviceTimeTest(stub):
258     response1 = stub.getDeviceTime(ReqGetDeviceTime(type='WALLCLOCK'))
259     response2 = stub.getDeviceTime(ReqGetDeviceTime(type='WALLCLOCK'))
260     print(response1, response2)
261     return response2.timestampUTC > response1.timestampUTC;
262
263 def getLocationTest(stub):
264     response = stub.getLocation(ReqGetLocation())
265
266     if response.alt < 0: return False
267     if response.lat < 0: return False
268     return True
269
270 def takeScreenshotTest(stub):
271     responses = stub.takeScreenshot(ReqTakeScreenshot())
272     image = open("screenshot.png", "wb")
273     for response in responses:
274         image.write(response.image)
275     image.close()
276     return True;
277
278 def defaultSetup(stub):
279     if stub.getAppInfo(ReqGetAppInfo(packageName='com.samsung.ui-widget-sample')).isRunning:
280         stub.closeApp(ReqCloseApp(packageName='com.samsung.ui-widget-sample'))
281
282     stub.launchApp(ReqLaunchApp(packageName='com.samsung.ui-widget-sample'))
283
284 def defaultTearDown(stub):
285     stub.closeApp(ReqCloseApp(packageName='com.samsung.ui-widget-sample'))
286
287 def runTest(stub, testFunc, setup=defaultSetup, tearDown=defaultTearDown, alwaySucceed=False):
288     print("Testing started :", testFunc)
289
290     setup(stub)
291     result = testFunc(stub)
292     tearDown(stub)
293
294     if alwaySucceed: return True
295     assert True == result
296
297 def runTestWithoutSetupAndTearDown(stub, testFunc, setup=defaultSetup, tearDown=defaultTearDown):
298     def Empty(stub):
299         pass
300
301     runTest(stub, testFunc, Empty, Empty)
302
303
304 def run():
305     with grpc.insecure_channel('127.0.0.1:50051') as channel:
306         stub = BootstrapStub(channel)
307
308         runTest(stub, getDeviceTimeTest)
309
310         runTest(stub, findElementTest)
311         runTest(stub, getValueTest)
312         runTest(stub, getSizeTest)
313         runTest(stub, getAttributeTest)
314         runTest(stub, clickTest)
315         runTest(stub, flickTest)
316         runTest(stub, touchTest)
317         runTest(stub, sendKeyTest)
318         runTest(stub, setValueClearTest)
319         runTest(stub, takeScreenshotTest)
320         runTestWithoutSetupAndTearDown(stub, installAppTest)
321         runTestWithoutSetupAndTearDown(stub, launchAppTest)
322         runTestWithoutSetupAndTearDown(stub, getAppInfoTest)
323         runTestWithoutSetupAndTearDown(stub, closeAppTest)
324         runTestWithoutSetupAndTearDown(stub, removeAppTest)
325
326         runTest(stub, longClickTest, alwaySucceed=True)
327         runTest(stub, getLocationTest, alwaySucceed=True)
328
329         runTest(stub, scrollToTest, alwaySucceed=True)
330
331 if __name__ == '__main__':
332     logging.basicConfig()
333     run()