This source file includes following definitions.
- getRealm
- getAccount
- getArgs
- notifyCalled
- getOnReceivedLoginRequestHelper
- onReceivedLoginRequest
- autoLoginTestHelper
- Feature
- testAutoLoginOnGoogleCom
- Feature
- testAutoLoginWithNullAccount
- Feature
- testAutoLoginOnNonGoogle
package org.chromium.android_webview.test;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Pair;
import org.chromium.android_webview.AwContents;
import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.util.CallbackHelper;
import org.chromium.net.test.util.TestWebServer;
import java.util.ArrayList;
import java.util.List;
public class AwContentsClientAutoLoginTest extends AwTestBase {
public static class OnReceivedLoginRequestHelper extends CallbackHelper {
String mRealm;
String mAccount;
String mArgs;
public String getRealm() {
assert getCallCount() > 0;
return mRealm;
}
public String getAccount() {
assert getCallCount() > 0;
return mAccount;
}
public String getArgs() {
assert getCallCount() > 0;
return mArgs;
}
public void notifyCalled(String realm, String account, String args) {
mRealm = realm;
mAccount = account;
mArgs = args;
notifyCalled();
}
}
private static class TestAwContentsClient
extends org.chromium.android_webview.test.TestAwContentsClient {
private OnReceivedLoginRequestHelper mOnReceivedLoginRequestHelper;
public TestAwContentsClient() {
mOnReceivedLoginRequestHelper = new OnReceivedLoginRequestHelper();
}
public OnReceivedLoginRequestHelper getOnReceivedLoginRequestHelper() {
return mOnReceivedLoginRequestHelper;
}
@Override
public void onReceivedLoginRequest(String realm, String account, String args) {
getOnReceivedLoginRequestHelper().notifyCalled(realm, account, args);
}
}
private TestAwContentsClient mContentsClient = new TestAwContentsClient();
private void autoLoginTestHelper(final String testName, final String xAutoLoginHeader,
final String expectedRealm, final String expectedAccount, final String expectedArgs)
throws Throwable {
AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
AwContents awContents = testView.getAwContents();
final OnReceivedLoginRequestHelper loginRequestHelper =
mContentsClient.getOnReceivedLoginRequestHelper();
final String path = "/" + testName + ".html";
final String html = testName;
List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>();
headers.add(Pair.create("x-auto-login", xAutoLoginHeader));
TestWebServer webServer = null;
try {
webServer = new TestWebServer(false);
final String pageUrl = webServer.setResponse(path, html, headers);
final int callCount = loginRequestHelper.getCallCount();
loadUrlAsync(awContents, pageUrl);
loginRequestHelper.waitForCallback(callCount);
assertEquals(expectedRealm, loginRequestHelper.getRealm());
assertEquals(expectedAccount, loginRequestHelper.getAccount());
assertEquals(expectedArgs, loginRequestHelper.getArgs());
} finally {
if (webServer != null) webServer.shutdown();
}
}
@Feature({"AndroidWebView"})
@SmallTest
public void testAutoLoginOnGoogleCom() throws Throwable {
autoLoginTestHelper(
"testAutoLoginOnGoogleCom",
"realm=com.google&account=foo%40bar.com&args=random_string",
"com.google",
"foo@bar.com",
"random_string" );
}
@Feature({"AndroidWebView"})
@SmallTest
public void testAutoLoginWithNullAccount() throws Throwable {
autoLoginTestHelper(
"testAutoLoginOnGoogleCom",
"realm=com.google&args=not.very.inventive",
"com.google",
null,
"not.very.inventive" );
}
@Feature({"AndroidWebView"})
@SmallTest
public void testAutoLoginOnNonGoogle() throws Throwable {
autoLoginTestHelper(
"testAutoLoginOnGoogleCom",
"realm=com.bar&account=foo%40bar.com&args=args",
"com.bar",
"foo@bar.com",
"args" );
}
}