From bca21d551537de0c62216ce8ac93c2e68ff746bf Mon Sep 17 00:00:00 2001 From: Bartlomiej Niechwiej Date: Fri, 5 Nov 2010 10:11:55 -0700 Subject: [PATCH] Addressed Rodrigo's comments. --- .../android/apps/mytracks/MyTracksTest.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/MyTracksTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/MyTracksTest.java index c7e135332..5ade845eb 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/MyTracksTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/MyTracksTest.java @@ -24,6 +24,8 @@ import android.net.Uri; import android.test.ActivityInstrumentationTestCase2; import java.io.File; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; /** * A unit test for {@link MyTracks} activity. @@ -101,6 +103,8 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2{ long selectedTrackId = getActivity().getSharedPreferences().getLong( getActivity().getString(R.string.selected_track_key), -1); assertEquals(selectedTrackId, getActivity().getSelectedTrackId()); + + // TODO: Finish this test. } public void testInitialization_viewActionWithInvalidData() throws Exception { @@ -121,6 +125,8 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2{ long selectedTrackId = getActivity().getSharedPreferences().getLong( getActivity().getString(R.string.selected_track_key), -1); assertEquals(selectedTrackId, getActivity().getSelectedTrackId()); + + // TODO: Finish this test. } public void testRecording_startAndStop() throws Exception { @@ -168,17 +174,22 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2{ * Waits until the UI thread becomes idle. */ private void waitForIdle() throws InterruptedException { + // Note: We can't use getInstrumentation().waitForIdleSync() here. final Object semaphore = new Object(); synchronized (semaphore) { + final AtomicBoolean isIdle = new AtomicBoolean(); getInstrumentation().waitForIdle(new Runnable() { @Override public void run() { synchronized (semaphore) { + isIdle.set(true); semaphore.notify(); } } }); - semaphore.wait(); + while (!isIdle.get()) { + semaphore.wait(); + } } } @@ -186,6 +197,7 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2{ * Clears {selected,recording}TrackId in the {@link SharedPreferences}. */ private void clearSelectedAndRecordingTracks() { + // TODO: Consider clearing all preferences. Editor editor = getActivity().getSharedPreferences().edit(); editor.putLong(getActivity().getString(R.string.selected_track_key), -1); editor.putLong(getActivity().getString(R.string.recording_track_key), -1); @@ -200,11 +212,11 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2{ * @return the recording track ID. */ private long awaitRecordingStatus(long timeout, boolean isRecording) - throws InterruptedException { + throws TimeoutException, InterruptedException { long startTime = System.nanoTime(); while (getActivity().isRecording() != isRecording) { 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); }