mobile/android/tests/background/junit3/src/healthreport/upload/TestAndroidSubmissionClient.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 package org.mozilla.gecko.background.healthreport.upload;
michael@0 5
michael@0 6 import java.util.Collection;
michael@0 7
michael@0 8 import org.mozilla.gecko.background.bagheera.BagheeraRequestDelegate;
michael@0 9 import org.mozilla.gecko.background.healthreport.Environment;
michael@0 10 import org.mozilla.gecko.background.healthreport.HealthReportStorage;
michael@0 11 import org.mozilla.gecko.background.healthreport.HealthReportDatabaseStorage;
michael@0 12 import org.mozilla.gecko.background.healthreport.MockHealthReportDatabaseStorage.PrepopulatedMockHealthReportDatabaseStorage;
michael@0 13 import org.mozilla.gecko.background.healthreport.MockProfileInformationCache;
michael@0 14 import org.mozilla.gecko.background.healthreport.ProfileInformationCache;
michael@0 15 import org.mozilla.gecko.background.healthreport.upload.AndroidSubmissionClient;
michael@0 16 import org.mozilla.gecko.background.healthreport.upload.AndroidSubmissionClient.SubmissionsFieldName;
michael@0 17 import org.mozilla.gecko.background.helpers.FakeProfileTestCase;
michael@0 18 import org.mozilla.gecko.background.testhelpers.StubDelegate;
michael@0 19
michael@0 20 import android.content.ContentProviderClient;
michael@0 21 import android.content.Context;
michael@0 22 import android.content.SharedPreferences;
michael@0 23 import org.json.JSONException;
michael@0 24 import org.json.JSONObject;
michael@0 25
michael@0 26 public class TestAndroidSubmissionClient extends FakeProfileTestCase {
michael@0 27 public static class MockAndroidSubmissionClient extends AndroidSubmissionClient {
michael@0 28 protected final PrepopulatedMockHealthReportDatabaseStorage storage;
michael@0 29
michael@0 30 public SubmissionState submissionState = SubmissionState.SUCCESS;
michael@0 31 public DocumentStatus documentStatus = DocumentStatus.VALID;
michael@0 32 public boolean hasUploadBeenRequested = true;
michael@0 33
michael@0 34 public MockAndroidSubmissionClient(final Context context, final SharedPreferences sharedPrefs,
michael@0 35 final PrepopulatedMockHealthReportDatabaseStorage storage) {
michael@0 36 super(context, sharedPrefs, "profilePath");
michael@0 37 this.storage = storage;
michael@0 38 }
michael@0 39
michael@0 40 @Override
michael@0 41 public HealthReportDatabaseStorage getStorage(final ContentProviderClient client) {
michael@0 42 return storage;
michael@0 43 }
michael@0 44
michael@0 45 @Override
michael@0 46 public boolean hasUploadBeenRequested() {
michael@0 47 return hasUploadBeenRequested;
michael@0 48 }
michael@0 49
michael@0 50 @Override
michael@0 51 protected void uploadPayload(String id, String payload, Collection<String> oldIds,
michael@0 52 BagheeraRequestDelegate delegate) {
michael@0 53 switch (submissionState) {
michael@0 54 case SUCCESS:
michael@0 55 delegate.handleSuccess(0, null, id, null);
michael@0 56 break;
michael@0 57
michael@0 58 case FAILURE:
michael@0 59 delegate.handleFailure(0, null, null);
michael@0 60 break;
michael@0 61
michael@0 62 case ERROR:
michael@0 63 delegate.handleError(null);
michael@0 64 break;
michael@0 65
michael@0 66 default:
michael@0 67 throw new IllegalStateException("Unknown submissionState, " + submissionState);
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 @Override
michael@0 72 public SubmissionsTracker getSubmissionsTracker(final HealthReportStorage storage,
michael@0 73 final long localTime, final boolean hasUploadBeenRequested) {
michael@0 74 return new MockSubmissionsTracker(storage, localTime, hasUploadBeenRequested);
michael@0 75 }
michael@0 76
michael@0 77 public class MockSubmissionsTracker extends SubmissionsTracker {
michael@0 78 public MockSubmissionsTracker (final HealthReportStorage storage, final long localTime,
michael@0 79 final boolean hasUploadBeenRequested) {
michael@0 80 super(storage, localTime, hasUploadBeenRequested);
michael@0 81 }
michael@0 82
michael@0 83 @Override
michael@0 84 public ProfileInformationCache getProfileInformationCache() {
michael@0 85 final MockProfileInformationCache cache = new MockProfileInformationCache(profilePath);
michael@0 86 cache.setInitialized(true); // Will throw errors otherwise.
michael@0 87 return cache;
michael@0 88 }
michael@0 89
michael@0 90 @Override
michael@0 91 public TrackingGenerator getGenerator() {
michael@0 92 return new MockTrackingGenerator();
michael@0 93 }
michael@0 94
michael@0 95 public class MockTrackingGenerator extends TrackingGenerator {
michael@0 96 @Override
michael@0 97 public JSONObject generateDocument(final long localTime, final long last,
michael@0 98 final String profilePath) throws JSONException {
michael@0 99 switch (documentStatus) {
michael@0 100 case VALID:
michael@0 101 return new JSONObject(); // Beyond == null, we don't check for valid FHR documents.
michael@0 102
michael@0 103 case NULL:
michael@0 104 // The overridden method should return null since we return a null has for the current
michael@0 105 // Environment.
michael@0 106 return super.generateDocument(localTime, last, profilePath);
michael@0 107
michael@0 108 case EXCEPTION:
michael@0 109 throw new IllegalStateException("Intended Exception");
michael@0 110
michael@0 111 default:
michael@0 112 throw new IllegalStateException("Unintended Exception");
michael@0 113 }
michael@0 114 }
michael@0 115
michael@0 116 // Used in super.generateDocument, where a null document is returned if getHash returns null
michael@0 117 @Override
michael@0 118 public Environment getCurrentEnvironment() {
michael@0 119 return new Environment() {
michael@0 120 @Override
michael@0 121 public int register() {
michael@0 122 return 0;
michael@0 123 }
michael@0 124
michael@0 125 @Override
michael@0 126 public String getHash() {
michael@0 127 return null;
michael@0 128 }
michael@0 129 };
michael@0 130 }
michael@0 131 }
michael@0 132 }
michael@0 133 }
michael@0 134
michael@0 135 public final SubmissionsFieldName[] SUBMISSIONS_STATUS_FIELD_NAMES = new SubmissionsFieldName[] {
michael@0 136 SubmissionsFieldName.SUCCESS,
michael@0 137 SubmissionsFieldName.CLIENT_FAILURE,
michael@0 138 SubmissionsFieldName.TRANSPORT_FAILURE,
michael@0 139 SubmissionsFieldName.SERVER_FAILURE
michael@0 140 };
michael@0 141
michael@0 142 public static enum SubmissionState { SUCCESS, FAILURE, ERROR }
michael@0 143 public static enum DocumentStatus { VALID, NULL, EXCEPTION };
michael@0 144
michael@0 145 public StubDelegate stubDelegate;
michael@0 146 public PrepopulatedMockHealthReportDatabaseStorage storage;
michael@0 147 public MockAndroidSubmissionClient client;
michael@0 148
michael@0 149 public void setUp() throws Exception {
michael@0 150 super.setUp();
michael@0 151 stubDelegate = new StubDelegate();
michael@0 152 storage = new PrepopulatedMockHealthReportDatabaseStorage(context, fakeProfileDirectory);
michael@0 153 client = new MockAndroidSubmissionClient(context, getSharedPreferences(), storage);
michael@0 154 }
michael@0 155
michael@0 156 public int getSubmissionsCount(final SubmissionsFieldName fieldName) {
michael@0 157 final int id = fieldName.getID(storage);
michael@0 158 return storage.getIntFromQuery("SELECT COUNT(*) FROM events WHERE field = " + id, null);
michael@0 159 }
michael@0 160
michael@0 161 public void testUploadSubmissionsFirstAttemptCount() throws Exception {
michael@0 162 client.hasUploadBeenRequested = false;
michael@0 163 client.upload(storage.now, null, null, stubDelegate);
michael@0 164 assertEquals(1, getSubmissionsCount(SubmissionsFieldName.FIRST_ATTEMPT));
michael@0 165 assertEquals(0, getSubmissionsCount(SubmissionsFieldName.CONTINUATION_ATTEMPT));
michael@0 166 }
michael@0 167
michael@0 168 public void testUploadSubmissionsContinuationAttemptCount() throws Exception {
michael@0 169 client.upload(storage.now, null, null, stubDelegate);
michael@0 170 assertEquals(0, getSubmissionsCount(SubmissionsFieldName.FIRST_ATTEMPT));
michael@0 171 assertEquals(1, getSubmissionsCount(SubmissionsFieldName.CONTINUATION_ATTEMPT));
michael@0 172 }
michael@0 173
michael@0 174 /**
michael@0 175 * Asserts that the given field name has a count of 1 and all other status (success and failures)
michael@0 176 * have a count of 0.
michael@0 177 */
michael@0 178 public void assertStatusCount(final SubmissionsFieldName fieldName) {
michael@0 179 for (SubmissionsFieldName name : SUBMISSIONS_STATUS_FIELD_NAMES) {
michael@0 180 if (name == fieldName) {
michael@0 181 assertEquals(1, getSubmissionsCount(name));
michael@0 182 } else {
michael@0 183 assertEquals(0, getSubmissionsCount(name));
michael@0 184 }
michael@0 185 }
michael@0 186 }
michael@0 187
michael@0 188 public void testUploadSubmissionsSuccessCount() throws Exception {
michael@0 189 client.upload(storage.now, null, null, stubDelegate);
michael@0 190 assertStatusCount(SubmissionsFieldName.SUCCESS);
michael@0 191 }
michael@0 192
michael@0 193 public void testUploadNullDocumentSubmissionsFailureCount() throws Exception {
michael@0 194 client.documentStatus = DocumentStatus.NULL;
michael@0 195 client.upload(storage.now, null, null, stubDelegate);
michael@0 196 assertStatusCount(SubmissionsFieldName.CLIENT_FAILURE);
michael@0 197 }
michael@0 198
michael@0 199 public void testUploadDocumentGenerationExceptionSubmissionsFailureCount() throws Exception {
michael@0 200 client.documentStatus = DocumentStatus.EXCEPTION;
michael@0 201 client.upload(storage.now, null, null, stubDelegate);
michael@0 202 assertStatusCount(SubmissionsFieldName.CLIENT_FAILURE);
michael@0 203 }
michael@0 204 }

mercurial