package com.xtremelabs.robolectric.shadows;

import android.app.Activity;
import android.view.View;
import android.widget.TabHost;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

import java.util.ArrayList;
import java.util.List;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(TabHost.class)
public class ShadowTabHost extends ShadowFrameLayout {
    private List<TabHost.TabSpec> tabSpecs = new ArrayList<TabHost.TabSpec>();
    private TabHost.OnTabChangeListener listener;
    private TabHost.TabSpec currentTab;

    @RealObject
    TabHost realObject;

    @Implementation
    public android.widget.TabHost.TabSpec newTabSpec(java.lang.String tag) {
        TabHost.TabSpec realTabSpec = Robolectric.newInstanceOf(TabHost.TabSpec.class);
        shadowOf(realTabSpec).setTag(tag);
        return realTabSpec;
    }

    @Implementation
    public void addTab(android.widget.TabHost.TabSpec tabSpec) {
        tabSpecs.add(tabSpec);
        View indicatorAsView = shadowOf(tabSpec).getIndicatorAsView();
        if (indicatorAsView != null) {
            realObject.addView(indicatorAsView);
        }
    }

    @Implementation
    public void setCurrentTab(int index) {
        currentTab = tabSpecs.get(index);
        if (listener != null) {
            listener.onTabChanged(currentTab.getTag());
        }
    }

    @Implementation
    public void setCurrentTabByTag(String tag) {
        for (TabHost.TabSpec tabSpec : tabSpecs) {
            if (tag.equals(tabSpec.getTag())) {
                currentTab = tabSpec;
            }
        }
        if (listener != null) {
            listener.onTabChanged(currentTab.getTag());
        }
    }

    @Implementation
    public String getCurrentTabTag() {
    	return currentTab.getTag();
    }
    
    @Implementation
    public void setOnTabChangedListener(android.widget.TabHost.OnTabChangeListener listener) {
        this.listener = listener;
    }
    @Implementation
    public View getCurrentView() {
    	ShadowTabSpec ts = Robolectric.shadowOf(currentTab);
    	View v = ts.getContentView(); //just get the view if it was built by a TabContentFactory
    	if (v==null) {
    		//otherwise findViewById
	    	int viewId = ts.getContentViewId();
	    	if (getContext() instanceof Activity) {
	    		/** wasn't quite sure how to find the view here, simply using 
	    		findViewById() wasn't enough, I had to get it though the 
	    		Activity context.**/
	    		v = ((Activity) getContext()).findViewById(viewId);
	    	} else {
	    	//	throw new RuntimeException("getContext is not an Activity, unable to findViewById without an Activity context");
	    		return null;
	    	}
    	}
    	return v;
    }
    
    public TabHost.TabSpec getSpecByTag(String tag) {
        for (TabHost.TabSpec tabSpec : tabSpecs) {
            if (tag.equals(tabSpec.getTag())) {
                return tabSpec;
            }
        }
        return null;
    }
}

@@ -120,24 +120,64 @@
 et.TabHost;%0A
+import android.widget.TabHost.TabSpec;%0A%0A
 import com.x
@@ -761,31 +761,19 @@
 private 
-TabHost.TabSpec
+int
  current
@@ -775,16 +775,21 @@
 rrentTab
+ = -1
 ;%0A%0A    @
@@ -929,24 +929,16 @@
         
-TabHost.
 TabSpec 
@@ -1434,28 +1434,25 @@
 ndex) %7B%0A    
-    
+%09
 currentTab =
@@ -1456,27 +1456,13 @@
 b = 
-tabSpecs.get(
 index
-)
 ;%0A  
@@ -1517,33 +1517,36 @@
 er.onTabChanged(
-c
+getC
 urrentTab.getTag
@@ -1530,36 +1530,32 @@
 ed(getCurrentTab
-.get
 Tag());%0A        
@@ -1636,33 +1636,61 @@
 ) %7B%0A    
-    
+%09
 for (
-TabHost.
+int x=0;x%3CtabSpecs.size();x++) %7B%0A    %09%09
 TabSpec 
@@ -1689,33 +1689,33 @@
 TabSpec tabSpec 
-:
+=
  tabSpecs) %7B%0A   
@@ -1699,35 +1699,40 @@
 bSpec = tabSpecs
-) %7B
+.get(x);
 %0A            if 
@@ -1793,23 +1793,17 @@
 ntTab = 
-tabSpec
+x
 ;%0A      
@@ -1886,17 +1886,20 @@
 Changed(
-c
+getC
 urrentTa
@@ -1903,109 +1903,448 @@
 tTab
-.getTag());%0A        %7D%0A    %7D%0A%0A    @Implementation%0A    public String getCurrentTabTag() %7B%0A    %09return c
+Tag());%0A        %7D%0A    %7D%0A    %0A    @Implementation%0A    public int getCurrentTab() %7B%0A    %09if (currentTab==-1 && tabSpecs.size()%3E0) currentTab=0; %0A        return currentTab;%0A    %7D%0A    %0A    public TabSpec getCurrentTabSpec() %7B%0A    %09return tabSpecs.get(getCurrentTab());%0A    %7D%0A%0A    @Implementation%0A    public String getCurrentTabTag() %7B%0A        if (getCurrentTab() %3E= 0 && getCurrentTab() %3C tabSpecs.size()) %7B%0A            return tabSpecs.get(getC
 urre
@@ -2348,16 +2348,19 @@
 rrentTab
+())
 .getTag(
@@ -2358,24 +2358,55 @@
 ).getTag();%0A
+        %7D%0A        return null;%0A
     %7D%0A    %0A 
@@ -2659,17 +2659,20 @@
 hadowOf(
-c
+getC
 urrentTa
@@ -2672,16 +2672,22 @@
 rrentTab
+Spec()
 );%0A    %09

implement getCurrentTab(), and edit getCurrentTabTag() to use getCurrentTab(). Also, use an integer to record the currentTab internally instead of a TabSpec object. On branch getCurrentTabTag modified: src/main/java/com/xtremelabs/robolectric/shadows/ShadowTabHost.java