Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import android.os.DropBoxManager;
      4 import android.os.DropBoxManager.Entry;
      5 import java.util.SortedMap;
      6 import java.util.TreeMap;
      7 import org.robolectric.annotation.Implementation;
      8 import org.robolectric.annotation.Implements;
      9 
     10 /** Fake dropbox manager that starts with no entries. */
     11 @Implements(value = DropBoxManager.class)
     12 public class ShadowDropBoxManager {
     13   private final SortedMap<Long, Entry> entries = new TreeMap<>();
     14 
     15   public ShadowDropBoxManager() {
     16     reset();
     17   }
     18 
     19   /**
     20    * Adds entry to the DropboxManager with the flag indicating data is text.
     21    *
     22    * <p>The existing {@link DropBoxManager#addData}, {@link DropBoxManager#addFile}, and {@link
     23    * DropBoxManager#addText} methods in DropBoxManager are not shadowed. This method is a
     24    * convenience for quickly adding multiple historical entries. The entries can be added in any
     25    * order since this shadow will sort the entries by the specified timestamp.
     26    *
     27    * <p>The flag will be set to {@link DropBoxManager#IS_TEXT} so that {@link
     28    * DropBoxManager.Entry#getText} can be used.
     29    *
     30    * @param tag can be any arbitrary string
     31    * @param timestamp is an arbitrary timestamp that must be unique from all other entries
     32    * @param data must not be null
     33    */
     34   void addData(String tag, long timestamp, byte[] data) {
     35     entries.put(timestamp, new DropBoxManager.Entry(tag, timestamp, data, DropBoxManager.IS_TEXT));
     36   }
     37 
     38   /**
     39    * Clears all entries.
     40    */
     41   public void reset() {
     42     entries.clear();
     43   }
     44 
     45   @Implementation
     46   protected DropBoxManager.Entry getNextEntry(String tag, long millis) {
     47     for (DropBoxManager.Entry entry : entries.tailMap(millis).values()) {
     48       if ((tag != null && !entry.getTag().equals(tag)) || entry.getTimeMillis() <= millis) {
     49         continue;
     50       }
     51       return entry;
     52     }
     53     return null;
     54   }
     55 }
     56