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

Side by Side Diff: mojo/android/javatests/src/org/chromium/mojo/bindings/ValidationTest.java

Issue 522353003: mojo: Run validation tests on java (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move backward compatibility test o its own package. Created 6 years, 3 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
OLDNEW
(Empty)
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.mojo.bindings;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8 import android.util.Log;
9
10 import org.chromium.base.test.util.UrlUtils;
11 import org.chromium.mojo.MojoTestCase;
12 import org.chromium.mojo.bindings.test.mojom.mojo.ConformanceTestInterface;
13 import org.chromium.mojo.bindings.test.mojom.mojo.IntegrationTestInterface1;
14 import org.chromium.mojo.bindings.test.mojom.mojo.IntegrationTestInterface2TestH elper;
15 import org.chromium.mojo.system.Handle;
16 import org.chromium.mojo.system.InvalidHandle;
17
18 import java.io.File;
19 import java.io.FileFilter;
20 import java.io.FileNotFoundException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Scanner;
24
25 /**
26 * Testing validation upon deserialization using the interfaces defined in the
27 * mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom file.
28 * <p>
29 * One needs to pass '--test_data=bindings:{path to mojo/public/interfaces/bindi ngs/tests/data}' to
30 * the test_runner script for this test to find the validation data it needs.
31 */
32 public class ValidationTest extends MojoTestCase {
33
34 /**
35 * The path where validation test data is.
36 */
37 private static final File VALIDATION_TEST_DATA_PATH =
38 new File(UrlUtils.getTestFilePath("bindings/validation"));
39
40 /**
41 * The data needed for a validation test.
42 */
43 private static class TestData {
44 public File dataFile;
45 public ValidationTestUtil.Data inputData;
46 public String expectedResult;
47 }
48
49 private static class DataFileFilter implements FileFilter {
50 private final String mPrefix;
51
52 public DataFileFilter(String prefix) {
53 this.mPrefix = prefix;
54 }
55
56 @Override
57 public boolean accept(File pathname) {
58 return pathname.isFile() && pathname.getName().startsWith(mPrefix)
59 && pathname.getName().endsWith(".data");
60 }
61 }
62
63 private static String getStringContent(File f) throws FileNotFoundException {
64 Scanner scanner = new Scanner(f).useDelimiter("\\Z");
65 if (scanner.hasNext()) {
66 return scanner.next();
67 }
68 return "";
69 }
70
71 private static List<TestData> getTestData(String prefix)
72 throws FileNotFoundException {
73 List<TestData> results = new ArrayList<TestData>();
74
75 // Do not fail if the test data is not present.
76 if (!VALIDATION_TEST_DATA_PATH.isDirectory()) {
77 Log.w("ValidationTest", "No test found.");
78 return results;
79 }
80
81 for (File dataFile : VALIDATION_TEST_DATA_PATH.listFiles(new DataFileFil ter(prefix))) {
82 File resultFile = new File(dataFile.getParent(),
83 dataFile.getName().replaceFirst("\\.data$", ".expected"));
84 TestData testData = new TestData();
85 testData.dataFile = dataFile;
86 testData.inputData = ValidationTestUtil.parseData(getStringContent(d ataFile));
87 testData.expectedResult = getStringContent(resultFile);
88 results.add(testData);
89 }
90 return results;
91 }
92
93 /**
94 * Runs all the test with the given prefix on the given {@link MessageReceiv er}.
95 */
96 private static void runTest(String prefix, MessageReceiver messageReceiver)
97 throws FileNotFoundException {
98 List<TestData> testData = getTestData(prefix);
99 for (TestData test : testData) {
100 assertNull(test.inputData.getErrorMessage());
101 List<Handle> handles = new ArrayList<Handle>();
102 for (int i = 0; i < test.inputData.getHandlesCount(); ++i) {
103 handles.add(InvalidHandle.INSTANCE);
104 }
105 Message message = new SimpleMessage(test.inputData.getData(), handle s);
106 boolean passed = messageReceiver.accept(message);
107 if (passed && !test.expectedResult.equals("PASS")) {
108 fail("Input: " + test.dataFile.getName() +
109 ": The message should have been refused. Expected error: " +
110 test.expectedResult);
111 }
112 if (!passed && test.expectedResult.equals("PASS")) {
113 fail("Input: " + test.dataFile.getName() +
114 ": The message should have been accepted.");
115 }
116 }
117 }
118
119 private static class RoutingMessageReceiver implements MessageReceiver {
120 private final MessageReceiver mRequest;
121 private final MessageReceiver mResponse;
122
123 private RoutingMessageReceiver(MessageReceiver request, MessageReceiver response) {
124 this.mRequest = request;
125 this.mResponse = response;
126 }
127
128 /**
129 * @see MessageReceiver#accept(Message)
130 */
131 @Override
132 public boolean accept(Message message) {
133 try {
134 MessageHeader header = message.asMojoMessage().getHeader();
135 if (header.hasFlag(MessageHeader.MESSAGE_IS_RESPONSE_FLAG)) {
136 return mResponse.accept(message);
137 } else {
138 return mRequest.accept(message);
139 }
140 } catch (DeserializationException e) {
141 return false;
142 }
143 }
144
145 /**
146 * @see MessageReceiver#close()
147 */
148 @Override
149 public void close() {
150 }
151
152 }
153
154 /**
155 * A trivial message receiver that refuses all messages it receives.
156 */
157 private static class SinkMessageReceiver implements MessageReceiverWithRespo nder {
158
159 @Override
160 public boolean accept(Message message) {
161 return false;
162 }
163
164 @Override
165 public void close() {
166 }
167
168 @Override
169 public boolean acceptWithResponder(Message message, MessageReceiver resp onder) {
170 return false;
171 }
172 }
173
174 /**
175 * Testing the conformance suite.
176 */
177 @SmallTest
178 public void testConformance() throws FileNotFoundException {
179 runTest("conformance_", ConformanceTestInterface.MANAGER.buildStub(null,
180 ConformanceTestInterface.MANAGER.buildProxy(null, new SinkMessag eReceiver())));
181 }
182
183 /**
184 * Testing the integration suite.
185 */
186 @SmallTest
187 public void testIntegration() throws FileNotFoundException {
188 runTest("integration_",
189 new RoutingMessageReceiver(IntegrationTestInterface1.MANAGER.bui ldStub(null,
190 IntegrationTestInterface1.MANAGER.buildProxy(null,
191 new SinkMessageReceiver())),
192 IntegrationTestInterface2TestHelper.
193 newIntegrationTestInterface2MethodCallback()));
194 }
195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698