. f Android action bar item test case using Robotium ~ Android Developers Blog

Sunday, 30 March 2014

Android action bar item test case using Robotium

Hello friends,
                  This is my small tutorial on Android test driven development using Robotium
Today, I am going to share the code which helps you in writing the testcase for
action bar menu item selection.

Here is the complete video  for your reference:

Here is my code:

1. MainActivityTest.java


  1. /* 
  2.  * Copyright (C) 2014 Mukesh Y authors 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16. package com.example.actionbar;  
  17.   
  18. import android.R.integer;  
  19. import android.content.Intent;  
  20. import android.os.Bundle;  
  21. import android.view.Window;  
  22. import android.widget.TextView;  
  23.   
  24. import com.actionbarsherlock.app.ActionBar;  
  25. import com.actionbarsherlock.app.SherlockActivity;  
  26. import com.actionbarsherlock.view.Menu;  
  27. import com.actionbarsherlock.view.MenuItem;  
  28. import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;  
  29. import com.actionbarsherlock.view.SubMenu;  
  30.   
  31. /** 
  32.  * @author Mukesh Y 
  33.  */  
  34. public class MainActivity extends SherlockActivity implements  
  35.   OnMenuItemClickListener {  
  36.   
  37.  private SubMenu mSortItem;  
  38.  private MenuItem mMapItem;  
  39.  private int MAP_ID = 101;  
  40.  private int SORT_ID = 102;  
  41.   
  42.  CharSequence selected = "Light";  
  43.  TextView mTextView;  
  44.   
  45.  @Override  
  46.  protected void onCreate(Bundle savedInstanceState) {  
  47.   super.onCreate(savedInstanceState);  
  48.   requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
  49.   setContentView(R.layout.activity_main);  
  50.   
  51.   mTextView = (TextView)findViewById(R.id.theme);  
  52.   ActionBar ab = getSupportActionBar();  
  53.   ab.setBackgroundDrawable(getApplicationContext().getResources()  
  54.     .getDrawable(R.drawable.bg_titlebar_tile));  
  55.   ab.setDisplayShowTitleEnabled(true);  
  56.   mTextView.setText(selected);  
  57.  }  
  58.   
  59.  @Override  
  60.  public boolean onCreateOptionsMenu(Menu menu) {  
  61.   
  62.   mSortItem = menu.addSubMenu(0,SORT_ID,0,selected);  
  63.   mSortItem.setIcon(R.drawable.ic_menu_sort);  
  64.     
  65.   mSortItem.getItem().setShowAsAction(  
  66.     MenuItem.SHOW_AS_ACTION_IF_ROOM  
  67.       | MenuItem.SHOW_AS_ACTION_WITH_TEXT);  
  68.   mMapItem = menu.add(0, MAP_ID, 0"map");  
  69.   mMapItem.setIcon(R.drawable.google_maps_icon_pressed)  
  70.     .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);  
  71.   
  72.   getMenuState("Light");  
  73.   return true;  
  74.  }  
  75.   
  76.  @Override  
  77.  public boolean onOptionsItemSelected(MenuItem item) {  
  78.     
  79.   if (item.getItemId()==mMapItem.getItemId()) {  
  80.    Intent mapIntent = new Intent(this, Map.class);  
  81.    startActivity(mapIntent);  
  82.   } else {  
  83.   
  84.   }  
  85.   return true;  
  86.  }  
  87.   
  88.  @Override  
  89.  public boolean onMenuItemClick(MenuItem item) {  
  90.   mSortItem.clear();  
  91.   getMenuState(item.getTitle());  
  92.   return false;  
  93.  }  
  94.   
  95.  public void getMenuState(CharSequence selected) {  
  96.   mTextView.setText(selected);  
  97.   if (selected.equals("Default")) {  
  98.    mSortItem.add(0, R.style.Theme_Sherlock, 0"Default")  
  99.      .setIcon(android.R.drawable.checkbox_on_background)  
  100.      .setOnMenuItemClickListener(this);  
  101.    mSortItem.add(0, R.style.Theme_Sherlock_Light, 0"Light")  
  102.      .setIcon(android.R.drawable.checkbox_off_background)  
  103.      .setOnMenuItemClickListener(this);  
  104.    mSortItem.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0"Light (Dark Action Bar)")  
  105.      .setIcon(android.R.drawable.checkbox_off_background)  
  106.      .setOnMenuItemClickListener(this);  
  107.      
  108.    setTheme(R.style.Theme_Sherlock);  
  109.   } else if (selected.equals("Light")) {  
  110.    mSortItem.add(0, R.style.Theme_Sherlock, 0"Default")  
  111.      .setIcon(android.R.drawable.checkbox_off_background)  
  112.      .setOnMenuItemClickListener(this);  
  113.    mSortItem.add(0, R.style.Theme_Sherlock_Light, 0"Light")  
  114.      .setIcon(android.R.drawable.checkbox_on_background)  
  115.      .setOnMenuItemClickListener(this);  
  116.    mSortItem.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0"Light (Dark Action Bar)")  
  117.      .setIcon(android.R.drawable.checkbox_off_background)  
  118.      .setOnMenuItemClickListener(this);  
  119.    setTheme(R.style.Theme_Sherlock_Light);  
  120.   } else {  
  121.    mSortItem.add("Default")  
  122.      .setIcon(android.R.drawable.checkbox_off_background)  
  123.      .setOnMenuItemClickListener(this);  
  124.    mSortItem.add(0, R.style.Theme_Sherlock_Light, 0"Light")  
  125.      .setIcon(android.R.drawable.checkbox_off_background)  
  126.      .setOnMenuItemClickListener(this);  
  127.    mSortItem.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0"Light (Dark Action Bar)")  
  128.      .setIcon(android.R.drawable.checkbox_on_background)  
  129.      .setOnMenuItemClickListener(this);  
  130.    setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);  
  131.   }  
  132.   
  133.  }  
  134. }  


2. AllTests.java
  1. /* 
  2.  * Copyright (C) 2014 Mukesh Y authors 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16. package com.robotium.actionbartest;  
  17.   
  18. import junit.framework.TestSuite;  
  19. import android.app.Activity;  
  20. import android.test.ActivityInstrumentationTestCase2;  
  21.   
  22. /** 
  23.  * @author Mukesh Y 
  24.  */  
  25. public class AllTests extends  ActivityInstrumentationTestCase2<activity> {  
  26.    
  27.    
  28.   
  29.  public AllTests(Class<activity> activityClass) {  
  30.   super(activityClass);  
  31.  }  
  32.   
  33.  public static TestSuite suite() {  
  34.   TestSuite t = new TestSuite();  
  35.   t.addTestSuite(MainActivityTest.class);  
  36.   t.addTestSuite(MapActivityTest.class);  
  37.     
  38.   return t;   
  39.  }  
  40.    
  41.  @Override  
  42.  public void setUp() throws Exception {  
  43.     
  44.  }  
  45.    
  46.    
  47.  @Override  
  48.  public void tearDown() throws Exception {  
  49.  }  
  50.   
  51. }  
  52. </activity></activity>  


Download Code From Here
Enjoy Coding...  :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

1 comments:

  1. hi frnds that was nice post ,thanks for sharing,now you can get all solutions in just one click ,here we have all type of solutions for all type of problems .
    click on app creator to solve your problem

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.