Skip to content

Instantly share code, notes, and snippets.

@landliebe
Created August 12, 2012 14:42
Show Gist options
  • Save landliebe/3332145 to your computer and use it in GitHub Desktop.
Save landliebe/3332145 to your computer and use it in GitHub Desktop.
Sd card unmount for test
package at.tugraz.ist.paintroid.test.integration;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.After;
import org.junit.Before;
import android.content.Context;
import android.os.storage.StorageManager;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.GridView;
import android.widget.TextView;
import at.tugraz.ist.paintroid.MainActivity;
import at.tugraz.ist.paintroid.R;
import com.jayway.android.robotium.solo.Solo;
public class SdCardTest extends ActivityInstrumentationTestCase2<MainActivity> {
Solo mSolo;
public SdCardTest() throws Exception {
super("at.tugraz.ist.paintroid", MainActivity.class);
}
@Override
@Before
protected void setUp() throws Exception {
super.setUp();
mSolo = new Solo(getInstrumentation(), getActivity());
}
@Override
@After
protected void tearDown() throws Exception {
setUSBMassstorage(false);
mSolo.finishInactiveActivities();
mSolo.finishOpenedActivities();
super.tearDown();
}
public void testNewDrawingWithoutSdCard() throws IllegalArgumentException, NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
setUSBMassstorage(true);
TextView toolBarButtonMain = (TextView) getActivity().findViewById(R.id.btn_Tool);
mSolo.clickOnView(toolBarButtonMain);
mSolo.waitForView(GridView.class, 1, 2000);
String tabFilemanagerCaption = getActivity().getString(R.string.menu_tab_file);
mSolo.clickOnText(tabFilemanagerCaption);
String newDrawingString = mSolo.getString(R.string.file_new);
mSolo.clickOnText(newDrawingString);
String newDrawingDialogText = mSolo.getString(R.string.dialog_newdrawing_btn_fromcam);
assertTrue("Waiting for New Drawing Dialog", mSolo.waitForText(newDrawingDialogText, 1, 3000));
mSolo.clickOnText(newDrawingDialogText);
String errorMessageSD = mSolo.getString(R.string.dialog_error_sdcard_text);
assertTrue("Waiting for Error message that SD Card not mounted", mSolo.waitForText(errorMessageSD, 1, 3000));
setUSBMassstorage(false);
}
public void testNoSD() throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
setUSBMassstorage(true);
mSolo.clickOnScreen(100, 100);
mSolo.sleep(200);
TextView toolBarButtonMain = (TextView) getActivity().findViewById(R.id.btn_Tool);
mSolo.clickOnView(toolBarButtonMain);
mSolo.waitForView(GridView.class, 1, 2000);
String tabFilemanagerCaption = getActivity().getString(R.string.menu_tab_file);
mSolo.clickOnText(tabFilemanagerCaption);
String saveString = mSolo.getString(R.string.save);
mSolo.waitForText(saveString, 1, 2000);
mSolo.clickOnText(saveString);
String saveDialogTitle = mSolo.getString(R.string.dialog_save_title);
mSolo.waitForText(saveDialogTitle);
Long miliseconds = System.currentTimeMillis();
String fileName = "dummyfile_" + (miliseconds / 1000);
mSolo.enterText(0, fileName);
String okCaption = mSolo.getString(R.string.ok);
mSolo.clickOnText(okCaption);
// Currently no checks, happy if not crashing
// String errorMessageSD = mSolo.getString(R.string.dialog_error_sdcard_text);
// assertTrue("Waiting for Error message that SD Card not mounted", mSolo.waitForText(errorMessageSD, 1, 3000));
// mSolo.clickOnText("OK");
// assertTrue("Waiting for DrawingSurface", mSolo.waitForView(DrawingSurfaceImplementation.class, 1, 3000));
setUSBMassstorage(false);
}
private boolean isSDPresent() {
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
return (isSDPresent);
}
private void setUSBMassstorage(boolean enabled) throws NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
StorageManager storageService = (StorageManager) mSolo.getCurrentActivity().getSystemService(
Context.STORAGE_SERVICE);
Method privateStringMethod;
if (enabled) {
privateStringMethod = StorageManager.class.getDeclaredMethod("enableUsbMassStorage", null);
} else {
privateStringMethod = StorageManager.class.getDeclaredMethod("disableUsbMassStorage", null);
}
privateStringMethod.setAccessible(true);
privateStringMethod.invoke(storageService, null);
int loopKillCount = 0;
boolean loopCondition;
if (enabled) {
loopCondition = isSDPresent();
} else {
loopCondition = !isSDPresent();
}
while (loopCondition) {
mSolo.sleep(500);
loopKillCount++;
if (loopKillCount > 15) {
assertTrue("SD should have been dismounted by now", false);
break;
}
if (enabled) {
loopCondition = isSDPresent();
} else {
loopCondition = !isSDPresent();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment