package org.robolectric.shadows;

import android.os.ParcelFileDescriptor;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.TestRunners;

import java.io.File;
import java.io.FileOutputStream;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(TestRunners.MultiApiWithDefaults.class)
public class ShadowParcelFileDescriptorTest {
  private File file;

  @Before
  public void setup() throws Exception {
    file = new File(RuntimeEnvironment.application.getFilesDir(), "test");
    FileOutputStream os = new FileOutputStream(file);
    os.close();
  }

  @Test
  public void testOpens() throws Exception {
    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, -1);
    assertThat(pfd).isNotNull();
    assertThat(pfd.getFileDescriptor().valid()).isTrue();
    pfd.close();
  }

  @Test
  public void testCloses() throws Exception {
    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, -1);
    pfd.close();
    assertThat(pfd.getFileDescriptor().valid()).isFalse();
  }

  @Test
  public void testAutoCloseInputStream() throws Exception {
    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, -1);
    ParcelFileDescriptor.AutoCloseInputStream is = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
    is.close();
    assertThat(pfd.getFileDescriptor().valid()).isFalse();
  }

  @Test
  public void testAutoCloseOutputStream() throws Exception {
    File f = new File(RuntimeEnvironment.application.getFilesDir(), "outfile");
    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(f, -1);
    ParcelFileDescriptor.AutoCloseOutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(pfd);
    os.close();
    assertThat(pfd.getFileDescriptor().valid()).isFalse();
  }
}

@@ -457,16 +457,45 @@
 le file;
+%0A  private File readOnlyFile;
 %0A%0A  @Bef
@@ -690,230 +690,1115 @@
 ;%0A  
-%7D%0A%0A  @Test%0A  public void testOpens() throws Exception %7B%0A    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, -1);%0A    assertThat(pfd).isNotNull();%0A    assertThat(pfd.getFileDescriptor().valid()).isTrue();%0A    pfd
+  readOnlyFile = new File(RuntimeEnvironment.application.getFilesDir(), %22test_readonly%22);%0A    os = new FileOutputStream(readOnlyFile);%0A    os.close();%0A    readOnlyFile.setReadOnly();%0A  %7D%0A%0A  @Test%0A  public void testOpens() throws Exception %7B%0A    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);%0A    assertThat(pfd).isNotNull();%0A    assertThat(pfd.getFileDescriptor().valid()).isTrue();%0A    pfd.close();%0A  %7D%0A%0A  @Test%0A  public void testOpens_readOnlyFile() throws Exception %7B%0A    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(readOnlyFile, ParcelFileDescriptor.MODE_READ_ONLY);%0A    assertThat(pfd).isNotNull();%0A    assertThat(pfd.getFileDescriptor().valid()).isTrue();%0A    pfd.close();%0A  %7D%0A%0A  @Test%0A  public void testOpens_canWriteWritableFile() throws Exception %7B%0A    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);%0A    assertThat(pfd).isNotNull();%0A    assertThat(pfd.getFileDescriptor().valid()).isTrue();%0A    FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor());%0A    os.write(5);%0A    os
 .clo

Add unit test.