diff options
author | maxbogue <maxbogue@chromium.org> | 2015-11-25 10:47:47 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-11-25 18:49:06 +0000 |
commit | 8879c45d2b58fb2b40417cbe54309fa308646cf9 (patch) | |
tree | d52ec7eb8ecb6515465b8de0303635937395819d /sync | |
parent | 0cafe7ecc2c99f9cd7c9392f56814eb83d7eb63d (diff) | |
download | chromium_src-8879c45d2b58fb2b40417cbe54309fa308646cf9.zip chromium_src-8879c45d2b58fb2b40417cbe54309fa308646cf9.tar.gz chromium_src-8879c45d2b58fb2b40417cbe54309fa308646cf9.tar.bz2 |
[Sync] Remove deprecated AccountManagerHelper calls in sync code.
This change also introduces the SimpleFuture class to help writing tests that need
to wait for the result of asynchronous computations.
BUG=517697
Review URL: https://codereview.chromium.org/1409123011
Cr-Commit-Position: refs/heads/master@{#361708}
Diffstat (limited to 'sync')
3 files changed, 104 insertions, 15 deletions
diff --git a/sync/android/java/src/org/chromium/sync/signin/AccountManagerHelper.java b/sync/android/java/src/org/chromium/sync/signin/AccountManagerHelper.java index dc3cd94..fa72690 100644 --- a/sync/android/java/src/org/chromium/sync/signin/AccountManagerHelper.java +++ b/sync/android/java/src/org/chromium/sync/signin/AccountManagerHelper.java @@ -277,6 +277,8 @@ public class AccountManagerHelper { /** * Asynchronously returns whether an account exists with the given name. */ + // TODO(maxbogue): Remove once this function is used outside of tests. + @VisibleForTesting public void hasAccountForName(String accountName, final Callback<Boolean> callback) { getAccountFromName(accountName, new Callback<Account>() { @Override diff --git a/sync/android/javatests/src/org/chromium/sync/notifier/signin/AccountManagerHelperTest.java b/sync/android/javatests/src/org/chromium/sync/notifier/signin/AccountManagerHelperTest.java index 90e1522..23220ec 100644 --- a/sync/android/javatests/src/org/chromium/sync/notifier/signin/AccountManagerHelperTest.java +++ b/sync/android/javatests/src/org/chromium/sync/notifier/signin/AccountManagerHelperTest.java @@ -12,6 +12,7 @@ import android.test.suitebuilder.annotation.SmallTest; import org.chromium.sync.signin.AccountManagerHelper; import org.chromium.sync.test.util.AccountHolder; import org.chromium.sync.test.util.MockAccountManager; +import org.chromium.sync.test.util.SimpleFuture; /** * Test class for {@link AccountManagerHelper}. @@ -31,30 +32,36 @@ public class AccountManagerHelperTest extends InstrumentationTestCase { mHelper = AccountManagerHelper.get(context); } - private Account addTestAccount(String accountName, String password) { - Account account = AccountManagerHelper.createAccountFromName(accountName); - AccountHolder.Builder accountHolder = - AccountHolder.create().account(account).password(password).alwaysAccept(true); - mAccountManager.addAccountHolderExplicitly(accountHolder.build()); - return account; - } - @SmallTest public void testCanonicalAccount() throws InterruptedException { addTestAccount("test@gmail.com", "password"); - assertTrue(mHelper.hasAccountForName("test@gmail.com")); - assertTrue(mHelper.hasAccountForName("Test@gmail.com")); - assertTrue(mHelper.hasAccountForName("te.st@gmail.com")); + assertTrue(hasAccountForName("test@gmail.com")); + assertTrue(hasAccountForName("Test@gmail.com")); + assertTrue(hasAccountForName("te.st@gmail.com")); } @SmallTest public void testNonCanonicalAccount() throws InterruptedException { addTestAccount("test.me@gmail.com", "password"); - assertTrue(mHelper.hasAccountForName("test.me@gmail.com")); - assertTrue(mHelper.hasAccountForName("testme@gmail.com")); - assertTrue(mHelper.hasAccountForName("Testme@gmail.com")); - assertTrue(mHelper.hasAccountForName("te.st.me@gmail.com")); + assertTrue(hasAccountForName("test.me@gmail.com")); + assertTrue(hasAccountForName("testme@gmail.com")); + assertTrue(hasAccountForName("Testme@gmail.com")); + assertTrue(hasAccountForName("te.st.me@gmail.com")); + } + + private Account addTestAccount(String accountName, String password) { + Account account = AccountManagerHelper.createAccountFromName(accountName); + AccountHolder.Builder accountHolder = + AccountHolder.create().account(account).password(password).alwaysAccept(true); + mAccountManager.addAccountHolderExplicitly(accountHolder.build()); + return account; + } + + private boolean hasAccountForName(String accountName) throws InterruptedException { + SimpleFuture<Boolean> result = new SimpleFuture<Boolean>(); + mHelper.hasAccountForName(accountName, result.createCallback()); + return result.get(); } } diff --git a/sync/test/android/javatests/src/org/chromium/sync/test/util/SimpleFuture.java b/sync/test/android/javatests/src/org/chromium/sync/test/util/SimpleFuture.java new file mode 100644 index 0000000..1cee25b --- /dev/null +++ b/sync/test/android/javatests/src/org/chromium/sync/test/util/SimpleFuture.java @@ -0,0 +1,80 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.sync.test.util; + +import org.chromium.base.Callback; + +/** + * A simple tool to make waiting for the result of an asynchronous operation easy. + * + * This class is thread-safe; the result can be provided and retrieved on any thread. + * + * Example usage: + * + * final SimpleFuture<Integer> future = new SimpleFuture<Integer>(); + * getValueAsynchronously(new Callback<Integer>() { + * public void onResult(Integer result) { + * // Do some other work... + * future.provide(result); + * } + * } + * int value = future.get(); + * + * Or, if your callback doesn't need to do anything but provide the value: + * + * SimpleFuture<Integer> result = new SimpleFuture<Integer>(); + * getValueAsynchronously(result.createCallback()); + * int value = result.get(); + * + * @param <V> The type of the value this future will return. + */ +public class SimpleFuture<V> { + private static final int GET_TIMEOUT_MS = 10000; + + private final Object mLock = new Object(); + private boolean mHasResult = false; + private V mResult; + + /** + * Provide the result value of this future for get() to return. + * + * Any calls after the first are ignored. + */ + public void provide(V result) { + synchronized (mLock) { + if (mHasResult) { + // You can only provide a result once. + return; + } + mHasResult = true; + mResult = result; + mLock.notifyAll(); + } + } + + /** + * Get the value of this future, or block until it's available. + */ + public V get() throws InterruptedException { + synchronized (mLock) { + while (!mHasResult) { + mLock.wait(GET_TIMEOUT_MS); + } + return mResult; + } + } + + /** + * Helper function to create a {@link Callback} that will provide its result. + */ + public Callback<V> createCallback() { + return new Callback<V>() { + @Override + public void onResult(V result) { + provide(result); + } + }; + } +} |