Addressed Rodrigo's comments.

This commit is contained in:
Bartlomiej Niechwiej
2010-11-05 10:11:55 -07:00
parent 89cae2e7c9
commit bca21d5515
@@ -24,6 +24,8 @@ import android.net.Uri;
import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityInstrumentationTestCase2;
import java.io.File; import java.io.File;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* A unit test for {@link MyTracks} activity. * A unit test for {@link MyTracks} activity.
@@ -101,6 +103,8 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
long selectedTrackId = getActivity().getSharedPreferences().getLong( long selectedTrackId = getActivity().getSharedPreferences().getLong(
getActivity().getString(R.string.selected_track_key), -1); getActivity().getString(R.string.selected_track_key), -1);
assertEquals(selectedTrackId, getActivity().getSelectedTrackId()); assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
// TODO: Finish this test.
} }
public void testInitialization_viewActionWithInvalidData() throws Exception { public void testInitialization_viewActionWithInvalidData() throws Exception {
@@ -121,6 +125,8 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
long selectedTrackId = getActivity().getSharedPreferences().getLong( long selectedTrackId = getActivity().getSharedPreferences().getLong(
getActivity().getString(R.string.selected_track_key), -1); getActivity().getString(R.string.selected_track_key), -1);
assertEquals(selectedTrackId, getActivity().getSelectedTrackId()); assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
// TODO: Finish this test.
} }
public void testRecording_startAndStop() throws Exception { public void testRecording_startAndStop() throws Exception {
@@ -168,17 +174,22 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
* Waits until the UI thread becomes idle. * Waits until the UI thread becomes idle.
*/ */
private void waitForIdle() throws InterruptedException { private void waitForIdle() throws InterruptedException {
// Note: We can't use getInstrumentation().waitForIdleSync() here.
final Object semaphore = new Object(); final Object semaphore = new Object();
synchronized (semaphore) { synchronized (semaphore) {
final AtomicBoolean isIdle = new AtomicBoolean();
getInstrumentation().waitForIdle(new Runnable() { getInstrumentation().waitForIdle(new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (semaphore) { synchronized (semaphore) {
isIdle.set(true);
semaphore.notify(); semaphore.notify();
} }
} }
}); });
semaphore.wait(); while (!isIdle.get()) {
semaphore.wait();
}
} }
} }
@@ -186,6 +197,7 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
* Clears {selected,recording}TrackId in the {@link SharedPreferences}. * Clears {selected,recording}TrackId in the {@link SharedPreferences}.
*/ */
private void clearSelectedAndRecordingTracks() { private void clearSelectedAndRecordingTracks() {
// TODO: Consider clearing all preferences.
Editor editor = getActivity().getSharedPreferences().edit(); Editor editor = getActivity().getSharedPreferences().edit();
editor.putLong(getActivity().getString(R.string.selected_track_key), -1); editor.putLong(getActivity().getString(R.string.selected_track_key), -1);
editor.putLong(getActivity().getString(R.string.recording_track_key), -1); editor.putLong(getActivity().getString(R.string.recording_track_key), -1);
@@ -200,11 +212,11 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
* @return the recording track ID. * @return the recording track ID.
*/ */
private long awaitRecordingStatus(long timeout, boolean isRecording) private long awaitRecordingStatus(long timeout, boolean isRecording)
throws InterruptedException { throws TimeoutException, InterruptedException {
long startTime = System.nanoTime(); long startTime = System.nanoTime();
while (getActivity().isRecording() != isRecording) { while (getActivity().isRecording() != isRecording) {
if (System.nanoTime() - startTime > timeout * 1000000) { if (System.nanoTime() - startTime > timeout * 1000000) {
throw new InterruptedException("Timeout while waiting for recording!"); throw new TimeoutException("Timeout while waiting for recording!");
} }
Thread.sleep(20); Thread.sleep(20);
} }