Skip to content

Commit 1853150

Browse files
authored
feat: use emulator snapshots (#33)
* feat: use emulator snapshots Feat: - Use emulator snapshot named `clean_boot` (if availalbe) Fix: - File.repalce() will now fail if old tests is not available in file Added Tests: - Error activity tests * fix: lint
1 parent e22e6c0 commit 1853150

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

core/utils/device/device_manager.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from core.utils.device.device import Device
88
from core.utils.device.idevice import IDevice
99
from core.utils.device.simctl import Simctl
10+
from core.utils.file_utils import Folder
1011
from core.utils.process import Process
1112
from core.utils.run import run
1213

@@ -51,12 +52,20 @@ def stop():
5152
Process.kill('qemu-system-i38')
5253

5354
@staticmethod
54-
def start(emulator, wipe_data=True):
55+
def start(emulator):
56+
# Define emulator start options and command
5557
emulator_path = os.path.join(ANDROID_HOME, 'emulator', 'emulator')
56-
base_options = '-no-snapshot-save -no-boot-anim -no-audio'
57-
options = '-port {0} {1}'.format(emulator.port, base_options)
58-
if wipe_data:
59-
options = '-port {0} -wipe-data {1}'.format(emulator.port, base_options)
58+
options = '-port {0} -wipe-data -no-snapshot-save -no-boot-anim -no-audio'.format(emulator.port)
59+
60+
# Check if clean snapshot is available and use it
61+
snapshot_name = 'clean_boot'
62+
home = os.path.expanduser("~")
63+
snapshot = os.path.join(home, '.android', 'avd', '{0}.avd'.format(emulator.avd), 'snapshots', snapshot_name)
64+
if Folder.exists(snapshot):
65+
Log.info('{0} has clean boot snapshot! Will user it.'.format(emulator.avd))
66+
options = '-port {0} -no-snapshot-save -no-boot-anim -no-audio -snapshot {1}'.format(emulator.port,
67+
snapshot_name)
68+
6069
command = '{0} @{1} {2}'.format(emulator_path, emulator.avd, options)
6170
Log.info('Booting {0} with cmd:'.format(emulator.avd))
6271
Log.info(command)

core/utils/file_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
"""
44
import errno
55
import os
6-
import stat
7-
86
import shutil
7+
import stat
98

109
from core.log.log import Log
1110
from core.settings import Settings
@@ -122,6 +121,7 @@ def append(path, text):
122121
@staticmethod
123122
def replace(path, old_string, new_string):
124123
content = File.read(path=path)
124+
assert old_string in content, 'Can not find "{0}" in {1}'.format(old_string, path)
125125
new_content = content.replace(old_string, new_string)
126126
File.write(path=path, text=new_content)
127127
Log.info("")

core_tests/device/adb_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AdbTests(TnsTest):
1515
def setUpClass(cls):
1616
TnsTest.setUpClass()
1717
DeviceManager.Emulator.stop()
18-
cls.emu = DeviceManager.Emulator.start(Settings.Emulators.DEFAULT, wipe_data=True)
18+
cls.emu = DeviceManager.Emulator.start(Settings.Emulators.DEFAULT)
1919

2020
def setUp(self):
2121
TnsTest.setUp(self)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Test for android error activity.
3+
4+
Verify that:
5+
- If error happens error activity is displayed (debug mode).
6+
- Stack trace of the error is printed in console.
7+
- No error activity in release builds.
8+
"""
9+
import os
10+
11+
from core.base_test.tns_test import TnsTest
12+
from core.settings import Settings
13+
from core.utils.device.device_manager import DeviceManager
14+
from data.changes import Sync, ChangeSet
15+
from data.templates import Template
16+
from products.nativescript.tns import Tns
17+
18+
APP_NAME = Settings.AppName.DEFAULT
19+
20+
21+
class AndroidErrorActivityTests(TnsTest):
22+
@classmethod
23+
def setUpClass(cls):
24+
TnsTest.setUpClass()
25+
Tns.create(app_name=APP_NAME, template=Template.HELLO_WORLD_JS.local_package, update=True)
26+
Tns.platform_add_android(app_name=APP_NAME, framework_path=Settings.Android.FRAMEWORK_PATH)
27+
28+
# Break the app to test error activity
29+
change = ChangeSet(file_path=os.path.join(Settings.TEST_RUN_HOME, APP_NAME, 'app', 'app.js'),
30+
old_value='application.run({ moduleName: "app-root" });',
31+
new_value='throw new Error("Kill the app!");')
32+
Sync.replace(app_name=APP_NAME, change_set=change)
33+
34+
# Start emulator
35+
cls.emu = DeviceManager.Emulator.ensure_available(Settings.Emulators.DEFAULT)
36+
37+
def test_200_error_activity_shown_on_error(self):
38+
Tns.run_android(app_name=APP_NAME, emulator=True, wait=False)
39+
self.emu.wait_for_text('Exception', timeout=180, retry_delay=10)
40+
self.emu.wait_for_text('Logcat')
41+
self.emu.wait_for_text('Error: Kill the app!')
42+
# TODO: Verify stack trace of the error is printed in console (waiting nativescript-tooling-qa/pull/26).
43+
44+
def test_400_no_error_activity_in_release_builds(self):
45+
Tns.run_android(app_name=APP_NAME, release=True, emulator=True, wait=False)
46+
self.emu.wait_for_text('Unfortunately', timeout=180, retry_delay=10)
47+
self.emu.is_text_visible('Exception')

tests/runtimes/android/todo.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# TODOs:
2+
- Write tests for android runtime

tests/runtimes/ios/todo.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# TODOs:
2+
- Write tests for ios runtime

0 commit comments

Comments
 (0)