1 package org.robolectric.shadows; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static java.nio.charset.StandardCharsets.UTF_8; 5 import static org.robolectric.Shadows.shadowOf; 6 7 import android.content.Context; 8 import android.os.DropBoxManager; 9 import android.os.DropBoxManager.Entry; 10 import androidx.test.core.app.ApplicationProvider; 11 import androidx.test.ext.junit.runners.AndroidJUnit4; 12 import java.io.BufferedReader; 13 import java.io.InputStreamReader; 14 import org.junit.Before; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 18 /** Unit tests for {@see ShadowDropboxManager}. */ 19 @RunWith(AndroidJUnit4.class) 20 public class ShadowDropBoxManagerTest { 21 22 private static final String TAG = "TAG"; 23 private static final String ANOTHER_TAG = "ANOTHER_TAG"; 24 private static final byte[] DATA = "HELLO WORLD".getBytes(UTF_8); 25 26 private DropBoxManager manager; 27 private ShadowDropBoxManager shadowDropBoxManager; 28 29 @Before 30 public void setup() { 31 manager = 32 (DropBoxManager) 33 ApplicationProvider.getApplicationContext().getSystemService(Context.DROPBOX_SERVICE); 34 shadowDropBoxManager = shadowOf(manager); 35 } 36 37 @Test 38 public void emptyDropbox() { 39 assertThat(manager.getNextEntry(null, 0)).isNull(); 40 } 41 42 @Test 43 public void dataExpected() throws Exception { 44 shadowDropBoxManager.addData(TAG, 1, DATA); 45 46 Entry entry = manager.getNextEntry(null, 0); 47 assertThat(entry).isNotNull(); 48 assertThat(entry.getTag()).isEqualTo(TAG); 49 assertThat(entry.getTimeMillis()).isEqualTo(1); 50 assertThat(new BufferedReader(new InputStreamReader(entry.getInputStream(), UTF_8)).readLine()) 51 .isEqualTo(new String(DATA)); 52 assertThat(entry.getText(100)).isEqualTo(new String(DATA, UTF_8)); 53 } 54 55 /** Checks that we retrieve the first entry <em>after</em> the specified time. */ 56 @Test 57 public void dataNotExpected_timestampSameAsEntry() throws Exception { 58 shadowDropBoxManager.addData(TAG, 1, DATA); 59 60 assertThat(manager.getNextEntry(null, 1)).isNull(); 61 } 62 63 @Test 64 public void dataNotExpected_timestampAfterEntry() throws Exception { 65 shadowDropBoxManager.addData(TAG, 1, DATA); 66 67 assertThat(manager.getNextEntry(null, 2)).isNull(); 68 } 69 70 @Test 71 public void dataNotExpected_wrongTag() throws Exception { 72 shadowDropBoxManager.addData(TAG, 1, DATA); 73 74 assertThat(manager.getNextEntry(ANOTHER_TAG, 0)).isNull(); 75 } 76 77 @Test 78 public void dataExpectedWithSort() throws Exception { 79 shadowDropBoxManager.addData(TAG, 3, DATA); 80 shadowDropBoxManager.addData(TAG, 1, new byte[] {(byte) 0x0}); 81 82 Entry entry = manager.getNextEntry(null, 2); 83 assertThat(entry).isNotNull(); 84 assertThat(entry.getText(100)).isEqualTo(new String(DATA, UTF_8)); 85 assertThat(entry.getTimeMillis()).isEqualTo(3); 86 } 87 88 @Test() 89 public void resetClearsData() throws Exception { 90 shadowDropBoxManager.addData(TAG, 1, DATA); 91 92 shadowDropBoxManager.reset(); 93 94 assertThat(manager.getNextEntry(null, 0)).isNull(); 95 } 96 } 97