package com.xtremelabs.robolectric.shadows;

import android.graphics.Typeface;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileWriter;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(WithTestDefaultsRunner.class)
public class ShadowTypefaceTest {
    @Test
    public void canAnswerAssetUsedDuringCreation() throws Exception {
        String fontFile = "assets/myfont.ttf";
        try {
            File assetsBase = shadowOf(Robolectric.application).getResourceLoader().getAssetsBase();
            FileWriter fileWriter = new FileWriter(new File(assetsBase, "myFont.ttf"));
            fileWriter.write("fontdata");
            fileWriter.close();
            Typeface typeface = Typeface.createFromAsset(Robolectric.application.getAssets(), "myFont.ttf");
            assertThat(shadowOf(typeface).getAssetPath(), equalTo("myFont.ttf"));
        } finally {
            new File(fontFile).delete();
        }
    }

    @Test(expected = RuntimeException.class)
    public void createFromAsset_throwsExceptionWhenFontNotFound() throws Exception {
        Typeface.createFromAsset(Robolectric.application.getAssets(), "nonexistent.ttf");
    }
}

@@ -177,16 +177,65 @@
 Runner;%0A
+import org.junit.After;%0Aimport org.junit.Before;%0A
 import o
@@ -569,142 +569,113 @@
     
-@Test%0A    public void canAnswerAssetUsedDuringCreation() throws Exception %7B%0A        String fontFile = %22assets/myfont.ttf%22;%0A        try
+private File assetsBase;%0A    private File fontFile;%0A%0A    @Before%0A    public void setup() throws Exception
  %7B%0A 
@@ -680,25 +680,16 @@
 %0A       
- 
-    File
  assetsB
@@ -761,32 +761,83 @@
 setsBase();%0A    
+    fontFile = new File(assetsBase, %22myFont.ttf%22);%0A
         FileWrit
@@ -871,49 +871,19 @@
 ter(
-new File(assetsBase, %22myFont.ttf%22));%0A    
+fontFile);%0A
     
@@ -912,28 +912,24 @@
 fontdata%22);%0A
-    
         file
@@ -944,24 +944,198 @@
 lose();%0A    
+%7D%0A%0A    @After%0A    public void teardown() throws Exception %7B%0A        fontFile.delete();%0A    %7D%0A%0A    @Test%0A    public void canAnswerAssetUsedDuringCreation() throws Exception %7B%0A
         Type
@@ -1231,28 +1231,24 @@
 %22);%0A        
-    
 assertThat(s
@@ -1309,79 +1309,8 @@
 ));%0A
-        %7D finally %7B%0A            new File(fontFile).delete();%0A        %7D%0A
     

Clean up temp files