Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

First install your application on your mobile, then launch the following commands to grant permissions:

[source,bash]
----
adb shell pm grant com.example.myapp android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant com.example.myapp android.permission.WRITE_EXTERNAL_STORAGE
----

Code Block
languagebash
adb shell pm grant com.example.myapp android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant com.example.myapp android.permission.WRITE_EXTERNAL_STORAGE

...

==== Generic Android Integration

===== API[source,java]
----

Code Block
languagejava
setUpMeasure(Context context, String[] packageName, int[] lstPid, String Appname, String Version, String URL, String privateToken, int timeout, int interval, boolean online)


----Sets up the measure with all the needed data.

- Context: We pass the context of the application.
- PackageName: Package name of the application to survey. For example, "com.android.chrome". If it is blank (""), the probe does not survey any particular process.
- lstPid: (Optional) In case of system application, the probe can't get the pid by package name so you need to precise the PID list matching the package name list.
- Appname: GREENSPECTOR's application name
- Version: Current application's version
- URL: GREENSPECTOR's server URL. For saas : https://api.greenspector.com/api. For on premises server, replace api.greenspector.com. by your URL
- privateToken: User's private token
- timeout: Test time in seconds. The measure stops when the measure time reaches timeout.
- interval: Measure interval in milliseconds. The interval must be greater than 100 ms or be 0 for not setting the interval.
- online: set false for recording the measures in phones, set true for sending it immediately to server.[source,java]
----

Code Block
languagejava
startMeasure()

...


Starts the measure.[source,java]
----

Code Block
languagejava
stopMeasure(String testName)


----Stops Stops the measure and write the results on the phone or send it to the GREENSPECTOR Server.

- testName: Name of the current test.[source,java]
----

Code Block
languagejava
setCoverageFile(String fileName) (Optional)


----Sets the path to coverage file.

If never set it, the default value is: /mnt/sdcard/greenspector/coverages/[Application Package]/[Test Name]-coverage.ec[source,java]
----

Code Block
languagejava
setMetadataFile(String fileName) (Optional)

...


Sets the path to metadata file.

If never set it, the default value is: /mnt/sdcard/greenspector/coverages/[Application Package]/coverage.em[source,java]
----

Code Block
languagejava
setProxy(String url, int port) (Optional)

...


Sets the proxy URL and port

If never set it, no proxy will be used.[source,java]
----

Code Block
languagejava
setProxy(String url, int port, String login, String password) (Optional)

...


Sets the proxy URL, port and authentication informations

If never set it, no proxy will be used.[source,java]
----

Code Block
languagejava
int cpt_test


----Sends the number of the completed tests to the probe. Enables the number of tests identification and the calculation of standardized metrics.

===== Integration with your program

Usage example[source,java]
----
package :

Code Block
languagejava
package com.greenspector.sample.BasicExample;

...



import android.content.Context;

...


import android.test.InstrumentationTestCase;

...



import android.support.test.InstrumentationRegistry;

...



import com.greenspector.probe.android.interfaces.Api;

...



public class GreenspectorBasicExample extends InstrumentationTestCase {

...



// Package to measure

...


private static final String BASIC_SAMPLE_PACKAGE = "com.android.chrome";

...



/** Probe configuration **/

...


private static final String APPLICATION = "TestApplication";

...


private static final String VERSION = "1.0.2";

...


private static final String URL = "https://my-instance.greenspector.com/api";

...


private static final String PRIVATETOKEN = "123456789AZERTY";

...


private static final int TIMEOUT = 180;

...


private static final int INTERVAL = 200;

...


private static final boolean ONLINE = true;

...



private Api gsptpp;

...



public void testBasicExample() {

...


gsptpp = new Api();

...


gsptpp.setUpMeasure(getInstrumentation().getTargetContext(), new String[]{BASIC_SAMPLE_PACKAGE}, null, APPLICATION, VERSION, URL, PRIVATETOKEN, TIMEOUT, INTERVAL, ONLINE);

...


gsptpp.startMeasure();

...



try {

...


Thread.sleep(10000);

...


} catch (InterruptedException e) {

...


gsptpp.stopMeasure("test_Idle_Failed");

...


return;

...


}

...



gsptpp.stopMeasure("test_Idle");

...


}

...


}

----

===== Usage with Greenspector Testrunnner

...

Else if you want to use the probe in offline only, you may not use this script too as the file will not be read and sent.[source,xml]
----

Code Block
languagexml
<project name="custom_rules">

...


<target name="-post-compile">

...


<if condition="${emma.enabled}">

...


<then>

...


<echo level="info">Uploading coverage.em file into sdcard ...</echo>

...


<if>

...


<condition>

...


<resourceexists>

...


<file file="${emma.coverage.absolute.file}"/>

...


</resourceexists>

...


</condition>

...


<then>

...


<exec executable="${adb}" failonerror="true">

...


<arg line="${adb.device.arg}" />

...


<arg value="push" />

...


<arg value="${emma.coverage.absolute.file}" />

...


<arg value="/sdcard/greenspector/coverages/${project.app.package}/coverage.em" />

...


</exec>

...


</then>

...


<else>

...


<fail message="File ${emma.coverage.absolute.file} does not exist." />

...


</else>

...


</if>

...


</then>

...


</if>

...


</target>

...


</project>

----.Install Sample Application

...

NB: The coverage.ec file is located where the command -e coveraFile sets it (Here: /sdcard/greenspector/coverages/{package name}/{test-name}-coverage.ec).

.Files[source,java]
.

Code Block
languagejava
titlegreenspector-coverage.gradle

...

def instrumented = false

...


gradle.taskGraph.afterTask { Task task ->

...


if(task.name.startsWith('compile') && task.name.endsWith('Sources') && !instrumented) {

...


ant.importBuild "greenspector-coverage-ant.xml"

...


tasks.instrument.execute()

...


instrumented = true

...


}
}

...


Code Block
languagexml
titlegreenspector-coverage-ant.xml

...

<?xml version="1.0" encoding="UTF-8"?>

...


<project>

...


<property file="../local.properties" />

...


<!-- Emma configuration -->

...


<property name="emma.dir" value="${sdk.dir}/tools/lib" />

...


<path id="emma.lib">

...


<pathelement location="${emma.dir}/emma.jar" />

...


<pathelement location="${emma.dir}/emma_ant.jar" />

...


</path>

...


<taskdef resource="emma_ant.properties" classpathref="emma.lib" />

...


<!-- End of emma configuration -->

...


<!-- Output directories -->

...


<property name="out.dir" value="build" />

...


<property name="out.classes.absolute.dir" location="${out.dir}/intermediates/classes" />

...


<condition property="verbosity" value="verbose" else="quiet">

...


<istrue value="${verbose}" />

...


</condition>

...


<!-- Instruments this project's .class files. -->

...


<target name="instrument">

...


<echo message="Instrumenting classes from ${out.dir}/classes..."></echo>

...


<property name="emma.coverage.absolute.file" location="${out.dir}/coverage.em" />

...



<!-- It only instruments class files, not any external libs -->

...


<emma enabled="true">

...


<instr verbosity="${verbosity}"

...


mode="overwrite"

...


instrpath="${out.classes.absolute.dir}"

...


outdir="${out.classes.absolute.dir}"

...


metadatafile="${emma.coverage.absolute.file}">

...


</instr>

...


</emma>

...


</target>

...



</project>

...


==== UIAutomator Sample Project

...