aurum: re-arrange files and add guide & scripts for TV
[platform/core/uifw/aurum.git] / protocol / resources / python / mobile / mobileDemoTest / mobileDemoTest.py
1 from __future__ import print_function
2 import os
3 import sys
4 sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(os.path.dirname(__file__)))))
5 from __future__ import print_function
6 from aurum_pb2 import *
7 from aurum_pb2_grpc import BootstrapStub
8 import logging
9 import grpc
10 import time
11
12 def findElementTest(stub):
13     response = stub.findElement(ReqFindElement(isClickable=True))
14     for el in response.elements:
15         return True
16     return False
17
18 def getValueTest(stub):
19     response = stub.findElement(ReqFindElement(textField='Button'))
20     print("els", response)
21     for el in response.elements:
22         response = stub.getValue(ReqGetValue(elementId=el.elementId))
23         return response.stringValue == 'Button'
24     return False
25
26 def setValueClearTest(stub):
27     def inScreen(size):
28         if size.x < 0: return False
29         if size.y < 0: return False
30         if size.x >= 720: return False
31         if size.y >= 1280: 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'))
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='Singleline Entry'))
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(widgetType='Elm_Entry'))
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 != '':
69         return False
70
71     return True
72
73 def getSizeTest(stub):
74     response = stub.findElement(ReqFindElement(textField='Button'))
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='Button'))
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='Check'))
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',     True],
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='Accessibility'))
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='Screen Reader'))
139     if len(response.elements) <= 0: return False
140
141     for el in response.elements:
142         stub.click(ReqClick(coordination=Point(x=320, y=130), type='COORD'))
143
144     response = stub.findElement(ReqFindElement(textField='UI Descriptions'))
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='Bg', isShowing=True))
157     if len(response.elements) <= 0: return False
158     targetObj = response.elements[0].elementId
159
160     for tryCnt in range(10):
161         print('Flick to bottom to find "Spinner" item @ tries:', tryCnt)
162         stub.flick(ReqFlick(startPoint=Point(x=160, y=359), endPoint=Point(x=160, y=1), durationMs=110))
163         response = stub.findElement(ReqFindElement(textField='Spinner'))
164         time.sleep(0.01)
165         print(response)
166         if len(response.elements) > 0:
167             for el in response.elements:
168                 if stub.getAttribute(ReqGetAttribute(elementId=el.elementId, attribute='SHOWING')).boolValue: return True
169
170     return False
171
172 def touchTest(stub):
173     res = stub.touchDown(ReqTouchDown(coordination=Point(x=160,y=640)))
174     print(res)
175     seq = res.seqId
176     if seq < 0: return False
177     for yy in range(640, 30, -10):
178         stub.touchMove(ReqTouchMove(coordination=Point(x=160,y=yy), seqId=seq))
179     stub.touchUp(ReqTouchUp(coordination=Point(x=160,y=30), seqId=seq))
180
181     return True
182
183
184 def get_file_chunks(filename):
185    CHUNK_SIZE = 1024 * 1024
186    with open(filename, 'rb') as f:
187        while True:
188            piece = f.read(CHUNK_SIZE)
189            if len(piece) == 0:
190                return
191            yield ReqInstallApp(package=piece)
192
193
194 def installAppTest(stub):
195     response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
196     if (response.isInstalled): return True
197
198     tpkFile = './org.tizen.uicomponents.arm.tpk'
199     binaryChunk = get_file_chunks(tpkFile)
200     response = stub.installApp(binaryChunk)
201
202     for waitCnt in range(10):
203         response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
204         print('tries:', waitCnt, 'isInstalled:', response.isInstalled)
205         time.sleep(1)
206         if response.isInstalled: return True
207     return False
208
209 def removeAppTest(stub):
210     response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
211     if (response.isInstalled): response = stub.removeApp(ReqRemoveApp(packageName='org.example.uicomponents'))
212     for waitCnt in range(10):
213         response = stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents'))
214         print('tries:', waitCnt, 'isInstalled:', response.isInstalled)
215         time.sleep(1)
216         if response.isInstalled != True: return True
217     return False
218
219 def getAppInfoTest(stub):
220     return stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents')).isRunning
221
222 def launchAppTest(stub):
223     print('launch result', stub.launchApp(ReqLaunchApp(packageName='org.example.uicomponents')))
224
225     return stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents')).isRunning
226
227 def closeAppTest(stub):
228     print('close result',stub.closeApp(ReqCloseApp(packageName='org.example.uicomponents')))
229     return stub.getAppInfo(ReqGetAppInfo(packageName='org.example.uicomponents')).isRunning != True
230
231 def sendKeyTest(stub):
232     response = stub.sendKey(ReqKey(type='WHEELUP', actionType='STROKE'))
233     time.sleep(0.3)
234     response = stub.sendKey(ReqKey(type='WHEELUP', actionType='STROKE'))
235     time.sleep(0.3)
236     response = stub.sendKey(ReqKey(type='WHEELDOWN', actionType='STROKE'))
237     time.sleep(0.3)
238     response = stub.sendKey(ReqKey(type='POWER', actionType='STROKE'))
239     time.sleep(1)
240     response = stub.sendKey(ReqKey(type='POWER', actionType='STROKE'))
241     time.sleep(1)
242     response = stub.sendKey(ReqKey(type='BACK', actionType='STROKE'))
243     time.sleep(1)
244     response = stub.sendKey(ReqKey(type='MENU', actionType='STROKE'))
245     time.sleep(1)
246     return True
247
248 def getDeviceTimeTest(stub):
249     response1 = stub.getDeviceTime(ReqGetDeviceTime(type='WALLCLOCK'))
250     response2 = stub.getDeviceTime(ReqGetDeviceTime(type='WALLCLOCK'))
251     print(response1, response2)
252     return response2.timestampUTC > response1.timestampUTC;
253
254 def getLocationTest(stub):
255     response = stub.getLocation(ReqGetLocation())
256
257     if response.alt < 0: return False
258     if response.lat < 0: return False
259     return True
260
261 def takeScreenshotTest(stub):
262     responses = stub.takeScreenshot(ReqTakeScreenshot())
263     image = open("screenshot.png", "wb")
264     for response in responses:
265         image.write(response.image)
266     image.close()
267     return True;
268
269 def defaultSetup(stub):
270     if stub.getAppInfo(ReqGetAppInfo(packageName='org.tizen.elm-demo-tizen-mobile')).isRunning:
271         stub.closeApp(ReqCloseApp(packageName='org.tizen.elm-demo-tizen-mobile'))
272
273     stub.launchApp(ReqLaunchApp(packageName='org.tizen.elm-demo-tizen-mobile'))
274
275 def defaultTearDown(stub):
276     stub.closeApp(ReqCloseApp(packageName='org.tizen.elm-demo-tizen-mobile'))
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     print("Testing result :", result)
286     if alwaySucceed: return True
287     assert True == result
288
289 def runTestWithoutSetupAndTearDown(stub, testFunc, setup=defaultSetup, tearDown=defaultTearDown):
290     def Empty(stub):
291         pass
292
293     runTest(stub, testFunc, Empty, Empty)
294
295
296 def run():
297     with grpc.insecure_channel('127.0.0.1:50051') as channel:
298         stub = BootstrapStub(channel)
299
300         runTest(stub, getDeviceTimeTest)
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         runTest(stub, takeScreenshotTest)
311         runTest(stub, longClickTest, alwaySucceed=True)
312         runTest(stub, getLocationTest, alwaySucceed=True)
313
314         runTestWithoutSetupAndTearDown(stub, installAppTest)
315         runTestWithoutSetupAndTearDown(stub, launchAppTest)
316         runTestWithoutSetupAndTearDown(stub, getAppInfoTest)
317         runTestWithoutSetupAndTearDown(stub, closeAppTest)
318         runTestWithoutSetupAndTearDown(stub, removeAppTest)
319
320 if __name__ == '__main__':
321     logging.basicConfig()
322     run()