Add more CTS tests.

These tests cover streaming mp3 over http, streaming the same file
again, streaming with a redirect, streaming with unknown content
length, streaming ogg vorbis, streaming ogg vorbis with redirect,
and streaming ogg vorbis with unknown content length.

Change-Id: I40b0dd5f7d1b44c2e27ab46bff5e0850d65e4776
diff --git a/tests/assets/noiseandchirps.mp3 b/tests/assets/noiseandchirps.mp3
new file mode 100644
index 0000000..a819efc
--- /dev/null
+++ b/tests/assets/noiseandchirps.mp3
Binary files differ
diff --git a/tests/assets/noiseandchirps.ogg b/tests/assets/noiseandchirps.ogg
new file mode 100644
index 0000000..1acb643
--- /dev/null
+++ b/tests/assets/noiseandchirps.ogg
Binary files differ
diff --git a/tests/src/android/webkit/cts/CtsTestServer.java b/tests/src/android/webkit/cts/CtsTestServer.java
index 87b3d1d..78eed43 100644
--- a/tests/src/android/webkit/cts/CtsTestServer.java
+++ b/tests/src/android/webkit/cts/CtsTestServer.java
@@ -87,6 +87,7 @@
     public static final String COOKIE_PREFIX = "/cookie";
     public static final String AUTH_PREFIX = "/auth";
     public static final String SHUTDOWN_PREFIX = "/shutdown";
+    public static final String NOLENGTH_POSTFIX = "nolength";
     public static final int DELAY_MILLIS = 2000;
 
     public static final String AUTH_REALM = "Android CTS";
@@ -381,6 +382,7 @@
         mLastQuery = uriString;
         URI uri = URI.create(uriString);
         String path = uri.getPath();
+        String query = uri.getQuery();
         if (path.equals(FAVICON_PATH)) {
             path = FAVICON_ASSET_PATH;
         }
@@ -449,7 +451,9 @@
                 }
                 entity.setContentType(mimeType);
                 response.setEntity(entity);
-                response.setHeader("Content-Length", "" + entity.getContentLength());
+                if (query == null || !query.contains(NOLENGTH_POSTFIX)) {
+                    response.setHeader("Content-Length", "" + entity.getContentLength());
+                }
             } catch (IOException e) {
                 response = null;
                 // fall through, return 404 at the end
diff --git a/tests/tests/media/src/android/media/cts/MediaPlayerTest.java b/tests/tests/media/src/android/media/cts/MediaPlayerTest.java
index a4a17c0..d18d684 100644
--- a/tests/tests/media/src/android/media/cts/MediaPlayerTest.java
+++ b/tests/tests/media/src/android/media/cts/MediaPlayerTest.java
@@ -51,6 +51,7 @@
 
     private static String TAG = "CtsMediaPlayerTest";
     private static final int SLEEP_TIME = 1000;
+    private static final int LONG_SLEEP_TIME = 6000;
     private final String mSourceMediaOnSdcard;
     private Monitor mOnVideoSizeChangedCalled = new Monitor();
     private Monitor mOnBufferingUpdateCalled = new Monitor();
@@ -89,6 +90,10 @@
     private static class Monitor {
         private boolean signalled;
 
+        public synchronized void reset() {
+            signalled = false;
+        }
+
         public synchronized void signal() {
             signalled = true;
             notifyAll();
@@ -419,15 +424,49 @@
             args = {}
         )
     })
-    public void testPlayStream() throws Throwable {
-        mServer = new CtsTestServer(mContext);
-        final String stream_mp3 = mServer.getAssetUrl("ringer.mp3");
 
-        mMediaPlayer.setDataSource(stream_mp3);
+    public void testPlayMp3Stream1() throws Throwable {
+        streamTest("ringer.mp3", false, false);
+    }
+    public void testPlayMp3Stream2() throws Throwable {
+        streamTest("ringer.mp3", false, false);
+    }
+    public void testPlayMp3StreamRedirect() throws Throwable {
+        streamTest("ringer.mp3", true, false);
+    }
+    public void testPlayMp3StreamNoLength() throws Throwable {
+        streamTest("noiseandchirps.mp3", false, true);
+    }
+    public void testPlayOggStream() throws Throwable {
+        streamTest("noiseandchirps.ogg", false, false);
+    }
+    public void testPlayOggStreamRedirect() throws Throwable {
+        streamTest("noiseandchirps.ogg", true, false);
+    }
+    public void testPlayOggStreamNoLength() throws Throwable {
+        streamTest("noiseandchirps.ogg", false, true);
+    }
+
+    private void streamTest(String name, boolean redirect, boolean nolength) throws Throwable {
+        mServer = new CtsTestServer(mContext);
+        String stream_url = null;
+        if (redirect) {
+            // Stagefright doesn't have a limit, but we can't test support of infinite redirects
+            // Up to 4 redirects seems reasonable though.
+            stream_url = mServer.getRedirectingAssetUrl(name, 4);
+        } else {
+            stream_url = mServer.getAssetUrl(name);
+        }
+        if (nolength) {
+            stream_url = stream_url + "?" + CtsTestServer.NOLENGTH_POSTFIX;
+        }
+
+        mMediaPlayer.setDataSource(stream_url);
 
         mMediaPlayer.setDisplay(getActivity().getSurfaceHolder());
         mMediaPlayer.setScreenOnWhilePlaying(true);
 
+        mOnBufferingUpdateCalled.reset();
         mMediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
             public void onBufferingUpdate(MediaPlayer mp, int percent) {
                 mOnBufferingUpdateCalled.signal();
@@ -436,11 +475,18 @@
 
         assertFalse(mOnBufferingUpdateCalled.signalled);
         mMediaPlayer.prepare();
-        mOnBufferingUpdateCalled.waitForSignal();
 
-        mMediaPlayer.start();
-        Thread.sleep(SLEEP_TIME);
+        if (nolength) {
+            mMediaPlayer.start();
+            Thread.sleep(LONG_SLEEP_TIME);
+            assertFalse(mMediaPlayer.isPlaying());
+        } else {
+            mOnBufferingUpdateCalled.waitForSignal();
+            mMediaPlayer.start();
+            Thread.sleep(SLEEP_TIME);
+        }
         mMediaPlayer.stop();
+        mMediaPlayer.reset();
     }
 
     @TestTargets({
@@ -537,6 +583,7 @@
             }
         });
 
+        mOnCompletionCalled.reset();
         mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
             public void onCompletion(MediaPlayer mp) {
                 mOnCompletionCalled.signal();