1 package com.xtremelabs.robolectric.shadows;
2 
3 import com.xtremelabs.robolectric.internal.Implementation;
4 import com.xtremelabs.robolectric.internal.Implements;
5 import com.xtremelabs.robolectric.internal.RealObject;
6 
7 import android.content.SyncResult;
8 import android.content.SyncStats;
9 
10 import java.lang.reflect.Field;
11 
12 @Implements(SyncResult.class)
13 public class ShadowSyncResult {
14     @RealObject
15     private SyncResult result;
16 
__constructor__()17     public void __constructor__() {
18         try {
19             Field f = SyncResult.class.getDeclaredField("stats");
20             f.setAccessible(true);
21             f.set(result, new SyncStats());
22         } catch (NoSuchFieldException e) {
23             throw new RuntimeException(e);
24         } catch (IllegalAccessException e) {
25             throw new RuntimeException(e);
26         }
27     }
28 
29     @Implementation
hasSoftError()30     public boolean hasSoftError() {
31         return result.syncAlreadyInProgress || result.stats.numIoExceptions > 0;
32     }
33 
34     @Implementation
hasHardError()35     public boolean hasHardError() {
36         return result.stats.numParseExceptions > 0
37                 || result.stats.numConflictDetectedExceptions > 0
38                 || result.stats.numAuthExceptions > 0
39                 || result.tooManyDeletions
40                 || result.tooManyRetries
41                 || result.databaseError;
42     }
43 
44     @Implementation
hasError()45     public boolean hasError() {
46         return hasSoftError() || hasHardError();
47     }
48 
49     @Implementation
madeSomeProgress()50     public boolean madeSomeProgress() {
51         return ((result.stats.numDeletes > 0) && !result.tooManyDeletions)
52                 || result.stats.numInserts > 0
53                 || result.stats.numUpdates > 0;
54     }
55 
56     @Implementation
clear()57     public void clear() {
58         if (result.syncAlreadyInProgress) {
59             throw new UnsupportedOperationException(
60                     "you are not allowed to clear the ALREADY_IN_PROGRESS SyncStats");
61         }
62         result.tooManyDeletions = false;
63         result.tooManyRetries = false;
64         result.databaseError = false;
65         result.fullSyncRequested = false;
66         result.partialSyncUnavailable = false;
67         result.moreRecordsToGet = false;
68         result.delayUntil = 0;
69         result.stats.clear();
70     }
71 
72 
73     @Implements(SyncStats.class)
74     public static class ShadowSyncStats {
75         @RealObject
76         private SyncStats stats;
77 
78         @Implementation
clear()79         public void clear() {
80             stats.numAuthExceptions = 0;
81             stats.numIoExceptions = 0;
82             stats.numParseExceptions = 0;
83             stats.numConflictDetectedExceptions = 0;
84             stats.numInserts = 0;
85             stats.numUpdates = 0;
86             stats.numDeletes = 0;
87             stats.numEntries = 0;
88             stats.numSkippedEntries = 0;
89         }
90     }
91 }
92