Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / sample / javatests / src / org / chromium / cronet_sample_apk / CronetSampleUrlTest.java
1 // Copyright 2014 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.cronet_sample_apk;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8
9 import org.chromium.base.test.util.Feature;
10
11 import java.io.File;
12
13 /**
14  * Example test that just starts the cronet sample.
15  */
16 public class CronetSampleUrlTest extends CronetSampleTestBase {
17     // URL used for base tests.
18     private static final String URL = "http://127.0.0.1:8000";
19
20     @SmallTest
21     @Feature({"Cronet"})
22     public void testLoadUrl() throws Exception {
23         CronetSampleActivity activity = launchCronetSampleWithUrl(URL);
24
25         // Make sure the activity was created as expected.
26         assertNotNull(activity);
27
28         waitForActiveShellToBeDoneLoading();
29
30         // Make sure that the URL is set as expected.
31         assertEquals(URL, activity.getUrl());
32         assertEquals(200, activity.getHttpStatusCode());
33     }
34
35     @SmallTest
36     @Feature({"Cronet"})
37     public void testInvalidUrl() throws Exception {
38         CronetSampleActivity activity = launchCronetSampleWithUrl(
39                 "127.0.0.1:8000");
40
41         // Make sure the activity was created as expected.
42         assertNotNull(activity);
43
44         waitForActiveShellToBeDoneLoading();
45
46         // The load should fail.
47         assertEquals(0, activity.getHttpStatusCode());
48     }
49
50     @SmallTest
51     @Feature({"Cronet"})
52     public void testPostData() throws Exception {
53         String[] commandLineArgs = {
54                 CronetSampleActivity.POST_DATA_KEY, "test" };
55         CronetSampleActivity activity =
56                 launchCronetSampleWithUrlAndCommandLineArgs(URL,
57                                                             commandLineArgs);
58
59         // Make sure the activity was created as expected.
60         assertNotNull(activity);
61
62         waitForActiveShellToBeDoneLoading();
63
64         // Make sure that the URL is set as expected.
65         assertEquals(URL, activity.getUrl());
66         assertEquals(200, activity.getHttpStatusCode());
67     }
68
69     @SmallTest
70     @Feature({"Cronet"})
71     public void testNetLog() throws Exception {
72         CronetSampleActivity activity = launchCronetSampleWithUrl(
73                 "127.0.0.1:8000");
74
75         // Make sure the activity was created as expected.
76         assertNotNull(activity);
77
78         waitForActiveShellToBeDoneLoading();
79         File file = File.createTempFile("cronet", "json");
80         activity.mRequestContext.startNetLogToFile(file.getPath());
81         activity.startWithURL_UrlRequest(URL);
82         Thread.sleep(5000);
83         activity.mRequestContext.stopNetLog();
84         assertTrue(file.exists());
85         assertTrue(file.length() != 0);
86         assertTrue(file.delete());
87         assertTrue(!file.exists());
88     }
89 }