Skip to content

Add tests for background worker support. #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions assets/runtime/android/files/android-runtime-1488/app.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Add your native dependencies here:

// Uncomment to add recyclerview-v7 dependency
dependencies {
implementation "androidx.work:work-runtime:2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime:2.1.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0"
}

// If you want to add something to be applied before applying plugins' include.gradle files
// e.g. project.ext.googlePlayServicesVersion = "15.0.1"
// create a file named before-plugins.gradle in the current directory and place it there

android {
defaultConfig {
minSdkVersion 17
generatedDensities = []
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
27 changes: 27 additions & 0 deletions assets/runtime/android/files/android-runtime-1488/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
In NativeScript, the app.js file is the entry point to your application.
You can use this file to perform app-level initialization, but the primary
purpose of the file is to pass control to the app’s first module.
*/

const application = require("tns-core-modules/application");

var MyWorker = androidx.work.Worker.extend("com.tns.jobs.MyWorker",{
doWork:function(){
// Do your work here.

//
return androidx.work.ListenableWorker.Result.success();
},

onStopped:function() {
// Cleanup because you are being stopped.
console.log("onStopped from MyWorker !!!")
}
});
application.run({ moduleName: "app-root" });

/*
Do not place any code after the application has been started as it will not
be executed on iOS.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const Observable = require("tns-core-modules/data/observable").Observable;

function getMessage(counter) {
if (counter <= 0) {
return "Hoorraaay! You unlocked the NativeScript clicker achievement!";
} else {
return `${counter} taps left`;
}
}


function getJobConstrains() {
return (new androidx.work.Constraints.Builder())
.build();
}

function getWorkRequest() {
let constrains = getJobConstrains();
return (new androidx.work.OneTimeWorkRequest.Builder(java.lang.Class.forName("com.tns.jobs.MyWorker")))
.setConstraints(constrains)
.build()
}

function enqueue() {
let worker = getWorkRequest();

(androidx.work.WorkManager).getInstance()
.enqueueUniqueWork("jojoworker", androidx.work.ExistingWorkPolicy.REPLACE , worker);

let lifecycleowner = androidx.lifecycle.ProcessLifecycleOwner.get();

(androidx.work.WorkManager).getInstance().getWorkInfoByIdLiveData(worker.getId())
.observe(lifecycleowner, new androidx.lifecycle.Observer({
onChanged : function(workInfo) {

console.log("OnChange 1 : ", workInfo)

}
}))
}
function createViewModel() {
const viewModel = new Observable();
viewModel.counter = 42;
viewModel.message = getMessage(viewModel.counter);

viewModel.onTap = () => {
viewModel.counter--;
viewModel.set("message", getMessage(viewModel.counter));
enqueue();
};

return viewModel;
}

exports.createViewModel = createViewModel;
29 changes: 26 additions & 3 deletions tests/runtimes/android/android_service_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,32 @@ def test_203_test_foreground__intent_service_without_oncreate_method_is_working_
period=5)
assert test_result, "Intent service is not working! Missing Log! Logs: " + File.read(log.log_file)

def test_204_test_foreground__intent_service_without_oncreate_method_is_working_api28(self):
def test_204_test_background_worker_support(self):
"""
https://github.com/NativeScript/android-runtime/issues/1488
"""
Adb.clear_logcat(self.emulator.id)
File.copy(os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files',
'android-runtime-1488', 'app.js'),
os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'app.js'), True)
File.copy(os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files',
'android-runtime-1488', 'main-view-model.js'),
os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'main-view-model.js'), True)
File.copy(os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files',
'android-runtime-1488', 'app.gradle'),
os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'App_Resources', 'Android', 'app.gradle'), True)
log = Tns.run_android(APP_NAME, device=self.emulator.id, wait=False, verify=False)
strings = ['Successfully synced application', 'on device', self.emulator.id]
test_result = Wait.until(lambda: all(string in File.read(log.log_file) for string in strings), timeout=240,
period=5)
assert test_result, "App not build correctly ! Logs: " + File.read(log.log_file)
Device.click(self.emulator, text="TAP", case_sensitive=True)
time.sleep(5)
device_log = Adb.get_logcat(self.emulator.id)
error_message = "Background worker not working as expected. Logs: "+device_log
assert "WM-WorkerWrapper: Worker result SUCCESS for Work" in device_log, error_message

def test_205_test_foreground__intent_service_without_oncreate_method_is_working_api28(self):
"""
https://github.com/NativeScript/android-runtime/issues/1426
"""
Expand All @@ -204,5 +229,3 @@ def test_204_test_foreground__intent_service_without_oncreate_method_is_working_
test_result = Wait.until(lambda: "Intent Handled!" in File.read(log.log_file), timeout=30,
period=5)
assert test_result, "Intent service is not working! Missing Log! Logs: " + File.read(log.log_file)
DeviceManager.Emulator.stop()
self.emulator = DeviceManager.Emulator.ensure_available(Emulators.DEFAULT)