Commits

Ville Saalo committed 9309f02

Created a couple of tests for LauncherActivity.

  • Participants
  • Parent commits f6f6f33

Comments (0)

Files changed (2)

src/com/saibotd/bitbeaker/tests/BitbeakerInstrumentationTestRunner.java

 		suite.addTestSuite(MarkupHelperTest.class);
 		suite.addTestSuite(BitbeakerTest.class);
 		suite.addTestSuite(HelperTest.class);
+		suite.addTestSuite(LauncherActivityTest.class);
 		
 		return suite;
 	}

src/com/saibotd/bitbeaker/tests/LauncherActivityTest.java

+package com.saibotd.bitbeaker.tests;
+
+import java.lang.reflect.Method;
+
+import android.content.res.Resources;
+import android.net.Uri;
+import android.os.AsyncTask;
+import android.test.mock.MockResources;
+
+import com.saibotd.bitbeaker.LauncherActivity;
+import com.saibotd.bitbeaker.R;
+
+import junit.framework.TestCase;
+
+public class LauncherActivityTest extends TestCase {
+	private TestableLauncherActivity activity;
+	private Method handleUriData;
+	
+	/**
+	 * Prepare a fresh instance of the class and mark the handleUriData(Uri) method as accessible.
+	 */
+	public void setUp() throws Exception {
+		activity = new TestableLauncherActivity();
+		handleUriData = activity.getClass().getSuperclass().getDeclaredMethod("handleUriData", Uri.class);
+		handleUriData.setAccessible(true);
+	}
+	
+	public void test_single_issue() throws Exception {
+		Uri uri = Uri.parse("http://bitbucket.org/saibotd/bitbeaker/issue/11/implement-show-edit-add-and-delete");
+		handleUriData.invoke(activity, uri);
+		
+		String[] resultParams = activity.getParameters();
+		assertNotNull(resultParams);
+		assertEquals(3, resultParams.length);
+		assertEquals("https://api.bitbucket.org/1.0/repositories/saibotd/bitbeaker/issues/11", resultParams[0]);
+		assertNull(resultParams[1]);
+		assertEquals("Async error -- test message", resultParams[2]);
+	}
+	
+	public void test_changesets() throws Exception {
+		Uri uri = Uri.parse("https://bitbucket.org/saibotd/bitbeaker/changeset/e5dc8a46eda8d9a2bfc518f7ba98cc54b5ae4b26");
+		handleUriData.invoke(activity, uri);
+		
+		String[] resultParams = activity.getParameters();
+		assertNotNull(resultParams);
+		assertEquals(3, resultParams.length);
+		assertEquals("https://api.bitbucket.org/1.0/repositories/saibotd/bitbeaker/changesets/e5dc8a46eda8d9a2bfc518f7ba98cc54b5ae4b26", resultParams[0]);
+		assertNull(resultParams[1]);
+		assertEquals("Async error -- test message", resultParams[2]);
+	}
+	
+	/**
+	 * This class overrides the executeAsyncLoader(String...) method of
+	 * LauncherActivity, allowing this test class to get the parameters
+	 * passed to that method.
+	 */
+	private class TestableLauncherActivity extends LauncherActivity {
+		private String[] parameters;
+		
+		@Override
+		public AsyncTask<String, Integer, String> executeAsyncLoader(String... params) {
+			parameters = params;
+			return null;
+		}
+		
+		public String[] getParameters() {
+			return parameters;
+		}
+		
+		public Resources getResources() {
+			return new MockResources() {
+				@Override
+				public String getString(int id) {
+					switch (id) {
+					case R.string.AsyncErrorMSG:
+						return "Async error -- test message!";
+					case R.string.no_branches:
+						return "No branches -- test message!";
+					default:
+						return "Test error message!";
+					}
+				}
+			};
+		}
+	}
+}