Skip to content

Commit 48f2b19

Browse files
author
Vasil Hristov
authored
Add tests for intent service for android (#282)
Add tests for intent service for android. NativeScript/android#1426
1 parent 92897a0 commit 48f2b19

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="__PACKAGE__"
4+
android:versionCode="1"
5+
android:versionName="1.0">
6+
7+
<supports-screens
8+
android:smallScreens="true"
9+
android:normalScreens="true"
10+
android:largeScreens="true"
11+
android:xlargeScreens="true"/>
12+
13+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
14+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
15+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
16+
<uses-permission android:name="android.permission.INTERNET"/>
17+
<uses-permission android:name="android.permission.WAKE_LOCK"/>
18+
19+
<application
20+
android:name="com.tns.NativeScriptApplication"
21+
android:allowBackup="true"
22+
android:icon="@drawable/icon"
23+
android:label="@string/app_name"
24+
android:theme="@style/AppTheme">
25+
<service android:name="com.nativescript.MyIntentServiceAndroidX"
26+
android:permission="android.permission.BIND_JOB_SERVICE"
27+
android:exported="false" >
28+
</service>
29+
<activity
30+
android:name="com.tns.NativeScriptActivity"
31+
android:label="@string/title_activity_kimera"
32+
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|locale|uiMode"
33+
android:theme="@style/LaunchScreenTheme">
34+
35+
<meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />
36+
37+
<intent-filter>
38+
<action android:name="android.intent.action.MAIN" />
39+
<category android:name="android.intent.category.LAUNCHER" />
40+
</intent-filter>
41+
</activity>
42+
<activity android:name="com.tns.ErrorReportActivity"/>
43+
</application>
44+
</manifest>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
In NativeScript, the app.js file is the entry point to your application.
3+
You can use this file to perform app-level initialization, but the primary
4+
purpose of the file is to pass control to the app’s first module.
5+
*/
6+
const app = require("tns-core-modules/application");
7+
const application = require("tns-core-modules/application");
8+
androidx.core.app.JobIntentService.extend("com.nativescript.MyIntentServiceAndroidX", {
9+
10+
onHandleWork: function () {
11+
console.log("Intent Handled!");
12+
}
13+
});
14+
application.run({ moduleName: "app-root" });
15+
16+
/*
17+
Do not place any code after the application has been started as it will not
18+
be executed on iOS.
19+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const Observable = require("tns-core-modules/data/observable").Observable;
2+
const platformModule = require("tns-core-modules/platform");
3+
const app = require("tns-core-modules/application");
4+
5+
function getMessage(counter) {
6+
if (counter <= 0) {
7+
return "Hoorraaay! You unlocked the NativeScript clicker achievement!";
8+
} else {
9+
return `${counter} taps left`;
10+
}
11+
}
12+
13+
function createViewModel() {
14+
15+
const viewModel = new Observable();
16+
viewModel.counter = 42;
17+
viewModel.message = getMessage(viewModel.counter);
18+
19+
viewModel.onTap = () => {
20+
viewModel.counter--;
21+
viewModel.set("message", getMessage(viewModel.counter));
22+
if (platformModule.device.sdkVersion >= "26") {
23+
var utilsAd = require("utils/utils").ad;
24+
var context = utilsAd.getApplicationContext();
25+
let intent = new android.content.Intent(context, com.nativescript.MyIntentServiceAndroidX.class);
26+
console.log(context);
27+
com.nativescript.MyIntentServiceAndroidX.enqueueWork(context, com.nativescript.MyIntentServiceAndroidX.class, 1000, intent)
28+
} else {
29+
let intent = new android.content.Intent(app.android.context, com.nativescript.MyIntentServiceAndroidX.class);
30+
app.android.context.startService(intent);
31+
32+
}
33+
};
34+
35+
return viewModel;
36+
}
37+
38+
exports.createViewModel = createViewModel;

tests/runtimes/android/android_service_tests.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,61 @@ def test_202_test_foreground_service_without_oncreate_method_is_working(self):
158158
assert service_name not in service_info, "{0} service found! Logs: {1}".format(service_name, service_info)
159159
assert pid not in service_info, "{0} service with id {1} found! Logs: {2}".format(service_name, pid,
160160
service_info)
161+
162+
def test_203_test_foreground__intent_service_without_oncreate_method_is_working_api23(self):
163+
"""
164+
https://github.com/NativeScript/android-runtime/issues/1426
165+
"""
166+
File.copy(os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files',
167+
'android-runtime-1426', 'AndroidManifest.xml'),
168+
os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'App_Resources', 'Android', 'src', 'main',
169+
'AndroidManifest.xml'), True)
170+
File.copy(os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files',
171+
'android-runtime-1426', 'app.js'),
172+
os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'app.js'), True)
173+
File.copy(os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files',
174+
'android-runtime-1426', 'main-view-model.js'),
175+
os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'main-view-model.js'), True)
176+
log = Tns.run_android(APP_NAME, device=self.emulator.id, wait=False, verify=False)
177+
strings = ['Successfully synced application', 'on device', self.emulator.id]
178+
test_result = Wait.until(lambda: all(string in File.read(log.log_file) for string in strings), timeout=240,
179+
period=5)
180+
assert test_result, "App not build correctly ! Logs: " + File.read(log.log_file)
181+
182+
Device.wait_for_text(self.emulator, text='TAP', timeout=20)
183+
Device.click(self.emulator, text="TAP", case_sensitive=True)
184+
time.sleep(5)
185+
test_result = Wait.until(lambda: "Intent Handled!" in File.read(log.log_file), timeout=30,
186+
period=5)
187+
assert test_result, "Intent service is not working! Missing Log! Logs: " + File.read(log.log_file)
188+
189+
def test_204_test_foreground__intent_service_without_oncreate_method_is_working_api28(self):
190+
"""
191+
https://github.com/NativeScript/android-runtime/issues/1426
192+
"""
193+
DeviceManager.Emulator.stop()
194+
self.emulator = DeviceManager.Emulator.ensure_available(Emulators.EMU_API_28)
195+
File.copy(os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files',
196+
'android-runtime-1426', 'AndroidManifest.xml'),
197+
os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'App_Resources', 'Android', 'src', 'main',
198+
'AndroidManifest.xml'), True)
199+
File.copy(os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files',
200+
'android-runtime-1426', 'app.js'),
201+
os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'app.js'), True)
202+
File.copy(os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files',
203+
'android-runtime-1426', 'main-view-model.js'),
204+
os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'main-view-model.js'), True)
205+
log = Tns.run_android(APP_NAME, device=self.emulator.id, wait=False, verify=False)
206+
strings = ['Successfully synced application', 'on device', self.emulator.id]
207+
test_result = Wait.until(lambda: all(string in File.read(log.log_file) for string in strings), timeout=240,
208+
period=5)
209+
assert test_result, "App not build correctly ! Logs: " + File.read(log.log_file)
210+
211+
Device.wait_for_text(self.emulator, text='TAP', timeout=20)
212+
Device.click(self.emulator, text="TAP", case_sensitive=True)
213+
time.sleep(5)
214+
test_result = Wait.until(lambda: "Intent Handled!" in File.read(log.log_file), timeout=30,
215+
period=5)
216+
assert test_result, "Intent service is not working! Missing Log! Logs: " + File.read(log.log_file)
217+
DeviceManager.Emulator.stop()
218+
self.emulator = DeviceManager.Emulator.ensure_available(Emulators.DEFAULT)

0 commit comments

Comments
 (0)