Sunday, January 3, 2016

Mockito

Hi


Motivation
realy excellent
only drawback is that it can not handle static method but this is solved by PowerMockito).





References


Nathan

Test GUI - Espresso

Hi

Motivation
How can i autotest the UI ?


Solution
Espresso
  • very simple functionality : onView\onData->perform->check



  • ViewMatchers : withId , ...
  • ViewActions : click , typeText , pressKey , clearText
  • ViewAssertions : matches(Matcher) , ....

  • onView(withId(R.id.my_view)) // withId(R.id.my_view) is a ViewMatcher
.perform(click()) // click() is a ViewAction .check(matches(isDisplayed())); // matches(isDisplayed()) is a ViewAssertion
  • notice 3 operations : 1. get view 2. perform operation 3. check result
Espresso runs under "androidTest" i.e. under Device\Emulator


Espresso with intents
suppose i have activity which return info - how can i test it ?
looks like the following link has the answer - here


Questions :

Sunday, December 20, 2015

Gradle

Hi

1. What is it ?
  • Gradle is an advanced build management system
  • Gradle supports the automatic download and configuration of project dependencies (libraries)
  • A project using Gradle is described via build.gradle


2. Dependency management 
Dependency management is compsed of two pieces : dependencies and publications


dependencies :
  • this is what Gradle need to know so he can build or run your project.
  • these incoming files are called dependencies
  • Gradle allows you to tell what are the dependencies of the project so that he can take care of finding these dependencies and make them availeable for your project
  • the dependencies might be downloaded from Maven or Ivy repository or located in local directory or may be need to be build by another project in your multiproject build. this is called dependency resolution
  • notice that you need only to tell to tell Gradle the name of the dependency and it will find it 

publications
  • Gradle need to build and upload the things that your project produce. this is called publications
  • publication may be to put the app on the local disk , or may be upload it to remote maven  or Ivy repository . this process is called publication



3. Decalring your dependency
example :
build.gradle
apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

what do we have above ? build.gradle says few things

  • hibernate core 3.6.7 Final will be used for compilation
  • junit version > 4.0 will be used to compile the tests
  • Gradle will look for dependencies like hibernate in Maven central respository


4. Dependency Configurations
Gradle's dependencies is grouped into configurations.
The java plugin defines number of standard configurations :

  • compile - the dependencies required to compile the production source of the project
  • runtime - the dependencies required by the production classes at runtime. By default includes also the compiled time dependencies
  • testCompile -  the dependencies required to compile the test source of the project. By default includes also the compiled production classes and compiled time dependencies



5. External Dependencies
This dependency relate to file build and stored outside (e.g. on Maven or Ivy repository or local directory) the current build.

example :
build.gradle
dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
}

an external dependency is defined by : group , name and version attributes

You can however use a shortcut : group.name.version as follows :
build.gradle
dependencies {
    compile 'org.hibernate:hibernate-core:3.6.7.Final'
}


6. Repositories

  • Gradle looks for files for you , where does it find them ? in repositories
  • respositories are actually a collection of files organized by : group , name and version.
  • Gradle understands few repositories formats : Maven , Ivy and several ways for accessing the repository such as the local directory of HTTP.
an eaxample of using Maven central repository

build.gradle
repositories {
    mavenCentral()
}



7. References


Nathan

Tuesday, October 20, 2015

Android - Google Map Basic Usage and Sample Code

Hello

first go over this

Important classes\interface :

interface OnMapReadyCallback
  • callback

FragmentActivity
  • Activity that want to use the Fragment API
  • API :

FragmentManager
  • interface to interact with fragment objects inside an activity
  • API :
  • used to get fragment inside the activity using their resource id
  • result can be casted to SupportMapFragment
Fragment
  • basically it is a gui component which lives inside an Activity
  • nice explaination is here

SupportMapFragment

  • inherits Fragment
  • API

GoogleMap
  • API

  • Enables or disables the my-location layer. 
  • requires  permission for eitherACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION 
  • setting the boolean to true will show the circle button and false will hide it. it will center the map to the device location when enabled and provided the location is available. (it is not always working for me , i can see the button but press on it cause some times nothing)
  • reposition the camera according to  CameraUpdate 
  • CameraUpdate can be CameraUpdateFactory.newLatLngZoom( new LatLng(32.811,34.981), 13) . so in this case the camera will zoom around this location. 13 is the value of the zoom ,recommended to be between 2- 21

  • add marker to the map
  •  e.g. addMarker(new MarkerOptions()
                .title("Haifa")
                    .snippet("Home Town")
                      .position(LatLng(32.811,34.981)));

      will position a marker at 32.811,34.981. click on it and you will see the title and snippet






      Sample Code 

      MainActivity.java
      /**
       * This shows how to create a simple activity with a map and a marker on the map.
       */
      public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              SupportMapFragment mapFragment =
                      (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
              mapFragment.getMapAsync(this);
          }

          /**
           * This is where we can add markers or lines, add listeners or move the camera. In this case, we
           * just add a marker near Haifa.
           */
          @Override
       
          public void onMapReady(GoogleMap map) {
              LatLng Haifa = new LatLng(32.811,34.981);

              map.setMyLocationEnabled(true);
              map.moveCamera(CameraUpdateFactory.newLatLngZoom(Haifa, 13));

              map.addMarker(new MarkerOptions()
                      .title("Haifa")
                      .snippet("Home Town")
                      .position(Haifa));
          }
      }

      activity_main.xml
      <?xml version="1.0" encoding="utf-8"?>
      <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

      AndroidManifest.xml
      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.trygooglemaps"
          android:versionCode="1"
          android:versionName="1.0" >

          <uses-sdk
              android:minSdkVersion="14"
              android:targetSdkVersion="19" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.INTERNET"/>
          <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

          <application
              android:allowBackup="true"
              android:icon="@drawable/ic_launcher"
              android:label="@string/app_name">
              <activity
                  android:name=".MainActivity"
                  android:label="@string/app_name" >
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
           
               <!--   THIS KEY IS PER PACKAGE !!!! -->
          <meta-data android:name="com.google.android.geo.API_KEY"
          android:value="AIzaSyCgth2vzXi1r6AQzW87qNjU8dKbjxlZtYU"/>
         
           <meta-data
                  android:name="com.google.android.gms.version"
                  android:value="@integer/google_play_services_version" />
           
          </application>

      </manifest>

      you can get the sample here

      References :


      Nathan

      Monday, October 19, 2015

      Android - Install Google Maps Enhanced Sample Code

      Hello

      Change on eclipse.ini Xmx1024m   !!!!

      Google Maps API is supports API level 9 and up 


      This page describes how to set up the Google Maps Android API utility library with Eclipse, and to run the demo app included in the library.
      For guidelines on setting up the Google Maps Android API utility library with Android Studio/Gradle or Maven, please refer to the library's website.
      My remark ----> this library is for inhanced features and not a must !!!

      Prerequisites and notes

      The Google Maps Android API utility library supports Android API level 9 and above. The animated re-clustering features are only available for Android API level 14 and above. When setting your project build target, make sure you use the latest Android API level.
      Throughout the setup procedure described below, it's a good idea to clean and build the projects often, to make sure you don't try to fix errors that are already fixed. To be on the safe side, use Project > Clean > Clean all projects after each step. Also, make sure Project > Build Automatically is checked.

      Install the Android SDK

      Install the Android SDK. Note that the ADT Bundle includes a version of Eclipse with the Android Developer Tools (ADT) built in.

      Get the Google Maps Android API utility library

      Download the android-maps-utils repository. You can do it by cloning the repo (recommended, to receive automatic updates) or downloading a zip file. If you want to customize the library, you should fork the repo.
      The repository includes:
      • A demo application, in the demo directory.
      • The library of utilities, in the library directory.
      • Various files containing license, contributors, and readme information.
      • Gradle build configuration, for use with Android Studio.


      Import the utility library into Eclipse

      Follow these steps to import the demo and library directories from the utility library into Eclipse:
      1. Choose File > Import > Android > Existing Android Code Into Workspace, and click Next.
      2. Browse to the location where you saved the Google Maps Android API Utility Library, select the option to Copy projects into workspace, and import the demo and librarydirectories.












      Importing the demo and library directories
      Importing the utility library

      Hint: Eclipse will show errors on your project at this stage. Don't be discouraged. The next steps will fix the errors.



      Import the Google Play services SDK

      The Google Maps Android API is distributed as part of the Google Play services SDK. You can download the Google Play services SDK via the Android SDK Manager.













      Here is a summary of the steps you will need to take:
      1. Install the Google Play services SDK and import it into Eclipse, as described in the Google Play services documentation.
      2. Mark Google Play services as a library: Right-click the google-play-services_lib project, and choose Properties > Android. Tick the Is Libraryoption.
        Properties of the Google Play services library after import
        Properties of the Google Play services library
      3. Follow the above steps to mark the utility library project as a library too.
      4. Reference the Google Play services library as a dependency for the utility library project: Right-click the library project and choose Properties > Android. Follow the steps in Referencing a Library Project.
        Properties of the utility library
        Properties of the utility library


      Add the Android Support Library

      Now you need to add the Android Support library to the utility library project:
      1. Right-click the library project and choose Android Tools > Add support library.
      2. Accept the license and install the library.


      Run the utility demo app on your Android device

      The utility library ships with a demo app that includes sample implementations of each utility. Follow these steps to run the demo app on your Android device.

      Reference the utility library from the utility demo app

      Add the utility library as a dependency of the utility demo app:
      1. Right-click the MainActivity project (this is the utility demo app) and chooseProperties > Java Build Path.
      2. Click the Projects tab and choose Add.
      3. Select the library project and click OK, then OK again.












      Properties of the utility demo app
      Properties of the utility demo app

      I got memory problem running this Demo !!!!

      Get an API key

      To access the Google Maps servers with the Google Maps Android API, you need to add an API key to your application. The key is free, and it supports an unlimited number of users. To get a Maps API key, visit the Google Developers Console and provide the utility demo app's signing certificate and package name (com.google.maps.android.utils.demo).
      Then add the API key to the utility demo app's AndroidManifest.xml file. Note that the demo app already has an API key in the manifest. You will need to replace the key with your own API key.
      Detailed instructions are in the guide to API keys :

      ********************************************************************************
      start guide to API keys

      Signup and API Keys












      To use the Google Maps Android API, you must register your app project on the Google Developers Console and get a Google API key which you can add to your app. The type of API key you need is an Android key.

      Overview

      All Android apps are signed with a digital certificate for which you hold the private key. Refer to the Android guide to signing your applications for more information about digital certificates.
      Android API keys are linked to specific certificate/package pairs. You only need one key for each certificate, no matter how many users you have for the app.
      Getting a key for your app requires several steps. These steps are outlined below, and described in detail in the following sections.
      1. Get information about your app's certificate.
      2. Register a project in the Google Developers Console and add the Google Maps Android API as a service for the project.
      3. Create a key.
      4. Add the key to your app by adding an element to your app manifest.

      Display your app's certificate information

      The API key is based on a short form of your app's digital certificate, known as its SHA-1 fingerprint. To display the SHA-1 fingerprint for your certificate, first ensure that you are using the right certificate. You may have two certificates:
      • A debug certificate: The Android SDK tools generate this certificate automatically when you do a debug build. Only use this certificate with apps that you're testing. Do not attempt to publish an app that's signed with a debug certificate. The debug certificate is described in more detail in Signing in Debug Mode in the Android Developer Documentation.
      • A release certificate: The Android SDK tools generate this certificate when you do a release build. You can also generate this certificate using the keytoolprogram. Use this certificate when you are ready to release your app to the world.
      Follow the steps below to display a certificate's SHA-1 fingerprint using the keytoolprogram with the -v parameter. For more information about Keytool, see the Oracle documentation.





















      DEBUG CERTIFICATE
      RELEASE CERTIFICATE

      Displaying the debug certificate fingerprint

      1. Locate your debug keystore file. The file name is debug.keystore, and is created the first time you build your project. By default, it is stored in the same directory as your Android Virtual Device (AVD) files:
        • OS X and Linux~/.android/
        • Windows Vista and Windows 7C:\Users\your_user_name\.android\
        If you are using Eclipse with ADT, and you're not sure where your debug keystore is located, you can select Windows > Prefs > Android > Build to check the full path. Notice that you can get SHA1 using  Windows > Prefs > Android > Build
      2. List the SHA-1 fingerprint:
        • For Linux or OS X, open a terminal window and enter the following:
          keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
        • For Windows Vista and Windows 7, run:
          keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
      You should see output similar to this:
      Alias name: androiddebugkeyCreation date: Jan 01, 2013 Entry type: PrivateKeyEntry Certificate chain length: 1 Certificate[1]: Owner: CN=Android Debug, O=Android, C=USIssuer: CN=Android Debug, O=Android, C=USSerial number: 4aa9b300 Valid from: Mon Jan 01 08:04:04 UTC 2013 until: Mon Jan 01 18:04:04 PST 2033 Certificate fingerprints:      MD5:  AE:9F:95:D0:A6:86:89:BC:A8:70:BA:34:FF:6A:AC:F9      SHA1: BB:0D:AC:74:D3:21:E1:43:07:71:9B:62:90:AF:A1:66:6E:44:5D:75      Signature algorithm name: SHA1withRSA      Version: 3

      The line that begins with SHA1 contains the certificate's SHA-1 fingerprint. The fingerprint is the sequence of 20 two-digit hexadecimal numbers separated by colons.











      Create an API project in the Google Developers Console

      Follow these steps to create or modify a project for your application in the Google Developers Console and enable the Google Maps Android API.
      If you want to be guided through the process and activate the Google Maps Android APIautomatically, click this link.
      Alternatively, you can activate the Google Maps Android API yourself in the Developers Console by doing the following:
      1. Go to the Google Developers Console.
      2. Select a project, or create a new one.
      3. Open the API Library in the Google Developers Console. If prompted, select a project or create a new one. Select the Enabled APIs link in the API section to see a list of all your enabled APIs. Make sure that the API is on the list of enabled APIs. If you have not enabled it, select the API from the list of APIs, then select theEnable API button for the API. The only API you need is the Google Maps Android API, although you can choose to enable other APIs for the same project too.

      Get an Android API key












      Access your project's API keys and other credentials as follows:
      1. Go to the Google Developers Console.
      2. In the sidebar on the left, select Credentials.
      3. If your project doesn't already have an Android API key, create one now by selecting Add credentials > API key > Android key.
      4. In the resulting dialog, enter your app's SHA-1 fingerprint and package name. For example:
        BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75
        com.example.android.mapexample
      5. Your new Android API key appears in the list of API keys for your project. An API key is a string of characters, something like this:
        AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0

      Add the API key to your application

      Follow the steps below to include the API key in your application's manifest, contained in the file AndroidManifest.xml.
      1. In AndroidManifest.xml, add the following element as a child of the<application> element, by inserting it just before the closing</application> tag:
            <meta-data         android:name="com.google.android.geo.API_KEY"         android:value="API_KEY"/>    
        Substitute your API key for API_KEY in the value attribute. This element sets the key com.google.android.geo.API_KEY to the value of your API key.
      2. Save AndroidManifest.xml and re-build your application.
      3. Note: As shown above, com.google.android.geo.API_KEY is the recommended metadata name for the API key. A key with this name can be used to authenticate to multiple Google Maps-based APIs on the Android platform, including the Google Maps Android API. For backwards compatibility, the API also supports the name com.google.android.maps.v2.API_KEY. This legacy name allows authentication to the Android Maps API v2 only. An application can specify only one of the API key metadata names. If both are specified, the API throws an exception.


      end guide to API keys
      *********************************************************************************


      Run the demo app

      If you haven't already done so, enable developer mode on the Android device. Then attach the Android device to your computer.
      Right click the MainActivity project (this is the utility demo app) and choose Run As > Android Application.
      The app should open on the Android device. You can also find it in your app list, under the name Maps Utils Demo.
      This screenshot shows the app on an Nexus 4 device:









      The utility demo app on a Nexus 4

      running it i get the :



      References :



      Nathan