Commit ec4490c0 authored by Boris Sazonov's avatar Boris Sazonov Committed by Commit Bot

[Signin][Android] Remove SimpleFuture from signin tests

This CL removes AccountManagerFacadeTest.hasAccountForName as the same
method in AccountManagerFacade can be invoked from any thread. It was
the last usage of org.chromium.components.signin.test.util.SimpleFuture,
it is removed as well.

Bug: 698258
Change-Id: I62271d79e469ae966ea500dbb058fb54baf36a54
Reviewed-on: https://chromium-review.googlesource.com/814514Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522476}
parent dfa4d7dd
......@@ -68,6 +68,5 @@ android_library("signin_java_test_support") {
java_files = [
"javatests/src/org/chromium/components/signin/test/util/AccountHolder.java",
"javatests/src/org/chromium/components/signin/test/util/FakeAccountManagerDelegate.java",
"javatests/src/org/chromium/components/signin/test/util/SimpleFuture.java",
]
}
......@@ -8,20 +8,17 @@ import android.accounts.Account;
import android.support.test.filters.SmallTest;
import android.support.test.rule.UiThreadTestRule;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.ProfileDataSource;
import org.chromium.components.signin.test.util.AccountHolder;
import org.chromium.components.signin.test.util.FakeAccountManagerDelegate;
import org.chromium.components.signin.test.util.SimpleFuture;
/**
* Test class for {@link AccountManagerFacade}.
......@@ -32,7 +29,7 @@ public class AccountManagerFacadeTest {
public UiThreadTestRule mRule = new UiThreadTestRule();
private FakeAccountManagerDelegate mDelegate;
private AccountManagerFacade mHelper;
private AccountManagerFacade mFacade;
@Before
public void setUp() throws Exception {
......@@ -41,12 +38,7 @@ public class AccountManagerFacadeTest {
Assert.assertFalse(mDelegate.isRegisterObserversCalled());
AccountManagerFacade.overrideAccountManagerFacadeForTests(mDelegate);
Assert.assertTrue(mDelegate.isRegisterObserversCalled());
mHelper = AccountManagerFacade.get();
}
@After
public void tearDown() throws Exception {
AccountManagerFacade.resetAccountManagerFacadeForTests();
mFacade = AccountManagerFacade.get();
}
@Test
......@@ -54,9 +46,9 @@ public class AccountManagerFacadeTest {
public void testCanonicalAccount() {
addTestAccount("test@gmail.com");
Assert.assertTrue(hasAccountForName("test@gmail.com"));
Assert.assertTrue(hasAccountForName("Test@gmail.com"));
Assert.assertTrue(hasAccountForName("te.st@gmail.com"));
Assert.assertTrue(mFacade.hasAccountForName("test@gmail.com"));
Assert.assertTrue(mFacade.hasAccountForName("Test@gmail.com"));
Assert.assertTrue(mFacade.hasAccountForName("te.st@gmail.com"));
}
// If this test starts flaking, please re-open crbug.com/568636 and make sure there is some sort
......@@ -66,10 +58,10 @@ public class AccountManagerFacadeTest {
public void testNonCanonicalAccount() {
addTestAccount("test.me@gmail.com");
Assert.assertTrue(hasAccountForName("test.me@gmail.com"));
Assert.assertTrue(hasAccountForName("testme@gmail.com"));
Assert.assertTrue(hasAccountForName("Testme@gmail.com"));
Assert.assertTrue(hasAccountForName("te.st.me@gmail.com"));
Assert.assertTrue(mFacade.hasAccountForName("test.me@gmail.com"));
Assert.assertTrue(mFacade.hasAccountForName("testme@gmail.com"));
Assert.assertTrue(mFacade.hasAccountForName("Testme@gmail.com"));
Assert.assertTrue(mFacade.hasAccountForName("te.st.me@gmail.com"));
}
@Test
......@@ -100,19 +92,4 @@ public class AccountManagerFacadeTest {
mDelegate.addAccountHolderBlocking(holder);
return account;
}
private boolean hasAccountForName(final String accountName) {
final SimpleFuture<Boolean> result = new SimpleFuture<Boolean>();
ThreadUtils.postOnUiThread(new Runnable() {
@Override
public void run() {
mHelper.hasAccountForName(accountName, result.createCallback());
}
});
try {
return result.get();
} catch (InterruptedException e) {
throw new RuntimeException("Exception occurred while waiting for future", e);
}
}
}
// 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.components.signin.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);
}
};
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment