-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathtest.py
More file actions
625 lines (516 loc) · 15.3 KB
/
test.py
File metadata and controls
625 lines (516 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
from __future__ import print_function, unicode_literals
import sys
import types
import traceback
# Test imports.
import time
droid = None
skip_gui = False
fOutName = True
# tests for python modification for android {{{1
def test_029_isfile(): # issue #29 {{{1
import os
# FIXME: determine path to sdcard. like: path = os.environ[""]
path = os.path.dirname(__file__)
fname = os.path.abspath(os.path.join(path, "test_isfile"))
open(fname, "w").write("this is test")
os.path.isfile(fname)
os.remove(fname)
try:
assert os.path.isfile(fname) is False
except Exception as e:
print(e)
return False
return True
def test_047_ttyname(): # issue #47 {{{1
import os
try:
os.ttyname(0)
os.ttyname(1)
except Exception as e:
print(e)
return False
return True
def test_071_anydbm(): # issue #71 {{{1
import os
if sys.version_info[0] == 2:
import anydbm
else:
import dbm as anydbm
# FIXME: determine path to sdcard. like: path = os.environ[""]
del os.chmod
for fname in (
# failed: this is not SL4A application folder...
# os.path.join("/data/data/com.googlecode.pythonforandroid",
# "files", "test_anydbm.dbm"),
# OK: _chmod work well.
# os.path.join("/data/local/abc", "test_anydbm.dbm"),
# failed: _chmod not worked in FAT (SD card)
os.path.join("/sdcard", "sl4a", "test_anydbm.dbm"),
):
try:
os.remove(fname + ".dat")
except:
pass
anydbm.open(fname, "n")
os.remove(fname + ".dat")
return True
def test_075_httpserver(): # issue #75 {{{1
import time
import threading
if sys.version_info[0] == 2:
from BaseHTTPServer import BaseHTTPRequestHandler as handler
from BaseHTTPServer import HTTPServer
else:
from http.server import BaseHTTPRequestHandler as handler
from http.server import HTTPServer
fname = "/sdcard/sl4a/test_075.html"
port = 9090
class Handler(handler):
def do_GET(s):
open(fname, "w").write("""
<html><head></head><body>fine 075</body></html>""")
html = open(fname, 'rb')
s.send_response(200)
s.send_header("Content-Type", "text/html")
s.end_headers()
s.wfile.write(html.read())
server_class = HTTPServer
httpd = server_class(('', port), Handler)
if not skip_gui:
# and manual test has passed, open http://127.0.0.1:9090 in browser.
th = threading.Thread(target=httpd.serve_forever)
th.start()
droid.startActivity('android.intent.action.VIEW',
'http://127.0.0.1:9090/')
time.sleep(3)
httpd.shutdown()
return True
def test_106_https_certification_failed(): # issue #106 {{{1
if sys.version_info[0] == 2:
import urllib2
else:
from urllib import request as urllib2
import os
import take_cacert_pem
fname = take_cacert_pem.main()
if not fname:
return False
os.environ["SSL_CERT_FILE"] = fname
# input = input.replace("!web ", "")
url = "https://ccc.de/"
# url = "https://www.openssl.org/"
req = urllib2.Request(url)
info = urllib2.urlopen(req).read()
info
# Message.Chat.SendMessage("" + info)
'''not worked...
import httplib
c = httplib.HTTPSConnection("ccc.de")
c.request("GET", "/")
response = c.getresponse()
print("%s,%s" % (response.status, response.reason))
data = response.read()
print(data)
'''
return True
def test_107_large_file_report(): # issue #107 {{{1
import os
errors = []
fname = "sample.bin"
for n in (4294967294, 4294967297):
fp = open(fname, "wb")
fp.seek(n)
fp.write("1".encode("utf-8"))
fp.close()
ans = os.path.getsize(fname)
if ans != (n + 1):
errors.append("%s(answer) vs %s(expected)" % (ans, n + 1))
os.remove(fname)
if not errors:
return True
print("can't get size collectly with %s" % str(errors))
return False
def test_013s_scanBarcode(): # issue sl4a #13 {{{1
if not skip_gui:
code = droid.scanBarcode()
ext = code.result.get('extras', None)
if ext is None:
return False
if 'SCAN_RESULT_BYTES' not in ext or 'SCAN_RESULT' not in ext:
print("no results:" + str(ext))
return False
bts = ext['SCAN_RESULT_BYTES']
msg = ext['SCAN_RESULT']
print(msg, bts, len(bts))
return True
def test_009s_airplanemode(): # issue sl4a #9 {{{1
# this cause null pointer exception in Anroid 4.4>
ret = droid.checkAirplaneMode()
if ret.error:
return False
if fOutName:
print("%s" % ret.result, end="")
ret = droid.toggleAirplaneMode(True)
if ret.error:
return False
return True
def test_032s_wificonnect(): # issue sl4a #32 {{{1
method = "WPA2"
if method == "no-security":
cfg = dict(
SSID="invalidwifi",
# below parameters are not used in example of my expalation site.
# BSSID=,
# hiddenSSID=False,
# priority=,
# apBand=,
)
elif method == "WEP":
cfg = dict(
SSID="invalidwifi",
wepKeys=["key0"],
wepTxKeyIndex=0,
)
else: # elif method == "WPA2":
cfg = dict(
SSID="invalidwifi",
preSharedKey="kuaihuawifi123",
# or you can use: password="presharedkey",
# be careful SL4A can't allow 64byte key.
)
droid.wifiConnect(cfg)
return True
# tests for some facade {{{1
def event_loop():
for i in range(10):
time.sleep(1)
droid.eventClearBuffer()
time.sleep(1)
e = droid.eventPoll(1)
if e.result is not None:
return True
return False
def test_imports():
try:
import termios
import bs4 as BeautifulSoup
import pyxmpp2 as xmpp
from xml.dom import minidom
except ImportError:
return False
return True
def test_clipboard():
previous = droid.getClipboard().result
msg = 'Hello, world!'
droid.setClipboard(msg)
echo = droid.getClipboard().result
droid.setClipboard(previous)
return echo == msg
def test_gdata():
if True:
try:
import gdata.docs.service
global skip_gui
if skip_gui:
return True
except:
return False
# Create a client class which will make HTTP requests with Google Docs server.
client = gdata.docs.service.DocsService()
# Authenticate using your Google Docs email address and password.
username = droid.dialogGetInput('Username').result
password = droid.dialogGetPassword('Password', 'For ' + username).result
try:
client.ClientLogin(username, password)
except:
return False
# Query the server for an Atom feed containing a list of your documents.
documents_feed = client.GetDocumentListFeed()
# Loop through the feed and extract each document entry.
return bool(list(documents_feed.entry))
def test_gps():
droid.startLocating()
try:
return event_loop()
finally:
droid.stopLocating()
def test_battery():
droid.batteryStartMonitoring()
time.sleep(1)
try:
return bool(droid.batteryGetStatus())
finally:
droid.batteryStopMonitoring()
def test_sensors():
ret = droid.startSensingTimed(1, 20)
if ret.error:
return False
try:
return event_loop()
finally:
droid.stopSensing()
def test_speak():
result = droid.ttsSpeak('Hello, world!')
return result.error is None
def test_phone_state():
droid.startTrackingPhoneState()
try:
return event_loop()
finally:
droid.stopTrackingPhoneState()
def test_ringer_silent():
result1 = droid.toggleRingerSilentMode()
result2 = droid.toggleRingerSilentMode()
return result1.error is None and result2.error is None
def test_ringer_volume():
get_result = droid.getRingerVolume()
if get_result.error is not None:
return False
droid.setRingerVolume(0)
set_result = droid.setRingerVolume(get_result.result)
if set_result.error is not None:
return False
return True
def test_get_last_known_location():
result = droid.getLastKnownLocation()
return result.error is None
def test_geocode():
result = droid.geocode(0.0, 0.0, 1)
return result.error is None
def test_wifi():
result1 = droid.toggleWifiState()
result2 = droid.toggleWifiState()
return result1.error is None and result2.error is None
def test_make_toast():
result = droid.makeToast('Hello, world!')
return result.error is None
def test_vibrate():
result = droid.vibrate()
return result.error is None
def test_notify():
result = droid.notify('Test Title', 'Hello, world!')
return result.error is None
def test_get_running_packages():
result = droid.getRunningPackages()
return result.error is None
# tests for USBSerialFacade {{{1
def test_usb(): # {{{2
result = droid.usbserialDeviceList()
if result.error is None:
print(result.data)
return True
return False
# tests for SL4A GUI parts {{{1
def test_alert_dialog(): # {{{2
global skip_gui
if skip_gui:
return None
title = 'User Interface'
message = 'Welcome to the SL4A integration test.'
droid.dialogCreateAlert(title, message)
droid.dialogSetPositiveButtonText('Continue')
droid.dialogShow()
response = droid.dialogGetResponse().result
return True
def test__alert_dialog_with_buttons(): # {{{2
global skip_gui
if skip_gui:
return None
title = 'Alert'
message = ('This alert box has 3 buttons and '
'will wait for you to press one.')
droid.dialogCreateAlert(title, message)
droid.dialogSetPositiveButtonText('Yes')
droid.dialogSetNegativeButtonText('No')
droid.dialogSetNeutralButtonText('Cancel')
droid.dialogShow()
response = droid.dialogGetResponse().result
assert response['which'] in ('positive', 'negative', 'neutral')
# print("debug:", response)
skip_gui = response['which'] == "negative"
return True
def test_spinner_progress(): # {{{2
title = 'Spinner'
message = 'This is simple spinner progress.'
droid.dialogCreateSpinnerProgress(title, message)
droid.dialogShow()
time.sleep(2)
droid.dialogDismiss()
return True
def test_horizontal_progress(): # {{{2
title = 'Horizontal'
message = 'This is simple horizontal progress.'
droid.dialogCreateHorizontalProgress(title, message, 50)
droid.dialogShow()
for x in range(0, 15):
time.sleep(0.1)
droid.dialogSetCurrentProgress(x)
droid.dialogDismiss()
return True
def test__alert_dialog_with_list(): # {{{2
global skip_gui
if skip_gui:
return None
title = 'Alert'
droid.dialogCreateAlert(title)
droid.dialogSetItems(['foo', 'bar', 'baz'])
droid.dialogShow()
response = droid.dialogGetResponse().result
# print("debug:", response)
skip_gui = response.item == 1
return True
def test__alert_dialog_with_single_choice_list(): # {{{2
global skip_gui
if skip_gui:
return None
title = 'GUI Test?'
droid.dialogCreateAlert(title)
droid.dialogSetSingleChoiceItems(['Continue', 'Skip', 'baz'])
droid.dialogSetPositiveButtonText('Yay!')
droid.dialogShow()
response = droid.dialogGetResponse().result
choices = droid.dialogGetSelectedItems().result
skip_gui = 1 in choices
return True
def test__alert_dialog_with_multi_choice_list(): # {{{2
global skip_gui
if skip_gui:
return None
title = 'Alert'
droid.dialogCreateAlert(title)
droid.dialogSetMultiChoiceItems(['foo', 'bar', 'baz'], [])
droid.dialogSetPositiveButtonText('Yay!')
droid.dialogShow()
response = droid.dialogGetResponse().result
choices = droid.dialogGetSelectedItems().result
# print("debug:", choices)
skip_gui = 1 in choices
return True
# tests for native module {{{1
def test_ssl():
try:
import ssl
except:
return False
# TODO: make test method
ssl # missing ssl extension?
return True
def test_ctypes():
try:
import ctypes
except:
return False
# TODO: make test method
ctypes # r17-22, this cause segfault error.
return True
def test_readline():
try:
import readline
except:
return False
# TODO: make test method
readline
return True
def test0_curses(): # {{{2
import os
if not os.environ.get("TERM", ""):
os.environ["TERM"] = "vt100"
os.environ["TERMINFO"] = ("/data/data/com.googlecode.pythonforandroid"
"/files/python/share/terminfo")
try:
import _curses
except:
return False
_curses.initscr()
_curses.endwin()
return True
def test_termios():
try:
import termios
except:
return False
# TODO: make test method
termios
return True
def test_bz2():
try:
import bz2
except:
return False
# TODO: make test method
bz2
return True
def test_expat():
try:
import pyexpat
except:
return False
# TODO: make test method
pyexpat
return True
def test_sqlite3():
try:
import sqlite3
except:
return False
# TODO: make test method
sqlite3
return True
# tests for pure python module {{{1
def test_bs():
try:
import BeautifulSoup
except:
return False
# TODO: make test method
BeautifulSoup
return True
def test_xmpp():
try:
import xmpp
except:
return False
# TODO: make test method
xmpp
return True
if __name__ == '__main__': # {{{1
try:
import android
except:
import os
sys.path.insert(0, os.path.dirname(__file__))
import android_mock as android # for PC debugging
droid = android.Android()
def boilerplate(f):
try:
ret = f()
except:
print(traceback.format_exc())
return False
return ret
seq = globals().items()
seq = [i for i in seq if i[0].startswith("test_")]
seq = [i for i in seq if isinstance(i[1], types.FunctionType)]
seq.sort(key=lambda x: x[0])
for name, value in seq:
if fOutName:
print('Running %s...' % name, end="")
f = boilerplate(value)
sys.stdout.flush()
if f is True:
print(' PASS')
elif f is None:
print(' SKIP')
else:
print(' FAIL')
else:
sys.stdout.flush()
f = boilerplate(value)
if f is True:
print(".", end="")
elif f is None:
print("S", end="")
else:
print("F:%s" % name, end="")
# vi: ft=python:et:ts=4:fdm=marker