Merge change 2960 into donut

* changes:
  CTS: Clean up and fix MemoryFileTest
diff --git a/tests/tests/os/src/android/os/cts/MemoryFileTest.java b/tests/tests/os/src/android/os/cts/MemoryFileTest.java
index 1a062bc..bb5795d 100644
--- a/tests/tests/os/src/android/os/cts/MemoryFileTest.java
+++ b/tests/tests/os/src/android/os/cts/MemoryFileTest.java
@@ -52,13 +52,9 @@
             args = {}
         )
     })
-    public void testConstructor() {
-        try {
-            // new the MemoryFile instance
-            new MemoryFile("Test File", 1024);
-        } catch (IOException e) {
-            fail(e.getMessage());
-        }
+    public void testConstructor() throws IOException {
+        // new the MemoryFile instance
+        new MemoryFile("Test File", 1024);
     }
 
     @TestTargetNew(
@@ -67,38 +63,29 @@
         method = "writeBytes",
         args = {byte[].class, int.class, int.class, int.class}
     )
-    public void testWriteBytes() {
+    public void testWriteBytes() throws IOException {
         byte[] data = new byte[512];
-        try {
-            // new the MemoryFile instance
-            mMemoryFile = new MemoryFile("Test File", 1024);
+        // new the MemoryFile instance
+        mMemoryFile = new MemoryFile("Test File", 1024);
 
-            mMemoryFile.writeBytes(data, 0, 0, 512);
-        } catch (IOException e) {
-            fail(e.getMessage());
-        }
+        mMemoryFile.writeBytes(data, 0, 0, 512);
 
         checkWriteBytesInIllegalParameter(-1, 0, 128);
-
         checkWriteBytesInIllegalParameter(1000, 0, 128);
-
         checkWriteBytesInIllegalParameter(0, 0, -1);
-
         checkWriteBytesInIllegalParameter(0, 0, 1024);
-
         checkWriteBytesInIllegalParameter(0, -1, 512);
-
         checkWriteBytesInIllegalParameter(0, 2000, 512);
     }
 
-    private void checkWriteBytesInIllegalParameter(int srcOffset, int destOffset, int count) {
+    private void checkWriteBytesInIllegalParameter(int srcOffset, int destOffset, int count)
+            throws IOException {
         try {
             byte[] data = new byte[512];
             mMemoryFile.writeBytes(data, srcOffset, destOffset, count);
-            fail("MemoryFile would throw IndexOutOfBoundsException here.");
+            fail("MemoryFile should throw IndexOutOfBoundsException here.");
         } catch (IndexOutOfBoundsException e) {
-        } catch (IOException e) {
-            fail("MemoryFile would not throw IOException here.");
+            // expected
         }
     }
 
@@ -116,27 +103,19 @@
             args = {}
         )
     })
-    public void testGetOutputStream() {
+    public void testGetOutputStream() throws IOException {
         byte[] bs = new byte[] { 1, 2, 3, 4 };
-        try {
-            // new the MemoryFile instance
-            mMemoryFile = new MemoryFile("Test File", 1024);
-            OutputStream out = mMemoryFile.getOutputStream();
-            assertNotNull(out);
-            out.write(bs);
-        } catch (IOException e) {
-            fail(e.getMessage());
-        }
+        // new the MemoryFile instance
+        mMemoryFile = new MemoryFile("Test File", 1024);
+        OutputStream out = mMemoryFile.getOutputStream();
+        assertNotNull(out);
+        out.write(bs);
 
         InputStream in = mMemoryFile.getInputStream();
-        try {
-            assertEquals(1, in.read());
-            assertEquals(2, in.read());
-            assertEquals(3, in.read());
-            assertEquals(4, in.read());
-        } catch (IOException e) {
-            fail(e.getMessage());
-        }
+        assertEquals(1, in.read());
+        assertEquals(2, in.read());
+        assertEquals(3, in.read());
+        assertEquals(4, in.read());
     }
 
     @TestTargets({
@@ -155,31 +134,22 @@
     })
     @ToBeFixed(bug = "1537041", explanation = "When set mAllowPurging to true, writeBytes"
                      + "should throw out exception")
-    public void testAllowPurging() {
+    public void testAllowPurging() throws IOException {
+        // new the MemoryFile instance
+        mMemoryFile = new MemoryFile("Test File", 1024);
+
+        assertFalse(mMemoryFile.allowPurging(true));
+        byte[] data = new byte[512];
         try {
-            // new the MemoryFile instance
-            mMemoryFile = new MemoryFile("Test File", 1024);
-
-            assertFalse(mMemoryFile.allowPurging(true));
-            byte[] data = new byte[512];
-            try {
-                mMemoryFile.writeBytes(data, 0, 0, 512);
-                // TODO: a bug?
-//                fail("Exception should be throw out when mAllowPurging is true");
-            } catch (Exception e) {
-            }
-
-            assertTrue(mMemoryFile.isPurgingAllowed());
-            assertTrue(mMemoryFile.allowPurging(false));
-            try {
-                mMemoryFile.writeBytes(data, 0, 0, 512);
-            } catch (Exception e) {
-                fail("Exception should not be throw out when mAllowPurging is false");
-            }
-            assertFalse(mMemoryFile.isPurgingAllowed());
+            mMemoryFile.writeBytes(data, 0, 0, 512);
         } catch (IOException e) {
-            fail("MemoryFile should not throw IOException here");
+            // IOException may be thrown here since purging is allowed
         }
+
+        assertTrue(mMemoryFile.isPurgingAllowed());
+        assertTrue(mMemoryFile.allowPurging(false));
+        mMemoryFile.writeBytes(data, 0, 0, 512);
+        assertFalse(mMemoryFile.isPurgingAllowed());
     }
 
     @TestTargetNew(
@@ -188,34 +158,18 @@
         method = "length",
         args = {}
     )
-    public void testLength() {
-        try {
-            mMemoryFile = new MemoryFile("Test File", 1024);
-            assertEquals(1024, mMemoryFile.length());
-        } catch (IOException e) {
-            fail(e.getMessage());
-        }
+    public void testLength() throws IOException {
+        mMemoryFile = new MemoryFile("Test File", 1024);
+        assertEquals(1024, mMemoryFile.length());
 
-        try {
-            mMemoryFile = new MemoryFile("Test File", 512);
-            assertEquals(512, mMemoryFile.length());
-        } catch (IOException e) {
-            fail(e.getMessage());
-        }
+        mMemoryFile = new MemoryFile("Test File", 512);
+        assertEquals(512, mMemoryFile.length());
 
-        try {
-            mMemoryFile = new MemoryFile("Test File", Integer.MAX_VALUE);
-            assertEquals(Integer.MAX_VALUE, mMemoryFile.length());
-        } catch (IOException e) {
-            fail(e.getMessage());
-        }
+        mMemoryFile = new MemoryFile("Test File", Integer.MAX_VALUE);
+        assertEquals(Integer.MAX_VALUE, mMemoryFile.length());
 
-        try {
-            mMemoryFile = new MemoryFile("Test File", Integer.MIN_VALUE);
-            assertEquals(Integer.MIN_VALUE, mMemoryFile.length());
-        } catch (IOException e) {
-            fail(e.getMessage());
-        }
+        mMemoryFile = new MemoryFile("Test File", Integer.MIN_VALUE);
+        assertEquals(Integer.MIN_VALUE, mMemoryFile.length());
     }
 
     @TestTargetNew(
@@ -224,20 +178,16 @@
         method = "readBytes",
         args = {byte[].class, int.class, int.class, int.class}
     )
-    public void testReadBytes() {
-        try {
-            // new the MemoryFile instance
-            mMemoryFile = new MemoryFile("Test File", 1024);
+    public void testReadBytes() throws IOException {
+        // new the MemoryFile instance
+        mMemoryFile = new MemoryFile("Test File", 1024);
 
-            byte[] data = new byte[] { 1, 2, 3, 4 };
-            mMemoryFile.writeBytes(data, 0, 0, data.length);
-            byte[] gotData = new byte[4];
-            mMemoryFile.readBytes(gotData, 0, 0, gotData.length);
-            for (int i = 0; i < gotData.length; i++) {
-                assertEquals(i + 1, gotData[i]);
-            }
-        } catch (IOException e) {
-            fail(e.getMessage());
+        byte[] data = new byte[] { 1, 2, 3, 4 };
+        mMemoryFile.writeBytes(data, 0, 0, data.length);
+        byte[] gotData = new byte[4];
+        mMemoryFile.readBytes(gotData, 0, 0, gotData.length);
+        for (int i = 0; i < gotData.length; i++) {
+            assertEquals(i + 1, gotData[i]);
         }
 
         checkReadBytesInIllegalParameter(0, -1, 128);
@@ -253,14 +203,14 @@
         checkReadBytesInIllegalParameter(2000, 0, 512);
     }
 
-    private void checkReadBytesInIllegalParameter(int srcOffset, int destOffset, int count) {
+    private void checkReadBytesInIllegalParameter(int srcOffset, int destOffset, int count)
+            throws IOException {
         try {
             byte[] data = new byte[512];
             mMemoryFile.readBytes(data, srcOffset, destOffset, count);
-            fail("MemoryFile would throw IndexOutOfBoundsException here.");
+            fail("MemoryFile should throw IndexOutOfBoundsException here.");
         } catch (IndexOutOfBoundsException e) {
-        } catch (IOException e) {
-            fail("MemoryFile would not throw IOException here.");
+            // expected
         }
     }
 
@@ -270,30 +220,20 @@
         method = "close",
         args = {}
     )
-    @ToBeFixed(bug="1398215", explanation="the file still can be read even after it closes.")
-    public void testClose() {
-        try {
-            // new the MemoryFile instance
-            mMemoryFile = new MemoryFile("Test File", 1024);
-            byte[] data = new byte[512];
-            mMemoryFile.writeBytes(data, 0, 0, 128);
-        } catch (IndexOutOfBoundsException e) {
-            fail("MemoryFile would not throw IndexOutOfBoundsException here.");
-        } catch (IOException e) {
-            fail("MemoryFile would not throw IOException here.");
-        }
+    public void testClose() throws IOException {
+        // new the MemoryFile instance
+        mMemoryFile = new MemoryFile("Test File", 1024);
+        byte[] data = new byte[512];
+        mMemoryFile.writeBytes(data, 0, 0, 128);
 
         mMemoryFile.close();
 
+        data = new byte[512];
         try {
-            byte[] data = new byte[512];
-            // the file has been closed, we should not read any data from
-            // this file, but we still could read 128 bytes, this maybe a bug.
-            assertEquals(128, mMemoryFile.readBytes(data, 0, 0, 128));
-        } catch (IndexOutOfBoundsException e) {
-            fail("MemoryFile would not throw IndexOutOfBoundsException here.");
+            mMemoryFile.readBytes(data, 0, 0, 128);
+            fail("Reading from closed MemoryFile did not throw an exception.");
         } catch (IOException e) {
-            fail("MemoryFile would not throw IOException here.");
+            // expected, since file is already closed
         }
     }