From 303b609573b0e94797096638b79b63fc9e295d29 Mon Sep 17 00:00:00 2001 From: Nedyalko Nikolov Date: Mon, 7 Nov 2016 10:29:23 +0200 Subject: [PATCH 1/2] Adding request for write storage permissions. --- .gitignore | 1 + tests/app/app/mainPage.ts | 32 +++++++++++++++++-- tests/app/testRunner.ts | 4 ++- tests/hooks/after-install/after-install.js | 19 +++++++++++ tns-core-modules/application/application.d.ts | 6 ++-- 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 tests/hooks/after-install/after-install.js diff --git a/.gitignore b/.gitignore index 5124b4239..c688bf537 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ dist/ package/ *.js +!tests/hooks/**/*.* !gruntfile.js !js-libs/**/*.* !css/**/*.* diff --git a/tests/app/app/mainPage.ts b/tests/app/app/mainPage.ts index 3f6394614..f6571c320 100644 --- a/tests/app/app/mainPage.ts +++ b/tests/app/app/mainPage.ts @@ -2,6 +2,8 @@ import * as trace from "trace"; import tests = require("../testRunner"); import {Label} from "ui/label"; +import * as application from "application"; +import * as platform from "platform"; trace.enable(); trace.addCategories(trace.categories.Test + "," + trace.categories.Error); @@ -21,14 +23,38 @@ page.id = "mainPage"; page.on(Page.navigatedToEvent, onNavigatedTo); +function runTests() { + setTimeout(function () { + tests.runAll(); + }, 10); +} + function onNavigatedTo(args) { let label = new Label(); label.text = "Running non-UI tests..."; page.content = label; args.object.off(Page.navigatedToEvent, onNavigatedTo); - setTimeout(function () { - tests.runAll(); - }, 10); + + if (parseInt(platform.device.sdkVersion) >= 23) { + let handler = (args: application.AndroidActivityRequestPermissionsEventData) => { + application.android.off(application.AndroidApplication.activityRequestPermissionsEvent, handler); + if (args.requestCode === 1234 && args.grantResults.length > 0 && args.grantResults[0] === android.content.pm.PackageManager.PERMISSION_GRANTED) { + runTests(); + } else { + trace.write("Permission for write to external storage not granted!", trace.categories.Error, trace.messageType.error); + } + }; + + application.android.on(application.AndroidApplication.activityRequestPermissionsEvent, handler); + + if ((android.support.v4.content.ContextCompat).checkSelfPermission(application.android.currentContext, (android).Manifest.permission.WRITE_EXTERNAL_STORAGE) !== android.content.pm.PackageManager.PERMISSION_GRANTED) { + (android.support.v4.app.ActivityCompat).requestPermissions(application.android.currentContext, [(android).Manifest.permission.WRITE_EXTERNAL_STORAGE], 1234); + } else { + runTests(); + } + } else { + runTests(); + } } export function createPage() { diff --git a/tests/app/testRunner.ts b/tests/app/testRunner.ts index 80127e8e1..d0414cdb2 100644 --- a/tests/app/testRunner.ts +++ b/tests/app/testRunner.ts @@ -176,7 +176,9 @@ function printRunTestStats() { testFileContent.push(""); testFileContent.push(""); - let testFilePath = fs.path.join(fs.knownFolders.documents().path, "test-results.xml"); + let testFilePath = fs.path.join(android.os.Environment.getExternalStorageDirectory().getAbsolutePath(), "Documents", "test-results.xml"); + console.log("testFilePath =====>>>>>>> " + testFilePath); + let testFile = fs.File.fromPath(testFilePath); testFile.writeTextSync(testFileContent.join("")); diff --git a/tests/hooks/after-install/after-install.js b/tests/hooks/after-install/after-install.js new file mode 100644 index 000000000..d271d8799 --- /dev/null +++ b/tests/hooks/after-install/after-install.js @@ -0,0 +1,19 @@ +var childProcess = require("child_process"); + +module.exports = function(logger, platformsData, projectData, hookArgs) { + if (/apk$/.test(hookArgs.packageFilePath)) { + var writeCommand = "adb shell pm grant " + projectData.projectId + " android.permission.WRITE_EXTERNAL_STORAGE"; + var readCommand = "adb shell pm grant " + projectData.projectId + " android.permission.READ_EXTERNAL_STORAGE"; + var callback = function(error, stdout, stderr) { + if (error) { + console.log(error); + } + }; + childProcess.exec(writeCommand, { + cwd: projectData.projectDir + }, callback); + childProcess.exec(readCommand, { + cwd: projectData.projectDir + }, callback); + } +} diff --git a/tns-core-modules/application/application.d.ts b/tns-core-modules/application/application.d.ts index 783aba00b..83cd2d3dd 100644 --- a/tns-core-modules/application/application.d.ts +++ b/tns-core-modules/application/application.d.ts @@ -300,12 +300,12 @@ declare module "application" { /** * The Permissions */ - permissions: Array, + permissions: Array, /** * The Granted. */ - grantResults: Array + grantResults: Array } /** @@ -480,7 +480,7 @@ declare module "application" { /** * This event is raised on the back button is pressed in an android application. */ - on(event: "activityRequestPermissions", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any); + on(event: "activityRequestPermissions", callback: (args: AndroidActivityRequestPermissionsEventData) => void, thisArg?: any); /** * String value used when hooking to activityCreated event. From a7fcc5864fa0a2f319a2482041e01cccdea7072b Mon Sep 17 00:00:00 2001 From: vchimev Date: Tue, 22 Nov 2016 11:42:15 +0200 Subject: [PATCH 2/2] Update Travis CI --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d9b9220ed..abfebd78c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,7 +45,7 @@ script: grunt buildOnlyTestsApp --verbose --platform=Android --modulesPath=./bin/dist/$PACKAGE_NAME-$FULL_PACKAGE_VERSION.tgz --runtimeVersion=$RUNTIMEVERSION --emuPId=.*emulator.* --avd=$AVD_NAME --showEmu=false > /dev/null && grunt runOnlyTestsApp --verbose --platform=Android --modulesPath=./bin/dist/$PACKAGE_NAME-$FULL_PACKAGE_VERSION.tgz --emuPId=.*emulator.* --avd=$AVD_NAME --showEmu=false - node ./build/travis-scripts/check-testrun-broken.js - - adb pull /data/data/org.nativescript.TestsApp/files/test-results.xml && + - adb pull /storage/sdcard/Documents/test-results.xml && mv test-results.xml ~/test-run-results$PACKAGE_VERSION.xml before_deploy: - mv bin/dist/$PACKAGE_NAME-$FULL_PACKAGE_VERSION.tgz ../.deploymentpackage