| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.blimp.auth; | |
| 6 | |
| 7 import android.content.Intent; | |
| 8 import android.os.Handler; | |
| 9 import android.os.Message; | |
| 10 | |
| 11 import com.google.android.gms.common.ConnectionResult; | |
| 12 | |
| 13 import junit.framework.Assert; | |
| 14 | |
| 15 /** | |
| 16 * Test implementation of a TokenSource. Has the following features: | |
| 17 * - Can return a specific token. | |
| 18 * - Can mimic a specific number of transient failures before a success or unrec
overable failure. | |
| 19 * - Can mimic an unrecoverable failure. | |
| 20 */ | |
| 21 public class MockTokenSource extends Handler implements TokenSource { | |
| 22 private static final int MSG_QUERY_TOKEN = 1; | |
| 23 | |
| 24 private final String mCorrectToken; | |
| 25 | |
| 26 /** Whether or not to fail in a non-transient way after all transient failur
es. */ | |
| 27 private final boolean mFailHard; | |
| 28 | |
| 29 private TokenSource.Callback mCallback; | |
| 30 | |
| 31 /** | |
| 32 * The number of transient failures left to simulate before a successful or
non-transient | |
| 33 * failure is reported. | |
| 34 */ | |
| 35 private int mTransientFailuresLeft; | |
| 36 | |
| 37 /** Whether or not a non-transient failure has already been reported. */ | |
| 38 private boolean mAlreadyFailedHard; | |
| 39 | |
| 40 /** | |
| 41 * @param correctToken The token to return on success. | |
| 42 * @param transientFailureCount The number of transient failures to emit. | |
| 43 * @param failsHardAfter Whether or not to show a non-transient failu
re or successfully | |
| 44 * return the token after all transient failure
s. | |
| 45 */ | |
| 46 public MockTokenSource(String correctToken, int transientFailureCount, boole
an failsHardAfter) { | |
| 47 mCorrectToken = correctToken; | |
| 48 mTransientFailuresLeft = transientFailureCount; | |
| 49 mFailHard = failsHardAfter; | |
| 50 } | |
| 51 | |
| 52 // TokenSource implementation. | |
| 53 @Override | |
| 54 public void destroy() {} | |
| 55 | |
| 56 @Override | |
| 57 public void setCallback(TokenSource.Callback callback) { | |
| 58 mCallback = callback; | |
| 59 } | |
| 60 | |
| 61 @Override | |
| 62 public void getToken() { | |
| 63 Assert.assertFalse("getToken() called after already returning a successf
ul token.", | |
| 64 isRetrievingToken()); | |
| 65 Assert.assertFalse("getToken() called after failing in an unrecoverable
way.", | |
| 66 mAlreadyFailedHard); | |
| 67 sendEmptyMessage(MSG_QUERY_TOKEN); | |
| 68 } | |
| 69 | |
| 70 @Override | |
| 71 public boolean isRetrievingToken() { | |
| 72 return hasMessages(MSG_QUERY_TOKEN); | |
| 73 } | |
| 74 | |
| 75 @Override | |
| 76 public int tokenIsInvalid(String token) { | |
| 77 return ConnectionResult.SUCCESS; | |
| 78 } | |
| 79 | |
| 80 @Override | |
| 81 public void onAccountSelected(Intent data) {} | |
| 82 | |
| 83 // Handler overrides. | |
| 84 @Override | |
| 85 public final void handleMessage(Message msg) { | |
| 86 if (msg.what != MSG_QUERY_TOKEN) return; | |
| 87 if (mTransientFailuresLeft > 0) { | |
| 88 mCallback.onTokenUnavailable(true); | |
| 89 } else if (mFailHard) { | |
| 90 mAlreadyFailedHard = true; | |
| 91 mCallback.onTokenUnavailable(false); | |
| 92 } else { | |
| 93 mCallback.onTokenReceived(mCorrectToken); | |
| 94 } | |
| 95 --mTransientFailuresLeft; | |
| 96 } | |
| 97 } | |
| OLD | NEW |