1// Copyright 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.
4
5package org.chromium.android_webview.test;
6
7import android.test.suitebuilder.annotation.SmallTest;
8import android.util.Pair;
9
10import org.chromium.android_webview.AwContents;
11import org.chromium.android_webview.test.TestAwContentsClient.OnReceivedLoginRequestHelper;
12import org.chromium.base.test.util.Feature;
13import org.chromium.net.test.util.TestWebServer;
14
15import java.util.ArrayList;
16import java.util.List;
17
18/**
19 * Tests for the AwContentsClient.onReceivedLoginRequest callback.
20 */
21public class AwContentsClientAutoLoginTest extends AwTestBase {
22    private TestAwContentsClient mContentsClient = new TestAwContentsClient();
23
24    private void autoLoginTestHelper(final String testName, final String xAutoLoginHeader,
25            final String expectedRealm, final String expectedAccount, final String expectedArgs)
26            throws Throwable {
27        AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
28        AwContents awContents = testView.getAwContents();
29        final OnReceivedLoginRequestHelper loginRequestHelper =
30            mContentsClient.getOnReceivedLoginRequestHelper();
31
32        final String path = "/" + testName + ".html";
33        final String html = testName;
34        List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>();
35        headers.add(Pair.create("x-auto-login", xAutoLoginHeader));
36
37        TestWebServer webServer = null;
38        try {
39            webServer = new TestWebServer(false);
40            final String pageUrl = webServer.setResponse(path, html, headers);
41            final int callCount = loginRequestHelper.getCallCount();
42            loadUrlAsync(awContents, pageUrl);
43            loginRequestHelper.waitForCallback(callCount);
44
45            assertEquals(expectedRealm, loginRequestHelper.getRealm());
46            assertEquals(expectedAccount, loginRequestHelper.getAccount());
47            assertEquals(expectedArgs, loginRequestHelper.getArgs());
48        } finally {
49            if (webServer != null) webServer.shutdown();
50        }
51    }
52
53    @Feature({"AndroidWebView"})
54    @SmallTest
55    public void testAutoLoginOnGoogleCom() throws Throwable {
56        autoLoginTestHelper(
57                "testAutoLoginOnGoogleCom",  /* testName */
58                "realm=com.google&account=foo%40bar.com&args=random_string", /* xAutoLoginHeader */
59                "com.google",  /* expectedRealm */
60                "foo@bar.com",  /* expectedAccount */
61                "random_string"  /* expectedArgs */);
62
63    }
64
65    @Feature({"AndroidWebView"})
66    @SmallTest
67    public void testAutoLoginWithNullAccount() throws Throwable {
68        autoLoginTestHelper(
69                "testAutoLoginOnGoogleCom",  /* testName */
70                "realm=com.google&args=not.very.inventive", /* xAutoLoginHeader */
71                "com.google",  /* expectedRealm */
72                null,  /* expectedAccount */
73                "not.very.inventive"  /* expectedArgs */);
74    }
75
76    @Feature({"AndroidWebView"})
77    @SmallTest
78    public void testAutoLoginOnNonGoogle() throws Throwable {
79        autoLoginTestHelper(
80                "testAutoLoginOnGoogleCom",  /* testName */
81                "realm=com.bar&account=foo%40bar.com&args=args", /* xAutoLoginHeader */
82                "com.bar",  /* expectedRealm */
83                "foo@bar.com",  /* expectedAccount */
84                "args"  /* expectedArgs */);
85    }
86}
87