MediaPlayerTest: Improve timestamp logic in testVideoSurfaceResetting

In android.media.cts.MediaPlayerTest#testVideoSurfaceResetting, the
code was assuming that:

   1. The media position does not updated between invocations of
      MediaPlayer.getCurrentPosition and MediaPlayer.setDisplay().

   2. Seeks happen immediately.

These are both false assumptions.

This patch sets a tolerance when comparing the results of
getCurrentPosition().  The tolerance is set to the time for 1.5 frames
of a 15 fps video to elapse.

This patch also waits for the seek complete signal from the media
before continuing with the test.  As a consequence, it is able to use
a lower tolerance when comparing the results after the seek.

Change-Id: I528577b5b92162b4c1a1ba81af801846bacd89d2
Signed-off-by: Gabriel M. Beddingfield <gabrbedd@ti.com>
diff --git a/tests/tests/media/src/android/media/cts/MediaPlayerTest.java b/tests/tests/media/src/android/media/cts/MediaPlayerTest.java
index 9947592..ff43e27 100644
--- a/tests/tests/media/src/android/media/cts/MediaPlayerTest.java
+++ b/tests/tests/media/src/android/media/cts/MediaPlayerTest.java
@@ -27,6 +27,7 @@
 import android.os.PowerManager;
 
 import java.io.File;
+import java.util.concurrent.CountDownLatch;
 
 /**
  * Tests for the MediaPlayer API and local video/audio playback.
@@ -36,6 +37,7 @@
  * Attribution 3.0 License at http://creativecommons.org/licenses/by/3.0/us/.
  */
 public class MediaPlayerTest extends MediaPlayerTestBase {
+
     public void testPlayNullSource() throws Exception {
         try {
             mMediaPlayer.setDataSource((String) null);
@@ -109,9 +111,18 @@
      * from the time setDisplay() was called
      */
     public void testVideoSurfaceResetting() throws Exception {
-        final int tolerance = 150;
+        final int tolerance = 66 * 3 / 2; /* Test video is 15fps... 66 ms per frame */
         final int seekPos = 1500;
 
+        final CountDownLatch seekDone = new CountDownLatch(1);
+
+        mMediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
+            @Override
+            public void onSeekComplete(MediaPlayer mp) {
+                seekDone.countDown();
+            }
+        });
+
         playVideoTest(R.raw.testvideo, 352, 288);
 
         mMediaPlayer.start();
@@ -121,19 +132,20 @@
         mMediaPlayer.setDisplay(getActivity().getSurfaceHolder2());
         int posAfter = mMediaPlayer.getCurrentPosition();
 
-        assertEquals(posAfter, posBefore);
+        assertEquals(posAfter, posBefore, tolerance);
         assertTrue(mMediaPlayer.isPlaying());
 
         Thread.sleep(SLEEP_TIME);
 
         mMediaPlayer.seekTo(seekPos);
+        seekDone.await();
         Thread.sleep(SLEEP_TIME / 2);
 
         posBefore = mMediaPlayer.getCurrentPosition();
         mMediaPlayer.setDisplay(null);
         posAfter = mMediaPlayer.getCurrentPosition();
 
-        assertEquals(posAfter, posBefore);
+        assertEquals(posAfter, posBefore, tolerance);
         assertEquals(seekPos + SLEEP_TIME / 2, posBefore, tolerance);
         assertTrue(mMediaPlayer.isPlaying());
 
@@ -143,7 +155,7 @@
         mMediaPlayer.setDisplay(getActivity().generateSurfaceHolder());
         posAfter = mMediaPlayer.getCurrentPosition();
 
-        assertEquals(posAfter, posBefore);
+        assertEquals(posAfter, posBefore, tolerance);
         assertTrue(mMediaPlayer.isPlaying());
 
         Thread.sleep(SLEEP_TIME);