Friday 7 March 2014

java.lang.RuntimeException: Stub! in Robolectric


Hello Droid Friends,
                              Have you facing following error while running a test case using
Roboelectric and Junit-4.

java.lang.RuntimeException: Stub!
at android.content.Context.<init>(Context.java:4)
at android.content.ContextWrapper.<init>(ContextWrapper.java:5)
at android.view.ContextThemeWrapper.<init>(ContextThemeWrapper.java:5)
at android.app.Activity.<init>(Activity.java:6)
at com.actionbarsherlock.app.SherlockActivity.<init>(SherlockActivity.java:21)
at com.opttown.OptTown.DrawerBaseActivity.<init>(DrawerBaseActivity.java:34)
at com.opttown.Places.MyPlaces.<init>(MyPlaces.java:63)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.fest.reflect.constructor.Invoker.newInstance(Invoker.java:77)
at org.robolectric.util.ActivityController.<init>(ActivityController.java:47)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:1409)
at com.opttown.test.MyPlacesTest.setUp(MyPlacesTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


Most of stub!  exceptions are because the tests are not annotated with the correct TestRunner. For example in my case I forgot to add:
@RunWith(RobolectricTestRunner.class)

After adding the above annotation the above issue will be fixed.


import static org.junit.Assert.assertNotNull;
import static org.robolectric.Robolectric.shadowOf;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;

import android.content.Context;

import com.android.Places.MyPlaces;

@RunWith(RobolectricTestRunner.class)
public class MyPlacesTest {
 
 private MyPlaces activity;
 Context ctx;
 ShadowActivity shadowActivity;
 
 @Before
 public void setUp() throws Exception {
  activity = Robolectric.buildActivity(MyPlaces.class)
                .create()
                .get();
  shadowActivity = shadowOf(activity);
  ctx = activity;
 }
 
 /**
   * @testdox: for activity instance test 
   * given: 
   * when:  app launch
   * then: checking the instance of activity
   */
  @Test
  @Config(reportSdk = 10)
  public void testActivityInstance() {
   assertNotNull(shadowActivity.getApplicationContext());
   Assert.assertEquals("Result",activity.getApplicationContext(),sh                       adowActivity.getApplicationContext());
  }



Hope this will help you during writing the test case for your android project.



Enjoy Coding... :) 

You may also Read this:
1. Roboelectric setup with eclipse
2. R
oboelectric AndroidManifest.xml not found error
3. Roboelectric tutorial
4. Asserting Toast message using Robolectric
.

Thursday 20 February 2014

Android capture image from Camera and Gallery

Hello Droid Friends,

Today , I am sharing the code for capture Image from Camera and Gallery . Actually
I found different behavior of Camera capture Intent and Gallery Intent on different
Android devices .

Here are the few issue I found while capturing Image from Camera and Gallery:
1. In some devices like Samsung  the Camera capture Intent returns
    data null or some time gallery Intent returns data null.
2. Android Camera : data intent returns null
3. Android camera capture activity returns null Uri
4. onActivityResult Camera resulting data as null (SAMSUNG)

I too faces all these issue, then I uses below code which works dine for me.

Code:.

1. MainActivity.java:


package com.gallerycamera.demo;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

 private static int THUMBNAIL_SIZE = 300;
 private static final int YOUR_SELECT_PICTURE_REQUEST_CODE = 232;

 private Button button;
 private ImageView image;
 private Bitmap bmp;

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

  image = (ImageView) findViewById(R.id.activity_main_image);
  button = (Button) findViewById(R.id.activity_main_button);
  button.setOnClickListener(buttonListener);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  if (bmp != null && !bmp.isRecycled()) {
   bmp.recycle();
   bmp = null;
  }
 }

 private View.OnClickListener buttonListener = new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   // Determine Uri of camera image to save.
   FileUtils.createDefaultFolder(MainActivity.this);
   //final File file = FileUtils.createFile(FileUtils.IMAGE_FILE);
   //outputFileUri = Uri.fromFile(file);
 
   // Camera.
   final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
   //captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
 
   final Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
   //galleryIntent.setType("image/*");
   // Filesystems
   // galleryIntent.setAction(Intent.ACTION_GET_CONTENT); // To allow file managers or any other app that are not gallery app.
 
   final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Image");
   // Add the camera options.
   chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent });
   startActivityForResult(chooserIntent, YOUR_SELECT_PICTURE_REQUEST_CODE);
  }
 };

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  try {
   //if (resultCode == Activity.RESULT_OK) {
    if (requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) {
      Bundle extras2 = data.getExtras();
      if (extras2 != null) {    
       Uri selectedImage = data.getData();
       if (selectedImage != null) {
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(
              selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        
        bmp = ImageUtils.getThumbnail(this,filePath, THUMBNAIL_SIZE);
        image.setImageBitmap(bmp);
        bmp = null;
       }
      }
     }
    //}
   
  } catch (IOException e) {
   System.out.println(e.getMessage());
  }
 }

}



2. ImageUtils.java

package com.gallerycamera.demo;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageUtils {

 /**
  * Get a thumbnail bitmap.
  * @param uri
  * @return a thumbnail bitmap
  * @throws FileNotFoundException
  * @throws IOException
  */
 public static Bitmap getThumbnail(Context context, String filePath, int thumbnailSize) throws FileNotFoundException, IOException {
  BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
  onlyBoundsOptions.inJustDecodeBounds = true;
  onlyBoundsOptions.inDither = true;// optional
  onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
  BitmapFactory.decodeFile(filePath, onlyBoundsOptions);
  if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
   return null;

  int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;

  double ratio = (originalSize > thumbnailSize) ? (originalSize / thumbnailSize) : 1.0;

  BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
  bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
  bitmapOptions.inDither = true;// optional
  bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
  Bitmap bitmap = BitmapFactory.decodeFile(filePath, bitmapOptions);
  return bitmap;
 }

 /**
  * Resolve the best value for inSampleSize attribute.
  * @param ratio
  * @return
  */
 private static int getPowerOfTwoForSampleRatio(double ratio) {
  int k = Integer.highestOneBit((int) Math.floor(ratio));
  if (k == 0)
   return 1;
  else
   return k;
 }
 
}


Download code Camera and Gallery Demo

Hope this will help some one.
Enjoy Coding.... :)


 

Copyright @ 2013 Android Developers Blog.