Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: chrome/test/android/javatests/src/org/chromium/chrome/test/ChromeInstrumentationTestRunner.java

Issue 1218053003: [Android] Begin switching from lighttpd + apache to EmbeddedTestServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/chrome_tests.gypi ('k') | content/content_shell.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.test; 5 package org.chromium.chrome.test;
6 6
7 import android.app.Activity;
8 import android.os.Bundle; 7 import android.os.Bundle;
9 import android.os.Environment;
10 import android.util.Log; 8 import android.util.Log;
11 9
12 import junit.framework.TestCase; 10 import junit.framework.TestCase;
13 11
14 import org.apache.http.HttpEntity;
15 import org.apache.http.HttpException;
16 import org.apache.http.HttpRequest;
17 import org.apache.http.HttpResponse;
18 import org.apache.http.HttpStatus;
19 import org.apache.http.HttpVersion;
20 import org.apache.http.RequestLine;
21 import org.apache.http.StatusLine;
22 import org.apache.http.entity.FileEntity;
23 import org.apache.http.message.BasicHttpResponse;
24 import org.apache.http.message.BasicStatusLine;
25 import org.apache.http.params.BasicHttpParams;
26 import org.apache.http.params.CoreProtocolPNames;
27 import org.apache.http.params.HttpParams;
28
29 import org.chromium.base.test.BaseInstrumentationTestRunner; 12 import org.chromium.base.test.BaseInstrumentationTestRunner;
30 import org.chromium.base.test.BaseInstrumentationTestRunner.SkipCheck; 13 import org.chromium.base.test.BaseInstrumentationTestRunner.SkipCheck;
31 import org.chromium.base.test.BaseInstrumentationTestRunner.SkippingTestResult; 14 import org.chromium.base.test.BaseInstrumentationTestRunner.SkippingTestResult;
32 import org.chromium.chrome.browser.util.FeatureUtilities; 15 import org.chromium.chrome.browser.util.FeatureUtilities;
33 import org.chromium.chrome.test.util.DisableInTabbedMode; 16 import org.chromium.chrome.test.util.DisableInTabbedMode;
34 import org.chromium.net.test.BaseHttpTestServer;
35 17
36 import java.io.File;
37 import java.io.IOException;
38 import java.lang.reflect.Method; 18 import java.lang.reflect.Method;
39 import java.net.Socket;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.Map;
43 19
44 20
45 /** 21 /**
46 * An Instrumentation test runner that optionally spawns a test HTTP server. 22 * An Instrumentation test runner that optionally spawns a test HTTP server.
47 * The server's root directory is the device's external storage directory. 23 * The server's root directory is the device's external storage directory.
48 */ 24 */
49 public class ChromeInstrumentationTestRunner extends BaseInstrumentationTestRunn er { 25 public class ChromeInstrumentationTestRunner extends BaseInstrumentationTestRunn er {
50 26
51 private static final String TAG = "ChromeInstrumentationTestRunner"; 27 private static final String TAG = "ChromeInstrumentationTestRunner";
52 28
53 public static final String EXTRA_DISABLE_TEST_HTTP_SERVER =
54 "org.chromium.chrome.test.ChromeInstrumentationTestRunner.DisableTes tHttpServer";
55 public static final String EXTRA_ENABLE_TEST_HTTP_SERVER =
56 "org.chromium.chrome.test.ChromeInstrumentationTestRunner.EnableTest HttpServer";
57
58 // TODO(jbudorick): Default this to true once the bots are all enabling the server via the
59 // flag.
60 private static final boolean DEFAULT_ENABLE_TEST_HTTP_SERVER = false;
61
62 private Thread mHttpServerThread;
63 private TestHttpServer mHttpServer;
64 private boolean mRunTestHttpServer;
65
66 @Override 29 @Override
67 public void onCreate(Bundle arguments) { 30 public void onCreate(Bundle arguments) {
68 if (arguments.containsKey(EXTRA_DISABLE_TEST_HTTP_SERVER)) { 31 loadLibraries();
69 mRunTestHttpServer = false;
70 } else if (arguments.containsKey(EXTRA_ENABLE_TEST_HTTP_SERVER)) {
71 mRunTestHttpServer = true;
72 } else {
73 mRunTestHttpServer = DEFAULT_ENABLE_TEST_HTTP_SERVER;
74 }
75
76 // InstrumentationTestRunner.onCreate() calls start() at the end, so we call it last.
77 super.onCreate(arguments); 32 super.onCreate(arguments);
78 } 33 }
79 34
80 @Override 35 @Override
81 public void onStart() {
82 if (mRunTestHttpServer) {
83 try {
84 startTestHttpServer();
85 } catch (IOException e) {
86 Log.e(TAG, "Failed to start HTTP test server", e);
87 finish(Activity.RESULT_CANCELED, null);
88 }
89 }
90 super.onStart();
91 }
92
93 private void startTestHttpServer() throws IOException {
94 mHttpServer = new TestHttpServer(Environment.getExternalStorageDirectory (), 8000);
95 mHttpServerThread = new Thread(mHttpServer);
96 mHttpServerThread.start();
97 mHttpServer.waitForServerToStart();
98 }
99
100 private static class TestHttpServer extends BaseHttpTestServer {
101 private static final String TAG = "ChromeInstrumentationTestRunner.TestH ttpServer";
102
103 private final File mRootDirectory;
104 private final String mRootPath;
105
106 private static final int ACCEPT_TIMEOUT_MS = 5000;
107 private static final String DEFAULT_CONTENT_TYPE = "application/octet-st ream";
108 private static final Map<String, String> EXTENSION_CONTENT_TYPE_MAP;
109 static {
110 Map<String, String> m = new HashMap<String, String>();
111 m.put(".conf", "text/plain");
112 m.put(".css", "text/css");
113 m.put(".dtd", "text/xml");
114 m.put(".gif", "image/gif");
115 m.put(".htm", "text/html");
116 m.put(".html", "text/html");
117 m.put(".jpeg", "image/jpeg");
118 m.put(".jpg", "image/jpeg");
119 m.put(".js", "application/x-javascript");
120 m.put(".log", "text/plain");
121 m.put(".manifest", "text/cache-manifest");
122 m.put(".mp4", "video/mp4");
123 m.put(".png", "image/png");
124 m.put(".svg", "image/svg+xml");
125 m.put(".text", "text/plain");
126 m.put(".txt", "text/plain");
127 m.put(".xhtml", "application/xhtml+xml");
128 m.put(".xhtmlmp", "application/vnd.wap.xhtml+xml");
129 m.put(".xml", "text/xml");
130 EXTENSION_CONTENT_TYPE_MAP = Collections.unmodifiableMap(m);
131 }
132
133 public TestHttpServer(File rootDirectory, int port) throws IOException {
134 super(port, ACCEPT_TIMEOUT_MS);
135 mRootDirectory = rootDirectory;
136 mRootPath = mRootDirectory.getAbsolutePath();
137 }
138
139 @Override
140 protected boolean validateSocket(Socket sock) {
141 return sock.getInetAddress().isLoopbackAddress();
142 }
143
144 @Override
145 protected HttpParams getConnectionParams() {
146 HttpParams httpParams = new BasicHttpParams();
147 httpParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVer sion.HTTP_1_0);
148 return httpParams;
149 }
150
151 @Override
152 protected void handleGet(HttpRequest request, HttpResponseCallback callb ack)
153 throws HttpException {
154 RequestLine requestLine = request.getRequestLine();
155
156 String requestPath = requestLine.getUri();
157 if (requestPath.startsWith(File.separator)) {
158 requestPath = requestPath.substring(File.separator.length());
159 }
160 File requestedFile = new File(mRootDirectory, requestPath);
161 String requestedPath = requestedFile.getAbsolutePath();
162
163 int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
164 String reason = "";
165 HttpEntity entity = null;
166 if (!requestedPath.startsWith(mRootPath)) {
167 Log.w(TAG, "Client tried to request something outside of " + mRo otPath + ": "
168 + requestedPath);
169 status = HttpStatus.SC_FORBIDDEN;
170 } else if (!requestedFile.exists()) {
171 Log.w(TAG, "Client requested non-existent file: " + requestedPat h);
172 status = HttpStatus.SC_NOT_FOUND;
173 } else if (!requestedFile.isFile()) {
174 Log.w(TAG, "Client requested something that isn't a file: " + re questedPath);
175 status = HttpStatus.SC_BAD_REQUEST;
176 reason = requestLine.getUri() + " is not a file.";
177 } else {
178 status = HttpStatus.SC_OK;
179 String contentType = null;
180 int extensionIndex = requestedPath.lastIndexOf('.');
181 if (extensionIndex == -1) {
182 contentType = DEFAULT_CONTENT_TYPE;
183 } else {
184 String extension = requestedPath.substring(extensionIndex);
185 contentType = EXTENSION_CONTENT_TYPE_MAP.get(extension);
186 if (contentType == null) {
187 Log.w(TAG, "Unrecognized extension: " + extension);
188 contentType = DEFAULT_CONTENT_TYPE;
189 }
190 }
191 entity = new FileEntity(requestedFile, contentType);
192 }
193
194 StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_0, st atus, reason);
195 HttpResponse response = new BasicHttpResponse(statusLine);
196 if (entity != null) {
197 response.setEntity(entity);
198 }
199 callback.onResponse(response);
200 }
201 }
202
203 @Override
204 public void onDestroy() {
205 super.onDestroy();
206 if (mRunTestHttpServer) stopTestHttpServer();
207 }
208
209 private void stopTestHttpServer() {
210 mHttpServer.stop();
211 try {
212 mHttpServerThread.join();
213 } catch (InterruptedException e) {
214 Log.w(TAG, "Interrupted while joining for TestHttpServer thread: " + e.toString());
215 }
216 }
217
218 @Override
219 protected void addSkipChecks(SkippingTestResult result) { 36 protected void addSkipChecks(SkippingTestResult result) {
220 super.addSkipChecks(result); 37 super.addSkipChecks(result);
221 result.addSkipCheck(new DisableInTabbedModeSkipCheck()); 38 result.addSkipCheck(new DisableInTabbedModeSkipCheck());
222 } 39 }
223 40
224 /** 41 /**
225 * Checks for tests that should only run in document mode. 42 * Checks for tests that should only run in document mode.
226 */ 43 */
227 private class DisableInTabbedModeSkipCheck implements SkipCheck { 44 private class DisableInTabbedModeSkipCheck implements SkipCheck {
228 45
(...skipping 19 matching lines...) Expand all
248 } 65 }
249 } 66 }
250 } catch (NoSuchMethodException e) { 67 } catch (NoSuchMethodException e) {
251 Log.e(TAG, "Couldn't find test method: " + e.toString()); 68 Log.e(TAG, "Couldn't find test method: " + e.toString());
252 } 69 }
253 70
254 return false; 71 return false;
255 } 72 }
256 } 73 }
257 } 74 }
OLDNEW
« no previous file with comments | « chrome/chrome_tests.gypi ('k') | content/content_shell.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698