1 // Copyright (c) 2013 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.
5 package org.chromium.android_webview.test;
7 import android.test.suitebuilder.annotation.SmallTest;
8 import android.util.Pair;
10 import org.chromium.base.test.util.Feature;
11 import org.chromium.content.browser.test.util.CallbackHelper;
12 import org.chromium.android_webview.AwContents;
13 import org.chromium.net.test.util.TestWebServer;
15 import java.util.ArrayList;
16 import java.util.List;
19 * Tests for the AwContentsClient.onReceivedLoginRequest callback.
21 public class AwContentsClientAutoLoginTest extends AwTestBase {
22 public static class OnReceivedLoginRequestHelper extends CallbackHelper {
27 public String getRealm() {
28 assert getCallCount() > 0;
32 public String getAccount() {
33 assert getCallCount() > 0;
37 public String getArgs() {
38 assert getCallCount() > 0;
42 public void notifyCalled(String realm, String account, String args) {
50 private static class TestAwContentsClient
51 extends org.chromium.android_webview.test.TestAwContentsClient {
53 private OnReceivedLoginRequestHelper mOnReceivedLoginRequestHelper;
55 public TestAwContentsClient() {
56 mOnReceivedLoginRequestHelper = new OnReceivedLoginRequestHelper();
59 public OnReceivedLoginRequestHelper getOnReceivedLoginRequestHelper() {
60 return mOnReceivedLoginRequestHelper;
64 public void onReceivedLoginRequest(String realm, String account, String args) {
65 getOnReceivedLoginRequestHelper().notifyCalled(realm, account, args);
69 private TestAwContentsClient mContentsClient = new TestAwContentsClient();
71 private void autoLoginTestHelper(final String testName, final String xAutoLoginHeader,
72 final String expectedRealm, final String expectedAccount, final String expectedArgs)
74 AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
75 AwContents awContents = testView.getAwContents();
76 final OnReceivedLoginRequestHelper loginRequestHelper =
77 mContentsClient.getOnReceivedLoginRequestHelper();
79 final String path = "/" + testName + ".html";
80 final String html = testName;
81 List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>();
82 headers.add(Pair.create("x-auto-login", xAutoLoginHeader));
84 TestWebServer webServer = null;
86 webServer = new TestWebServer(false);
87 final String pageUrl = webServer.setResponse(path, html, headers);
88 final int callCount = loginRequestHelper.getCallCount();
89 loadUrlAsync(awContents, pageUrl);
90 loginRequestHelper.waitForCallback(callCount);
92 assertEquals(expectedRealm, loginRequestHelper.getRealm());
93 assertEquals(expectedAccount, loginRequestHelper.getAccount());
94 assertEquals(expectedArgs, loginRequestHelper.getArgs());
96 if (webServer != null) webServer.shutdown();
100 @Feature({"AndroidWebView"})
102 public void testAutoLoginOnGoogleCom() throws Throwable {
104 "testAutoLoginOnGoogleCom", /* testName */
105 "realm=com.google&account=foo%40bar.com&args=random_string", /* xAutoLoginHeader */
106 "com.google", /* expectedRealm */
107 "foo@bar.com", /* expectedAccount */
108 "random_string" /* expectedArgs */);
112 @Feature({"AndroidWebView"})
114 public void testAutoLoginWithNullAccount() throws Throwable {
116 "testAutoLoginOnGoogleCom", /* testName */
117 "realm=com.google&args=not.very.inventive", /* xAutoLoginHeader */
118 "com.google", /* expectedRealm */
119 null, /* expectedAccount */
120 "not.very.inventive" /* expectedArgs */);
123 @Feature({"AndroidWebView"})
125 public void testAutoLoginOnNonGoogle() throws Throwable {
127 "testAutoLoginOnGoogleCom", /* testName */
128 "realm=com.bar&account=foo%40bar.com&args=args", /* xAutoLoginHeader */
129 "com.bar", /* expectedRealm */
130 "foo@bar.com", /* expectedAccount */
131 "args" /* expectedArgs */);