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