| 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. |
| 4 | |
| 5 | package org.chromium.android_webview.test; |
| 6 | |
| 7 | import android.test.suitebuilder.annotation.SmallTest; |
| 8 | import android.util.Pair; |
| 9 | |
| 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; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | |
| 18 | /** |
| 19 | * Tests for the AwContentsClient.onReceivedLoginRequest callback. |
| 20 | */ |
| 21 | public class AwContentsClientAutoLoginTest extends AwTestBase { |
| 22 | public static class OnReceivedLoginRequestHelper extends CallbackHelper { |
| 23 | String mRealm; |
| 24 | String mAccount; |
| 25 | String mArgs; |
| 26 | |
| 27 | public String getRealm() { |
| 28 | assert getCallCount() > 0; |
| 29 | return mRealm; |
| 30 | } |
| 31 | |
| 32 | public String getAccount() { |
| 33 | assert getCallCount() > 0; |
| 34 | return mAccount; |
| 35 | } |
| 36 | |
| 37 | public String getArgs() { |
| 38 | assert getCallCount() > 0; |
| 39 | return mArgs; |
| 40 | } |
| 41 | |
| 42 | public void notifyCalled(String realm, String account, String args) { |
| 43 | mRealm = realm; |
| 44 | mAccount = account; |
| 45 | mArgs = args; |
| 46 | notifyCalled(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | private static class TestAwContentsClient |
| 51 | extends org.chromium.android_webview.test.TestAwContentsClient { |
| 52 | |
| 53 | private OnReceivedLoginRequestHelper mOnReceivedLoginRequestHelper; |
| 54 | |
| 55 | public TestAwContentsClient() { |
| 56 | mOnReceivedLoginRequestHelper = new OnReceivedLoginRequestHelper(); |
| 57 | } |
| 58 | |
| 59 | public OnReceivedLoginRequestHelper getOnReceivedLoginRequestHelper() { |
| 60 | return mOnReceivedLoginRequestHelper; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void onReceivedLoginRequest(String realm, String account, String args) { |
| 65 | getOnReceivedLoginRequestHelper().notifyCalled(realm, account, args); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private TestAwContentsClient mContentsClient = new TestAwContentsClient(); |
| 70 | |
| 71 | private void autoLoginTestHelper(final String testName, final String xAutoLoginHeader, |
| 72 | final String expectedRealm, final String expectedAccount, final String expectedArgs) |
| 73 | throws Throwable { |
| 74 | AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient); |
| 75 | AwContents awContents = testView.getAwContents(); |
| 76 | final OnReceivedLoginRequestHelper loginRequestHelper = |
| 77 | mContentsClient.getOnReceivedLoginRequestHelper(); |
| 78 | |
| 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)); |
| 83 | |
| 84 | TestWebServer webServer = null; |
| 85 | try { |
| 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); |
| 91 | |
| 92 | assertEquals(expectedRealm, loginRequestHelper.getRealm()); |
| 93 | assertEquals(expectedAccount, loginRequestHelper.getAccount()); |
| 94 | assertEquals(expectedArgs, loginRequestHelper.getArgs()); |
| 95 | } finally { |
| 96 | if (webServer != null) webServer.shutdown(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Feature({"AndroidWebView"}) |
| 101 | @SmallTest |
| 102 | public void testAutoLoginOnGoogleCom() throws Throwable { |
| 103 | autoLoginTestHelper( |
| 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 */); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | @Feature({"AndroidWebView"}) |
| 113 | @SmallTest |
| 114 | public void testAutoLoginWithNullAccount() throws Throwable { |
| 115 | autoLoginTestHelper( |
| 116 | "testAutoLoginOnGoogleCom", /* testName */ |
| 117 | "realm=com.google&args=not.very.inventive", /* xAutoLoginHeader */ |
| 118 | "com.google", /* expectedRealm */ |
| 119 | null, /* expectedAccount */ |
| 120 | "not.very.inventive" /* expectedArgs */); |
| 121 | } |
| 122 | |
| 123 | @Feature({"AndroidWebView"}) |
| 124 | @SmallTest |
| 125 | public void testAutoLoginOnNonGoogle() throws Throwable { |
| 126 | autoLoginTestHelper( |
| 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 */); |
| 132 | } |
| 133 | } |