From 0e19f88a04030ab206efefef7ede5c0a6f91e1d2 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 20 Jan 2015 13:58:58 -0500 Subject: [PATCH] Make unit tests work with Gradle Had to split the test app from the tests, since that's how gradle forces you to do it. --- .gitignore | 34 ++++----- test/README.md | 55 ++++++++------ .../test/junit/BackButtonMultiPageTest.java | 0 .../test/junit/CordovaActivityTest.java | 0 .../test/junit/CordovaResourceApiTest.java | 0 .../cordova/test/junit/CordovaTest.java | 0 .../cordova/test/junit/ErrorUrlTest.java | 0 .../apache/cordova/test/junit/FixWebView.java | 0 .../cordova/test/junit/HtmlNotFoundTest.java | 0 .../apache/cordova/test/junit/IFrameTest.java | 0 .../test/junit/IntentUriOverrideTest.java | 0 .../cordova/test/junit/LifecycleTest.java | 0 .../apache/cordova/test/junit/MenuTest.java | 0 .../cordova/test/junit/PluginManagerTest.java | 0 .../cordova/test/junit/SplashscreenTest.java | 0 .../apache/cordova/test/junit/XhrTest.java | 0 .../org/apache/cordova/test/util/Purity.java | 0 test/build.gradle | 75 +++++++++++++++++++ test/project.properties | 2 +- test/settings.gradle | 4 + 20 files changed, 131 insertions(+), 39 deletions(-) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/CordovaActivityTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/CordovaResourceApiTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/CordovaTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/ErrorUrlTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/FixWebView.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/HtmlNotFoundTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/IFrameTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/IntentUriOverrideTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/LifecycleTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/MenuTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/PluginManagerTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/SplashscreenTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/junit/XhrTest.java (100%) rename test/{ => androidTest}/src/org/apache/cordova/test/util/Purity.java (100%) create mode 100644 test/build.gradle create mode 100644 test/settings.gradle diff --git a/.gitignore b/.gitignore index 55991577..0b3f8226 100644 --- a/.gitignore +++ b/.gitignore @@ -2,28 +2,28 @@ default.properties gen assets/www/cordova.js -framework/assets/www/.tmp* local.properties -framework/lib proguard.cfg proguard.cfg proguard-project.txt -framework/bin -framework/test/org/apache/cordova/*.class -framework/assets/www/.DS_Store -framework/assets/www/cordova-*.js -framework/assets/www/phonegap-*.js -framework/libs -framework/javadoc-public -framework/javadoc-private -test/libs +/framework/lib +/framework/build +/framework/bin +/framework/assets/www/.DS_Store +/framework/assets/www/cordova-*.js +/framework/assets/www/phonegap-*.js +/framework/libs +/framework/javadoc-public +/framework/javadoc-private +/test/libs example -./test -test/bin -test/assets/www/.tmp* -test/assets/www/cordova.js -test/cordova/plugins/org.apache.cordova.device/www/device.js -test/cordova/plugins/org.apache.cordova.device/src/android/Device.java +/test/bin +/test/assets/www/.tmp* +/test/assets/www/cordova.js +/test/gradle +/test/gradlew +/test/build +.gradle tmp/** .metadata tmp/**/* diff --git a/test/README.md b/test/README.md index 6f636742..1ef3950d 100755 --- a/test/README.md +++ b/test/README.md @@ -7,9 +7,9 @@ # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -18,34 +18,47 @@ # under the License. # --> -# Android Native Tests # +# Android Native Tests These tests are designed to verify Android native features and other Android specific features. +They currently are in disrepair, and don't pass / work on KitKat+ :(. -## Initial Setup ## +## Initial Setup -There really isn't any manual setup to do. The ant script takes care of that. -You don't even need to compile cordova-x.y.z.jar or copy it, because -project.properties has a library reference to ../framework. However, Robotium -has to be installed for the onScrollChanged tests to work correctly. It can be +### Setting env vars + +Run: + + ../bin/check_reqs + +Use the output to set your `ANDROID_HOME` and `JAVA_HOME` environment variables. + +### Adding `gradlew` + +Copy it from a freshly created project: + + ../bin/create foo + (cd foo && cordova/build --gradle; cp -r gradlew gradle ..) + rm -r foo + +### Robotium + +Robotium has to be installed for the onScrollChanged tests to work correctly. It can be found at https://code.google.com/p/robotium/ and the jar should be put in the -'libs' directory'. +'androidTests/libs' directory'. -To run manually from command line: +## Running -0. Build by entering `ant debug install` -0. Run tests by clicking on "CordovaNativeTests" app icon on the device +To run manual tests: -To run from Eclipse: + ./gradlew installDebug -0. Import Android project into Eclipse -0. Ensure Project properties "Java Build Path" includes the lib/cordova-x.y.z.jar -0. Create run configuration if not already created -0. Run As -> Android JUnit Test +To run unit tests: -## Automatic Runs ## + ./gradlew connectedAndroidTest -Once you have installed the test, you can launch and run the tests -automatically with the below command: +## Android Studio - adb shell am instrument -w org.apache.cordova.test/android.test.InstrumentationTestRunner +1. Use "Non-Android Studio Project" to import the `test` directory. +2. Right click on the `junit` package in the left-side nav +3. Select "Debug"`->`_The one with the Android icon_ diff --git a/test/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java b/test/androidTest/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java diff --git a/test/src/org/apache/cordova/test/junit/CordovaActivityTest.java b/test/androidTest/src/org/apache/cordova/test/junit/CordovaActivityTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/CordovaActivityTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/CordovaActivityTest.java diff --git a/test/src/org/apache/cordova/test/junit/CordovaResourceApiTest.java b/test/androidTest/src/org/apache/cordova/test/junit/CordovaResourceApiTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/CordovaResourceApiTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/CordovaResourceApiTest.java diff --git a/test/src/org/apache/cordova/test/junit/CordovaTest.java b/test/androidTest/src/org/apache/cordova/test/junit/CordovaTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/CordovaTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/CordovaTest.java diff --git a/test/src/org/apache/cordova/test/junit/ErrorUrlTest.java b/test/androidTest/src/org/apache/cordova/test/junit/ErrorUrlTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/ErrorUrlTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/ErrorUrlTest.java diff --git a/test/src/org/apache/cordova/test/junit/FixWebView.java b/test/androidTest/src/org/apache/cordova/test/junit/FixWebView.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/FixWebView.java rename to test/androidTest/src/org/apache/cordova/test/junit/FixWebView.java diff --git a/test/src/org/apache/cordova/test/junit/HtmlNotFoundTest.java b/test/androidTest/src/org/apache/cordova/test/junit/HtmlNotFoundTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/HtmlNotFoundTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/HtmlNotFoundTest.java diff --git a/test/src/org/apache/cordova/test/junit/IFrameTest.java b/test/androidTest/src/org/apache/cordova/test/junit/IFrameTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/IFrameTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/IFrameTest.java diff --git a/test/src/org/apache/cordova/test/junit/IntentUriOverrideTest.java b/test/androidTest/src/org/apache/cordova/test/junit/IntentUriOverrideTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/IntentUriOverrideTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/IntentUriOverrideTest.java diff --git a/test/src/org/apache/cordova/test/junit/LifecycleTest.java b/test/androidTest/src/org/apache/cordova/test/junit/LifecycleTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/LifecycleTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/LifecycleTest.java diff --git a/test/src/org/apache/cordova/test/junit/MenuTest.java b/test/androidTest/src/org/apache/cordova/test/junit/MenuTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/MenuTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/MenuTest.java diff --git a/test/src/org/apache/cordova/test/junit/PluginManagerTest.java b/test/androidTest/src/org/apache/cordova/test/junit/PluginManagerTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/PluginManagerTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/PluginManagerTest.java diff --git a/test/src/org/apache/cordova/test/junit/SplashscreenTest.java b/test/androidTest/src/org/apache/cordova/test/junit/SplashscreenTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/SplashscreenTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/SplashscreenTest.java diff --git a/test/src/org/apache/cordova/test/junit/XhrTest.java b/test/androidTest/src/org/apache/cordova/test/junit/XhrTest.java similarity index 100% rename from test/src/org/apache/cordova/test/junit/XhrTest.java rename to test/androidTest/src/org/apache/cordova/test/junit/XhrTest.java diff --git a/test/src/org/apache/cordova/test/util/Purity.java b/test/androidTest/src/org/apache/cordova/test/util/Purity.java similarity index 100% rename from test/src/org/apache/cordova/test/util/Purity.java rename to test/androidTest/src/org/apache/cordova/test/util/Purity.java diff --git a/test/build.gradle b/test/build.gradle new file mode 100644 index 00000000..c7731748 --- /dev/null +++ b/test/build.gradle @@ -0,0 +1,75 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +// GENERATED FILE! DO NOT EDIT! + +apply plugin: 'android' + +buildscript { + repositories { + mavenCentral() + } + + dependencies { + classpath 'com.android.tools.build:gradle:1.0.0+' + } +} + +ext { + apply from: '../framework/cordova.gradle' + cdvCompileSdkVersion = privateHelpers.getProjectTarget() + cdvBuildToolsVersion = privateHelpers.findLatestInstalledBuildTools() +} + +android { + sourceSets { + main { + manifest.srcFile 'AndroidManifest.xml' + java.srcDirs = ['src'] + resources.srcDirs = ['src'] + res.srcDirs = ['res'] + assets.srcDirs = ['assets'] + } + androidTest { + java.srcDirs = ['androidTest/src'] + resources.srcDirs = ['androidTest/src'] + res.srcDirs = [] + assets.srcDirs = [] + } + } + + defaultConfig { + testInstrumentationRunner "android.test.InstrumentationTestRunner" + } + + compileSdkVersion cdvCompileSdkVersion + buildToolsVersion cdvBuildToolsVersion + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_6 + targetCompatibility JavaVersion.VERSION_1_6 + } +} + +dependencies { + debugCompile project(path: ":CordovaLib", configuration: "debug") + releaseCompile project(path: ":CordovaLib", configuration: "release") + androidTestCompile fileTree(dir: 'androidTest/libs', include: '*.jar') +} + diff --git a/test/project.properties b/test/project.properties index e4145b05..fcee01ff 100644 --- a/test/project.properties +++ b/test/project.properties @@ -8,5 +8,5 @@ # project structure. # Project target. -target=android-19 +target=android-21 android.library.reference.1=../framework diff --git a/test/settings.gradle b/test/settings.gradle new file mode 100644 index 00000000..56959f70 --- /dev/null +++ b/test/settings.gradle @@ -0,0 +1,4 @@ +include ":" +include ":CordovaLib" +project(':CordovaLib').projectDir = new File('../framework') +