Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / android_webview / javatests / src / org / chromium / android_webview / test / AwContentsClientAutoLoginTest.java
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
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.android_webview.AwContents;
11 import org.chromium.android_webview.test.TestAwContentsClient.OnReceivedLoginRequestHelper;
12 import org.chromium.base.test.util.Feature;
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     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 = TestWebServer.start();
38         try {
39             final String pageUrl = webServer.setResponse(path, html, headers);
40             final int callCount = loginRequestHelper.getCallCount();
41             loadUrlAsync(awContents, pageUrl);
42             loginRequestHelper.waitForCallback(callCount);
43
44             assertEquals(expectedRealm, loginRequestHelper.getRealm());
45             assertEquals(expectedAccount, loginRequestHelper.getAccount());
46             assertEquals(expectedArgs, loginRequestHelper.getArgs());
47         } finally {
48             webServer.shutdown();
49         }
50     }
51
52     @Feature({"AndroidWebView"})
53     @SmallTest
54     public void testAutoLoginOnGoogleCom() throws Throwable {
55         autoLoginTestHelper(
56                 "testAutoLoginOnGoogleCom",  /* testName */
57                 "realm=com.google&account=foo%40bar.com&args=random_string", /* xAutoLoginHeader */
58                 "com.google",  /* expectedRealm */
59                 "foo@bar.com",  /* expectedAccount */
60                 "random_string"  /* expectedArgs */);
61
62     }
63
64     @Feature({"AndroidWebView"})
65     @SmallTest
66     public void testAutoLoginWithNullAccount() throws Throwable {
67         autoLoginTestHelper(
68                 "testAutoLoginOnGoogleCom",  /* testName */
69                 "realm=com.google&args=not.very.inventive", /* xAutoLoginHeader */
70                 "com.google",  /* expectedRealm */
71                 null,  /* expectedAccount */
72                 "not.very.inventive"  /* expectedArgs */);
73     }
74
75     @Feature({"AndroidWebView"})
76     @SmallTest
77     public void testAutoLoginOnNonGoogle() throws Throwable {
78         autoLoginTestHelper(
79                 "testAutoLoginOnGoogleCom",  /* testName */
80                 "realm=com.bar&account=foo%40bar.com&args=args", /* xAutoLoginHeader */
81                 "com.bar",  /* expectedRealm */
82                 "foo@bar.com",  /* expectedAccount */
83                 "args"  /* expectedArgs */);
84     }
85 }