package se.chalmers.watchmetest.activity;

import android.support.v4.view.ViewPager;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import se.chalmers.watchme.activity.MainActivity;
import se.chalmers.watchme.activity.TabsAdapter;
import se.chalmers.watchme.ui.MovieListFragment;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.Tab;


public class TabsAdapterTest extends ActivityInstrumentationTestCase2<MainActivity> {
	
	public TabsAdapterTest() {
		super(MainActivity.class);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
	}
	
	//TODO fix api level
	@TargetApi(11)
	@UiThreadTest
	public void testAddTab() {
		MainActivity mainActivity = this.getActivity();
		ActionBar actionBar = mainActivity.getActionBar();
		TabsAdapter tabsAdapter = new TabsAdapter(mainActivity, new ViewPager(mainActivity));
		
		//TODO: Smelly test? 
		// better getting empty action bar or clearing action bar from mainactivity?
		// actionBar.removeAllTabs() generated exception
		//Some tabs might have been added already, for example in mainActivity.onCreate()
		int initialNrOfTabs = actionBar.getTabCount();
		
		for (int i = 1; i <= 3 ; i++) {
			Tab tab = actionBar.newTab().setText("tab" + i);
			tabsAdapter.addTab(tab, MovieListFragment.class, null);	
		}
		//TODO: test tab.getTag()... 
		//Returns inner class, how to do?
		
		//Test if tab was added in the TabsAdapter's internal list
		assertTrue(tabsAdapter.getCount() == 3);
		
		//Test if tab was added in the action bar
		assertTrue(actionBar.getTabCount() == 3 + initialNrOfTabs);		
		
	}
	
	//TODO: fix api level
	@TargetApi(11)
	@UiThreadTest
	public void testOnTabSelected() {
		MainActivity mainActivity = this.getActivity();
		ActionBar actionBar = mainActivity.getActionBar();
		ViewPager viewPager = new ViewPager(mainActivity);
		TabsAdapter tabsAdapter = new TabsAdapter(mainActivity, viewPager);	
		
		Tab tab1 = actionBar.newTab().setText("tab1");
		tabsAdapter.addTab(tab1, MovieListFragment.class, null);	
		
		Tab tab2 = actionBar.newTab().setText("tab2");
		tabsAdapter.addTab(tab2, MovieListFragment.class, null);
		
		//TODO: possible change if less smelly: add getTabs() in TabsAdapter and 
		// use assertTrue(viewPager.getCurrentItem() == tabsAdapter.getTabs().indexOf(tab2))
		tabsAdapter.onTabSelected(tab2, null);
		assertTrue(viewPager.getCurrentItem() == 1);
		
		tabsAdapter.onTabSelected(tab1, null);
		assertTrue(viewPager.getCurrentItem() == 0);
		
	}

}

@@ -637,16 +637,205 @@
 etUp();%0A
+%09%09//TODO: Add mainActivity, actionBar, etc., as instance variables?%0A%09%09// Lesser repeating of code but bad reusing same stuff for different%0A%09%09// tests? (think Joacim von Hacht said that)...%0A
 %09%7D%0A%09%0A%09//
@@ -2729,12 +2729,475 @@
 );%0A%09%09%0A%09%7D
+%0A%09%0A%09//TODO: fix api level%0A%09@TargetApi(11)%0A%09@UiThreadTest%0A%09public void testGetItem() %7B%0A%09%09MainActivity mainActivity = this.getActivity();%0A%09%09ActionBar actionBar = mainActivity.getActionBar();%0A%09%09TabsAdapter tabsAdapter = new TabsAdapter(mainActivity, new ViewPager(mainActivity));%0A%09%09%0A%09%09Tab tab = actionBar.newTab().setText(%22tab%22);%0A%09%09tabsAdapter.addTab(tab, MovieListFragment.class, null);%0A%09%09assertTrue(tabsAdapter.getItem(0).getClass() == MovieListFragment.class);%0A%09%7D
 %0A%0A%7D%0A

Add method testGetItem() in TabsAdapterTest