This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
gaia_test.py
954 lines (780 loc) · 39.3 KB
/
gaia_test.py
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import os
import shutil
import tempfile
import time
from marionette import MarionetteTestCase, EnduranceTestCaseMixin, \
B2GTestCaseMixin, MemoryEnduranceTestCaseMixin
from marionette.by import By
from marionette import expected
from marionette.errors import NoSuchElementException
from marionette.errors import StaleElementException
from marionette.errors import InvalidResponseException
from marionette.wait import Wait
from file_manager import GaiaDeviceFileManager, GaiaLocalFileManager
class GaiaApp(object):
def __init__(self, origin=None, name=None, frame=None, src=None):
self.frame = frame
self.frame_id = frame
self.src = src
self.name = name
self.origin = origin
def __eq__(self, other):
return self.__dict__ == other.__dict__
class GaiaApps(object):
def __init__(self, marionette):
self.marionette = marionette
js = os.path.abspath(os.path.join(__file__, os.path.pardir, 'atoms', "gaia_apps.js"))
self.marionette.import_script(js)
def get_permission(self, app_name, permission_name):
self.marionette.switch_to_frame()
return self.marionette.execute_async_script("return GaiaApps.getPermission('%s', '%s')" % (app_name, permission_name))
def set_permission(self, app_name, permission_name, value):
self.marionette.switch_to_frame()
return self.marionette.execute_async_script("return GaiaApps.setPermission('%s', '%s', '%s')" %
(app_name, permission_name, value))
def launch(self, name, switch_to_frame=True, launch_timeout=None):
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script("GaiaApps.launchWithName('%s')" % name, script_timeout=launch_timeout)
assert result, "Failed to launch app with name '%s'" % name
app = GaiaApp(frame=result.get('frame'),
src=result.get('src'),
name=result.get('name'),
origin=result.get('origin'))
if app.frame_id is None:
raise Exception("App failed to launch; there is no app frame")
if switch_to_frame:
self.marionette.switch_to_frame(app.frame_id)
return app
@property
def displayed_app(self):
self.marionette.switch_to_frame()
result = self.marionette.execute_script('return GaiaApps.getDisplayedApp();')
return GaiaApp(frame=result.get('frame'),
src=result.get('src'),
name=result.get('name'),
origin=result.get('origin'))
def switch_to_displayed_app(self):
self.marionette.switch_to_default_content()
self.marionette.switch_to_frame(self.displayed_app.frame)
def is_app_installed(self, app_name):
self.marionette.switch_to_frame()
return self.marionette.execute_async_script("GaiaApps.locateWithName('%s')" % app_name)
def uninstall(self, name):
self.marionette.switch_to_frame()
self.marionette.execute_async_script("GaiaApps.uninstallWithName('%s')" % name)
def kill(self, app):
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script("GaiaApps.kill('%s');" % app.origin)
assert result, "Failed to kill app with name '%s'" % app.name
def kill_all(self):
# First we attempt to kill the FTU, we treat it as a user app
for app in self.running_apps(include_system_apps=True):
if app.origin == 'app://ftu.gaiamobile.org':
self.kill(app)
break
# Now kill the user apps
self.marionette.switch_to_frame()
self.marionette.execute_async_script("GaiaApps.killAll();")
@property
def installed_apps(self):
apps = self.marionette.execute_async_script(
'return GaiaApps.getInstalledApps();')
result = []
for app in [a for a in apps if not a['manifest'].get('role')]:
entry_points = app['manifest'].get('entry_points')
if entry_points:
for ep in entry_points.values():
result.append(GaiaApp(
origin=app['origin'],
name=ep['name']))
else:
result.append(GaiaApp(
origin=app['origin'],
name=app['manifest']['name']))
return result
def running_apps(self, include_system_apps=False):
''' Returns a list of running apps
Args:
include_system_apps: Includes otherwise hidden System apps in the list
Returns:
A list of GaiaApp objects representing the running apps.
'''
include_system_apps = json.dumps(include_system_apps)
self.marionette.switch_to_frame()
apps = self.marionette.execute_script(
"return GaiaApps.getRunningApps(%s);" % include_system_apps)
result = []
for app in [a[1] for a in apps.items()]:
result.append(GaiaApp(origin=app['origin'], name=app['name']))
return result
class GaiaData(object):
def __init__(self, marionette, testvars=None):
self.apps = GaiaApps(marionette)
self.marionette = marionette
self.testvars = testvars or {}
js = os.path.abspath(os.path.join(__file__, os.path.pardir, 'atoms', "gaia_data_layer.js"))
self.marionette.import_script(js)
def set_time(self, date_number):
self.marionette.set_context(self.marionette.CONTEXT_CHROME)
self.marionette.execute_script("window.navigator.mozTime.set(%s);" % date_number)
self.marionette.set_context(self.marionette.CONTEXT_CONTENT)
@property
def all_contacts(self):
self.marionette.switch_to_frame()
return self.marionette.execute_async_script('return GaiaDataLayer.getAllContacts();', special_powers=True)
@property
def sim_contacts(self):
self.marionette.switch_to_frame()
adn_contacts = self.marionette.execute_async_script('return GaiaDataLayer.getSIMContacts("adn");', special_powers=True)
sdn_contacts = self.marionette.execute_async_script('return GaiaDataLayer.getSIMContacts("sdn");', special_powers=True)
return adn_contacts + sdn_contacts
def insert_contact(self, contact):
self.marionette.switch_to_frame()
mozcontact = contact.create_mozcontact()
result = self.marionette.execute_async_script('return GaiaDataLayer.insertContact(%s);' % json.dumps(mozcontact), special_powers=True)
assert result, 'Unable to insert contact %s' % contact
def remove_all_contacts(self):
self.marionette.switch_to_frame()
timeout = max(self.marionette.timeout or 60000, 1000 * len(self.all_contacts))
result = self.marionette.execute_async_script('return GaiaDataLayer.removeAllContacts();', special_powers=True, script_timeout=timeout)
assert result, 'Unable to remove all contacts'
def get_setting(self, name):
return self.marionette.execute_async_script('return GaiaDataLayer.getSetting("%s")' % name, special_powers=True)
@property
def all_settings(self):
return self.get_setting('*')
def set_setting(self, name, value):
import json
value = json.dumps(value)
result = self.marionette.execute_async_script('return GaiaDataLayer.setSetting("%s", %s)' % (name, value), special_powers=True)
assert result, "Unable to change setting with name '%s' to '%s'" % (name, value)
def _get_pref(self, datatype, name):
self.marionette.switch_to_frame()
pref = self.marionette.execute_script("return SpecialPowers.get%sPref('%s');" % (datatype, name), special_powers=True)
return pref
def _set_pref(self, datatype, name, value):
value = json.dumps(value)
self.marionette.switch_to_frame()
self.marionette.execute_script("SpecialPowers.set%sPref('%s', %s);" % (datatype, name, value), special_powers=True)
def get_bool_pref(self, name):
"""Returns the value of a Gecko boolean pref, which is different from a Gaia setting."""
return self._get_pref('Bool', name)
def set_bool_pref(self, name, value):
"""Sets the value of a Gecko boolean pref, which is different from a Gaia setting."""
return self._set_pref('Bool', name, value)
def get_int_pref(self, name):
"""Returns the value of a Gecko integer pref, which is different from a Gaia setting."""
return self._get_pref('Int', name)
def set_int_pref(self, name, value):
"""Sets the value of a Gecko integer pref, which is different from a Gaia setting."""
return self._set_pref('Int', name, value)
def get_char_pref(self, name):
"""Returns the value of a Gecko string pref, which is different from a Gaia setting."""
return self._get_pref('Char', name)
def set_char_pref(self, name, value):
"""Sets the value of a Gecko string pref, which is different from a Gaia setting."""
return self._set_pref('Char', name, value)
def set_volume(self, value):
channels = ['alarm', 'content', 'notification']
for channel in channels:
self.set_setting('audio.volume.%s' % channel, value)
def bluetooth_enable(self):
self.marionette.switch_to_frame()
return self.marionette.execute_async_script("return GaiaDataLayer.enableBluetooth()")
def bluetooth_disable(self):
self.marionette.switch_to_frame()
return self.marionette.execute_async_script("return GaiaDataLayer.disableBluetooth()")
@property
def bluetooth_is_enabled(self):
return self.marionette.execute_script("return window.navigator.mozBluetooth.enabled")
@property
def is_cell_data_enabled(self):
return self.get_setting('ril.data.enabled')
def connect_to_cell_data(self):
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script("return GaiaDataLayer.connectToCellData()", special_powers=True)
assert result, 'Unable to connect to cell data'
def disable_cell_data(self):
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script("return GaiaDataLayer.disableCellData()", special_powers=True)
assert result, 'Unable to disable cell data'
@property
def is_cell_data_connected(self):
# XXX: check bug-926169
# this is used to keep all tests passing while introducing multi-sim APIs
return self.marionette.execute_script('var mobileConnection = window.navigator.mozMobileConnection || ' +
'window.navigator.mozMobileConnections && ' +
'window.navigator.mozMobileConnections[0]; ' +
'return mobileConnection.data.connected;')
def enable_cell_roaming(self):
self.set_setting('ril.data.roaming_enabled', True)
def disable_cell_roaming(self):
self.set_setting('ril.data.roaming_enabled', False)
@property
def is_wifi_enabled(self):
return self.marionette.execute_script("return window.navigator.mozWifiManager && "
"window.navigator.mozWifiManager.enabled;")
def enable_wifi(self):
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script("return GaiaDataLayer.enableWiFi()", special_powers=True)
assert result, 'Unable to enable WiFi'
def disable_wifi(self):
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script("return GaiaDataLayer.disableWiFi()", special_powers=True)
assert result, 'Unable to disable WiFi'
def connect_to_wifi(self, network=None):
network = network or self.testvars.get('wifi')
assert network, 'No WiFi network provided'
self.enable_wifi()
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script("return GaiaDataLayer.connectToWiFi(%s)" % json.dumps(network),
script_timeout = max(self.marionette.timeout, 60000))
assert result, 'Unable to connect to WiFi network'
def forget_all_networks(self):
self.marionette.switch_to_frame()
self.marionette.execute_async_script('return GaiaDataLayer.forgetAllNetworks()')
def is_wifi_connected(self, network=None):
network = network or self.testvars.get('wifi')
self.marionette.switch_to_frame()
return self.marionette.execute_script("return GaiaDataLayer.isWiFiConnected(%s)" % json.dumps(network))
@property
def known_networks(self):
known_networks = self.marionette.execute_async_script(
'return GaiaDataLayer.getKnownNetworks()')
return [n for n in known_networks if n]
@property
def active_telephony_state(self):
# Returns the state of only the currently active call or None if no active call
return self.marionette.execute_script("return GaiaDataLayer.getMozTelephonyState()")
@property
def is_antenna_available(self):
return self.marionette.execute_script('return window.navigator.mozFMRadio.antennaAvailable')
@property
def is_fm_radio_enabled(self):
return self.marionette.execute_script('return window.navigator.mozFMRadio.enabled')
@property
def fm_radio_frequency(self):
return self.marionette.execute_script('return window.navigator.mozFMRadio.frequency')
@property
def media_files(self):
result = []
result.extend(self.music_files)
result.extend(self.picture_files)
result.extend(self.video_files)
return result
def delete_all_sms(self):
self.marionette.switch_to_frame()
return self.marionette.execute_async_script("return GaiaDataLayer.deleteAllSms();", special_powers=True)
def get_all_sms(self):
self.marionette.switch_to_frame()
return self.marionette.execute_async_script("return GaiaDataLayer.getAllSms();", special_powers=True)
def delete_all_call_log_entries(self):
"""The call log needs to be open and focused in order for this to work."""
self.marionette.execute_script('window.wrappedJSObject.RecentsDBManager.deleteAll();')
def kill_active_call(self):
self.marionette.execute_script("var telephony = window.navigator.mozTelephony; " +
"if(telephony.active) telephony.active.hangUp();")
@property
def music_files(self):
return self.marionette.execute_async_script(
'return GaiaDataLayer.getAllMusic();')
@property
def picture_files(self):
return self.marionette.execute_async_script(
'return GaiaDataLayer.getAllPictures();')
@property
def video_files(self):
return self.marionette.execute_async_script(
'return GaiaDataLayer.getAllVideos();')
def sdcard_files(self, extension=''):
files = self.marionette.execute_async_script(
'return GaiaDataLayer.getAllSDCardFiles();')
if len(extension):
return [filename for filename in files if filename.endswith(extension)]
return files
def send_sms(self, number, message):
self.marionette.switch_to_frame()
import json
number = json.dumps(number)
message = json.dumps(message)
result = self.marionette.execute_async_script('return GaiaDataLayer.sendSMS(%s, %s)' % (number, message), special_powers=True)
assert result, 'Unable to send SMS to recipient %s with text %s' % (number, message)
# FIXME: Bug 1011000: will make use of SoundManager instead
def wait_for_audio_channel_changed(self):
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script("return GaiaDataLayer.waitForAudioChannelChanged();")
assert result, "Failed to get a mozChromeEvent audio-channel-changed"
return result
# FIXME: Bug 1011000: will make use of SoundManager instead
def wait_for_visible_audio_channel_changed(self):
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script("return GaiaDataLayer.waitForVisibleAudioChannelChanged();")
assert result, "Failed to get a mozChromeEvent visible-audio-channel-changed"
return result
class Accessibility(object):
def __init__(self, marionette):
self.marionette = marionette
js = os.path.abspath(os.path.join(__file__, os.path.pardir,
'atoms', "accessibility.js"))
self.marionette.import_script(js)
def is_hidden(self, element):
return self._run_async_script('isHidden', [element])
def is_visible(self, element):
return self._run_async_script('isVisible', [element])
def is_disabled(self, element):
return self._run_async_script('isDisabled', [element])
def click(self, element):
self._run_async_script('click', [element])
def wheel(self, element, direction):
self.marionette.execute_script('Accessibility.wheel.apply(Accessibility, arguments)', [
element, direction])
def get_name(self, element):
return self._run_async_script('getName', [element])
def get_role(self, element):
return self._run_async_script('getRole', [element])
def _run_async_script(self, func, args):
result = self.marionette.execute_async_script(
'return Accessibility.%s.apply(Accessibility, arguments)' % func,
args, special_powers=True)
if not result:
return
if result.has_key('error'):
message = 'accessibility.js error: %s' % result['error']
raise Exception(message)
return result.get('result', None)
class FakeUpdateChecker(object):
def __init__(self, marionette):
self.marionette = marionette
self.fakeupdatechecker_atom = os.path.abspath(
os.path.join(__file__, os.path.pardir, 'atoms', "fake_update-checker.js"))
def check_updates(self):
self.marionette.set_context(self.marionette.CONTEXT_CHROME)
self.marionette.import_script(self.fakeupdatechecker_atom)
self.marionette.execute_script("GaiaUITests_FakeUpdateChecker();")
self.marionette.set_context(self.marionette.CONTEXT_CONTENT)
class GaiaDevice(object):
def __init__(self, marionette, testvars=None, manager=None):
self.manager = manager
self.marionette = marionette
self.testvars = testvars or {}
if self.is_desktop_b2g:
self.file_manager = GaiaLocalFileManager(self)
# Use a temporary directory for storage
self.storage_path = tempfile.mkdtemp()
self._set_storage_path()
elif self.manager:
self.file_manager = GaiaDeviceFileManager(self)
# Use the device root for storage
self.storage_path = self.manager.deviceRoot
self.lockscreen_atom = os.path.abspath(
os.path.join(__file__, os.path.pardir, 'atoms', "gaia_lock_screen.js"))
def _set_storage_path(self):
if self.is_desktop_b2g:
# Override the storage location for desktop B2G. This will only
# work if the B2G instance is running locally.
GaiaData(self.marionette).set_char_pref(
'device.storage.overrideRootDir', self.storage_path)
@property
def is_android_build(self):
if self.testvars.get('is_android_build') is None:
self.testvars['is_android_build'] = 'android' in self.marionette.session_capabilities['platformName'].lower()
return self.testvars['is_android_build']
@property
def is_emulator(self):
if not hasattr(self, '_is_emulator'):
self._is_emulator = self.marionette.session_capabilities['device'] == 'qemu'
return self._is_emulator
@property
def is_desktop_b2g(self):
if self.testvars.get('is_desktop_b2g') is None:
self.testvars['is_desktop_b2g'] = self.marionette.session_capabilities['device'] == 'desktop'
return self.testvars['is_desktop_b2g']
@property
def is_online(self):
# Returns true if the device has a network connection established (cell data, wifi, etc)
return self.marionette.execute_script('return window.navigator.onLine;')
@property
def has_mobile_connection(self):
# XXX: check bug-926169
# this is used to keep all tests passing while introducing multi-sim APIs
return self.marionette.execute_script('var mobileConnection = window.navigator.mozMobileConnection || ' +
'window.navigator.mozMobileConnections && ' +
'window.navigator.mozMobileConnections[0]; ' +
'return mobileConnection !== undefined')
@property
def has_wifi(self):
if not hasattr(self, '_has_wifi'):
self._has_wifi = self.marionette.execute_script('return window.navigator.mozWifiManager !== undefined')
return self._has_wifi
def restart_b2g(self):
self.stop_b2g()
time.sleep(2)
self.start_b2g()
def start_b2g(self, timeout=60):
if self.marionette.instance:
# launch the gecko instance attached to marionette
self.marionette.instance.start()
elif self.is_android_build:
self.manager.shellCheckOutput(['start', 'b2g'])
else:
raise Exception('Unable to start B2G')
self.marionette.wait_for_port()
self.marionette.start_session()
self.wait_for_b2g_ready(timeout)
# Reset the storage path for desktop B2G
self._set_storage_path()
def wait_for_b2g_ready(self, timeout):
# Wait for the homescreen to finish loading
Wait(self.marionette, timeout).until(expected.element_present(
By.CSS_SELECTOR, '#homescreen[loading-state=false]'))
@property
def is_b2g_running(self):
return 'b2g' in self.manager.shellCheckOutput(['toolbox', 'ps'])
def stop_b2g(self, timeout=5):
if self.marionette.instance:
# close the gecko instance attached to marionette
self.marionette.instance.close()
elif self.is_android_build:
self.manager.shellCheckOutput(['stop', 'b2g'])
Wait(self.marionette, timeout=timeout).until(
lambda m: not self.is_b2g_running,
message='b2g failed to stop.')
else:
raise Exception('Unable to stop B2G')
self.marionette.client.close()
self.marionette.session = None
self.marionette.window = None
def press_sleep_button(self):
self.marionette.execute_script("""
window.wrappedJSObject.dispatchEvent(new CustomEvent('mozChromeEvent', {
detail: {
type: 'sleep-button-press'
}
}));""")
def press_release_volume_up_then_down_n_times(self, n_times):
self.marionette.execute_script("""
function sendEvent(aName, aType) {
window.wrappedJSObject.dispatchEvent(new CustomEvent('mozChromeEvent', {
detail: {
type: aName + '-button-' + aType
}
}));
}
for (var i = 0; i < arguments[0]; ++i) {
sendEvent('volume-up', 'press');
sendEvent('volume-up', 'release');
sendEvent('volume-down', 'press');
sendEvent('volume-down', 'release');
};""", script_args=[n_times])
def turn_screen_off(self):
self.marionette.execute_script("window.wrappedJSObject.ScreenManager.turnScreenOff(true)")
@property
def is_screen_enabled(self):
return self.marionette.execute_script('return window.wrappedJSObject.ScreenManager.screenEnabled')
def touch_home_button(self):
apps = GaiaApps(self.marionette)
if apps.displayed_app.name.lower() != 'homescreen':
# touching home button will return to homescreen
self._dispatch_home_button_event()
Wait(self.marionette).until(
lambda m: apps.displayed_app.name.lower() == 'homescreen')
apps.switch_to_displayed_app()
else:
apps.switch_to_displayed_app()
mode = self.marionette.find_element(By.TAG_NAME, 'body').get_attribute('class')
self._dispatch_home_button_event()
apps.switch_to_displayed_app()
if mode == 'edit-mode':
# touching home button will exit edit mode
Wait(self.marionette).until(lambda m: m.find_element(
By.TAG_NAME, 'body').get_attribute('class') != mode)
else:
# touching home button inside homescreen will scroll it to the top
Wait(self.marionette).until(lambda m: m.execute_script(
"return document.querySelector('.scrollable').scrollTop") == 0)
def _dispatch_home_button_event(self):
self.marionette.switch_to_frame()
self.marionette.execute_script("window.wrappedJSObject.dispatchEvent(new Event('home'));")
def hold_home_button(self):
self.marionette.switch_to_frame()
self.marionette.execute_script("window.wrappedJSObject.dispatchEvent(new Event('holdhome'));")
def hold_sleep_button(self):
self.marionette.switch_to_frame()
self.marionette.execute_script("window.wrappedJSObject.dispatchEvent(new Event('holdsleep'));")
@property
def is_locked(self):
self.marionette.switch_to_frame()
return self.marionette.execute_script('return window.wrappedJSObject.lockScreen.locked')
def lock(self):
self.marionette.import_script(self.lockscreen_atom)
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script('GaiaLockScreen.lock()')
assert result, 'Unable to lock screen'
Wait(self.marionette).until(lambda m: m.find_element(By.CSS_SELECTOR, 'div.lockScreenWindow.active'))
def unlock(self):
self.marionette.import_script(self.lockscreen_atom)
self.marionette.switch_to_frame()
result = self.marionette.execute_async_script('GaiaLockScreen.unlock()')
assert result, 'Unable to unlock screen'
def change_orientation(self, orientation):
""" There are 4 orientation states which the phone can be passed in:
portrait-primary(which is the default orientation), landscape-primary, portrait-secondary and landscape-secondary
"""
self.marionette.execute_async_script("""
if (arguments[0] === arguments[1]) {
marionetteScriptFinished();
}
else {
var expected = arguments[1];
window.screen.onmozorientationchange = function(e) {
console.log("Received 'onmozorientationchange' event.");
waitFor(
function() {
window.screen.onmozorientationchange = null;
marionetteScriptFinished();
},
function() {
return window.screen.mozOrientation === expected;
}
);
};
console.log("Changing orientation to '" + arguments[1] + "'.");
window.screen.mozLockOrientation(arguments[1]);
};""", script_args=[self.screen_orientation, orientation])
@property
def screen_width(self):
return self.marionette.execute_script('return window.screen.width')
@property
def screen_orientation(self):
return self.marionette.execute_script('return window.screen.mozOrientation')
class GaiaTestCase(MarionetteTestCase, B2GTestCaseMixin):
def __init__(self, *args, **kwargs):
self.restart = kwargs.pop('restart', False)
MarionetteTestCase.__init__(self, *args, **kwargs)
B2GTestCaseMixin.__init__(self, *args, **kwargs)
def setUp(self):
try:
MarionetteTestCase.setUp(self)
except InvalidResponseException:
if self.restart:
pass
# TODO: Once bug 1019043 is fixed we will be able to just use
# self.device_manager instead of guarding for desktop B2G
device_manager = None
if not self.marionette.session_capabilities['device'] == 'desktop':
device_manager = self.device_manager
self.device = GaiaDevice(self.marionette,
manager=device_manager,
testvars=self.testvars)
if self.restart and (self.device.is_android_build or self.marionette.instance):
# Restart if it's a device, or we have passed a binary instance with --binary command arg
self.device.stop_b2g()
if self.device.is_android_build:
self.cleanup_data()
self.device.start_b2g()
# Run the fake update checker
FakeUpdateChecker(self.marionette).check_updates()
# We need to set the default timeouts because we may have a new session
if self.marionette.timeout is None:
# if no timeout is passed in, we detect the hardware type and set reasonable defaults
timeouts = {}
if self.device.is_desktop_b2g:
self.marionette.timeout = 5000
timeouts[self.marionette.TIMEOUT_SEARCH] = 5000
timeouts[self.marionette.TIMEOUT_SCRIPT] = 10000
timeouts[self.marionette.TIMEOUT_PAGE] = 10000
elif self.device.is_emulator:
self.marionette.timeout = 30000
timeouts[self.marionette.TIMEOUT_SEARCH] = 30000
timeouts[self.marionette.TIMEOUT_SCRIPT] = 60000
timeouts[self.marionette.TIMEOUT_PAGE] = 60000
else:
# else, it is a device, the type of which is difficult to detect
self.marionette.timeout = 10000
timeouts[self.marionette.TIMEOUT_SEARCH] = 10000
timeouts[self.marionette.TIMEOUT_SCRIPT] = 20000
timeouts[self.marionette.TIMEOUT_PAGE] = 20000
for k, v in timeouts.items():
self.marionette.timeouts(k, v)
else:
# if the user has passed in --timeout then we override everything
self.marionette.timeouts(self.marionette.TIMEOUT_SEARCH, self.marionette.timeout)
self.marionette.timeouts(self.marionette.TIMEOUT_SCRIPT, self.marionette.timeout)
self.marionette.timeouts(self.marionette.TIMEOUT_PAGE, self.marionette.timeout)
self.apps = GaiaApps(self.marionette)
self.data_layer = GaiaData(self.marionette, self.testvars)
self.accessibility = Accessibility(self.marionette)
self.cleanup_storage()
if self.restart:
self.cleanup_gaia(full_reset=False)
else:
self.cleanup_gaia(full_reset=True)
def cleanup_data(self):
self.device.file_manager.remove('/cache/*')
self.device.file_manager.remove('/data/b2g/mozilla')
self.device.file_manager.remove('/data/local/debug_info_trigger')
self.device.file_manager.remove('/data/local/indexedDB')
self.device.file_manager.remove('/data/local/OfflineCache')
self.device.file_manager.remove('/data/local/permissions.sqlite')
self.device.file_manager.remove('/data/local/storage/persistent')
# remove remembered networks
self.device.file_manager.remove('/data/misc/wifi/wpa_supplicant.conf')
def cleanup_storage(self):
"""Remove all files from the device's storage paths"""
storage_paths = [self.device.storage_path]
if self.device.is_android_build:
# TODO: Remove hard-coded paths once bug 1018079 is resolved
storage_paths.extend(['/mnt/sdcard',
'/mnt/extsdcard',
'/storage/sdcard0',
'/storage/sdcard1'])
for path in storage_paths:
if self.device.file_manager.dir_exists(path):
for item in self.device.file_manager.list_items(path):
self.device.file_manager.remove('/'.join([path, item]))
def cleanup_gaia(self, full_reset=True):
# restore settings from testvars
[self.data_layer.set_setting(name, value) for name, value in self.testvars.get('settings', {}).items()]
# restore prefs from testvars
for name, value in self.testvars.get('prefs', {}).items():
if type(value) is int:
self.data_layer.set_int_pref(name, value)
elif type(value) is bool:
self.data_layer.set_bool_pref(name, value)
else:
self.data_layer.set_char_pref(name, value)
# unlock
if self.data_layer.get_setting('lockscreen.enabled'):
self.device.unlock()
# kill the FTU and any open, user-killable apps
self.apps.kill_all()
if full_reset:
# disable passcode
self.data_layer.set_setting('lockscreen.passcode-lock.code', '1111')
self.data_layer.set_setting('lockscreen.passcode-lock.enabled', False)
# change language back to english
self.data_layer.set_setting("language.current", "en-US")
# reset keyboard to default values
self.data_layer.set_setting("keyboard.enabled-layouts",
"{'app://keyboard.gaiamobile.org/manifest.webapp': {'en': True, 'number': True}}")
# reset do not track
self.data_layer.set_setting('privacy.donottrackheader.value', '-1')
if self.data_layer.get_setting('airplaneMode.enabled'):
# enable the device radio, disable airplane mode
self.data_layer.set_setting('airplaneMode.enabled', False)
# Re-set edge gestures pref to False
self.data_layer.set_setting('edgesgesture.enabled', False)
# disable carrier data connection
if self.device.has_mobile_connection:
self.data_layer.disable_cell_data()
self.data_layer.disable_cell_roaming()
if self.device.has_wifi:
# Bug 908553 - B2G Emulator: support wifi emulation
if not self.device.is_emulator:
self.data_layer.enable_wifi()
self.data_layer.forget_all_networks()
self.data_layer.disable_wifi()
# remove data
self.data_layer.remove_all_contacts()
# reset to home screen
self.device.touch_home_button()
# disable sound completely
self.data_layer.set_volume(0)
# disable auto-correction of keyboard
self.data_layer.set_setting('keyboard.autocorrect', False)
def connect_to_network(self):
if not self.device.is_online:
try:
self.connect_to_local_area_network()
except:
self.marionette.log('Failed to connect to wifi, trying cell data instead.')
if self.device.has_mobile_connection:
self.data_layer.connect_to_cell_data()
else:
raise Exception('Unable to connect to network')
assert self.device.is_online
def connect_to_local_area_network(self):
if not self.device.is_online:
if self.testvars.get('wifi') and self.device.has_wifi:
self.data_layer.connect_to_wifi()
assert self.device.is_online
else:
raise Exception('Unable to connect to local area network')
def push_resource(self, filename, remote_path=None, count=1):
# push to the test storage space defined by device root
self.device.file_manager.push_file(
self.resource(filename), remote_path, count)
def resource(self, filename):
return os.path.abspath(os.path.join(os.path.dirname(__file__), 'resources', filename))
def wait_for_element_present(self, by, locator, timeout=None):
return Wait(self.marionette, timeout, ignored_exceptions=NoSuchElementException).until(
lambda m: m.find_element(by, locator))
def wait_for_element_not_present(self, by, locator, timeout=None):
self.marionette.set_search_timeout(0)
try:
return Wait(self.marionette, timeout).until(
lambda m: not m.find_element(by, locator))
except NoSuchElementException:
pass
self.marionette.set_search_timeout(self.marionette.timeout or 10000)
def wait_for_element_displayed(self, by, locator, timeout=None):
Wait(self.marionette, timeout, ignored_exceptions=[NoSuchElementException, StaleElementException]).until(
lambda m: m.find_element(by, locator).is_displayed())
def wait_for_element_not_displayed(self, by, locator, timeout=None):
self.marionette.set_search_timeout(0)
try:
Wait(self.marionette, timeout, ignored_exceptions=StaleElementException).until(
lambda m: not m.find_element(by, locator).is_displayed())
except NoSuchElementException:
pass
self.marionette.set_search_timeout(self.marionette.timeout or 10000)
def wait_for_condition(self, method, timeout=None, message=None):
Wait(self.marionette, timeout).until(method, message=message)
def is_element_present(self, by, locator):
self.marionette.set_search_timeout(0)
try:
self.marionette.find_element(by, locator)
return True
except NoSuchElementException:
return False
finally:
self.marionette.set_search_timeout(self.marionette.timeout or 10000)
def is_element_displayed(self, by, locator):
self.marionette.set_search_timeout(0)
try:
return self.marionette.find_element(by, locator).is_displayed()
except NoSuchElementException:
return False
finally:
self.marionette.set_search_timeout(self.marionette.timeout or 10000)
def tearDown(self):
if self.device.is_desktop_b2g and self.device.storage_path:
shutil.rmtree(self.device.storage_path, ignore_errors=True)
self.apps = None
self.data_layer = None
MarionetteTestCase.tearDown(self)
class GaiaEnduranceTestCase(GaiaTestCase, EnduranceTestCaseMixin, MemoryEnduranceTestCaseMixin):
def __init__(self, *args, **kwargs):
GaiaTestCase.__init__(self, *args, **kwargs)
EnduranceTestCaseMixin.__init__(self, *args, **kwargs)
MemoryEnduranceTestCaseMixin.__init__(self, *args, **kwargs)
kwargs.pop('iterations', None)
kwargs.pop('checkpoint_interval', None)
def close_app(self):
# Close the current app (self.app) by using the home button
self.device.touch_home_button()
# Bring up the cards view
_cards_view_locator = ('id', 'cards-view')
self.device.hold_home_button()
self.wait_for_element_displayed(*_cards_view_locator)
# Sleep a bit
time.sleep(5)
# Tap the close icon for the current app
locator_part_two = '#cards-view li.card[data-origin*="%s"] .close-card' % self.app_under_test.lower()
_close_button_locator = ('css selector', locator_part_two)
close_card_app_button = self.marionette.find_element(*_close_button_locator)
close_card_app_button.tap()