Chromium Code Reviews| Index: mojo/android/javatests/src/org/chromium/mojo/bindings/ValidationTest.java |
| diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/ValidationTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/ValidationTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ef9ab74e0648f821be08b64c16b9468f6b6f4e5a |
| --- /dev/null |
| +++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/ValidationTest.java |
| @@ -0,0 +1,197 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.mojo.bindings; |
| + |
| +import android.test.suitebuilder.annotation.SmallTest; |
| +import android.util.Log; |
| + |
| +import org.chromium.base.test.util.UrlUtils; |
| +import org.chromium.mojo.MojoTestCase; |
| +import org.chromium.mojo.bindings.test.mojom.mojo.ConformanceTestInterface; |
| +import org.chromium.mojo.bindings.test.mojom.mojo.IntegrationTestInterface1; |
| +import org.chromium.mojo.bindings.test.mojom.mojo.IntegrationTestInterface2TestHelper; |
| +import org.chromium.mojo.system.Handle; |
| +import org.chromium.mojo.system.InvalidHandle; |
| + |
| +import java.io.File; |
| +import java.io.FileFilter; |
| +import java.io.FileNotFoundException; |
| +import java.util.ArrayList; |
| +import java.util.List; |
| +import java.util.Scanner; |
| + |
| +/** |
| + * Testing deserialization validation. |
|
ppi
2014/09/02 13:07:45
Let's indicate ".. using the interfaces defined in
ppi
2014/09/02 13:07:45
nit: Testing validation upon deserialization.
qsr
2014/09/02 13:28:53
Done.
qsr
2014/09/02 13:28:53
Done.
|
| + */ |
| +public class ValidationTest extends MojoTestCase { |
| + |
| + /** |
| + * The path where validation test data is. |
| + */ |
| + private static final File VALIDATION_TEST_DATA_PATH = |
| + new File(UrlUtils.getTestFilePath("bindings/validation")); |
| + |
| + /** |
| + * The data needed for a validation test. |
| + */ |
| + private static class TestData { |
| + public File file; |
|
ppi
2014/09/02 13:07:44
nit: s/file/dataFile/
qsr
2014/09/02 13:28:53
Done.
|
| + public ValidationTestUtil.Data inputData; |
| + public String result; |
|
ppi
2014/09/02 13:07:44
s/result/expectedResult/
qsr
2014/09/02 13:28:53
Done.
|
| + } |
| + |
| + private static class DataFileFilter implements FileFilter { |
| + private final String mPrefix; |
| + |
| + public DataFileFilter(String prefix) { |
| + this.mPrefix = prefix; |
| + } |
| + |
| + @Override |
| + public boolean accept(File pathname) { |
| + return pathname.isFile() && pathname.getName().startsWith(mPrefix) |
| + && pathname.getName().endsWith(".data"); |
| + } |
| + } |
| + |
| + private static String getStringContent(File f) throws FileNotFoundException { |
| + Scanner scanner = new Scanner(f).useDelimiter("\\Z"); |
| + if (scanner.hasNext()) { |
| + return scanner.next(); |
| + } |
| + return ""; |
| + } |
| + |
| + private static List<TestData> getTestData(String prefix) |
| + throws FileNotFoundException { |
| + List<TestData> results = new ArrayList<TestData>(); |
| + |
| + // Do not fail if not test path is given, just skip the test. |
|
ppi
2014/09/02 13:07:45
Did you mean "if test path is not given"? Also, wh
qsr
2014/09/02 13:28:53
It happens if you do not specify --test-data on th
ppi
2014/09/02 13:57:39
Ok. Is this automatically handled by "test_runner.
qsr
2014/09/02 14:15:22
It is not handled automatically. You need to add -
|
| + if (!VALIDATION_TEST_DATA_PATH.isDirectory()) { |
| + Log.w("ValidationTest", "No test found."); |
| + return results; |
| + } |
| + |
| + for (File data : VALIDATION_TEST_DATA_PATH.listFiles(new DataFileFilter(prefix))) { |
|
ppi
2014/09/02 13:07:44
s/data/dataFile/
qsr
2014/09/02 13:28:53
Done.
|
| + File expected = new File(data.getParent(), |
|
ppi
2014/09/02 13:07:45
s/expected/resultFile/
qsr
2014/09/02 13:28:53
Done.
|
| + data.getName().replaceFirst("\\.data$", ".expected")); |
| + TestData testData = new TestData(); |
| + testData.file = data; |
| + testData.inputData = ValidationTestUtil.parseData(getStringContent(data)); |
| + testData.result = getStringContent(expected); |
| + results.add(testData); |
| + } |
| + return results; |
| + } |
| + |
| + /** |
| + * Run all the test with the given prefix on the given {@link MessageReceiver}. |
|
ppi
2014/09/02 13:07:44
s/Run/Runs/
qsr
2014/09/02 13:28:53
Done.
|
| + */ |
| + private static void runTest(String prefix, MessageReceiver messageReceiver) |
| + throws FileNotFoundException { |
| + List<TestData> testData = getTestData(prefix); |
| + for (TestData test : testData) { |
| + // TODO(qsr): Found out what is the expected result here according to the specs. |
|
ppi
2014/09/02 13:07:44
Nit: Add "// Temporarily disable a failing test."
ppi
2014/09/02 13:07:45
s/Found/Find/
qsr
2014/09/02 13:28:53
Done.
qsr
2014/09/02 13:28:53
I do not plan to push this. I want to resolve the
|
| + if (test.file.getName().equals( |
| + "conformance_mthd0_struct_num_fields_less_than_min_requirement.data")) { |
| + continue; |
| + } |
| + assertNull(test.inputData.getErrorMessage()); |
| + List<Handle> handles = new ArrayList<Handle>(); |
| + for (int i = 0; i < test.inputData.getHandlesCount(); ++i) { |
| + handles.add(InvalidHandle.INSTANCE); |
| + } |
| + Message message = new SimpleMessage(test.inputData.getData(), handles); |
| + boolean result = messageReceiver.accept(message); |
|
ppi
2014/09/02 13:07:44
s/result/passed/ maybe?
qsr
2014/09/02 13:28:53
Done.
|
| + if (result && !test.result.equals("PASS")) { |
| + fail("Input: " + test.file.getName() + |
| + ": The message should have been refused. Error expected: " + test.result); |
|
ppi
2014/09/02 13:07:45
s/Error expected/Expected error/
qsr
2014/09/02 13:28:53
Done.
|
| + } |
| + if (!result && test.result.equals("PASS")) { |
| + fail("Input: " + test.file.getName() + ": The message should have been accepted."); |
| + } |
| + } |
| + } |
| + |
| + private static class RoutingMessageReceiver implements MessageReceiver { |
| + private final MessageReceiver mRequest; |
| + private final MessageReceiver mResponse; |
| + |
| + private RoutingMessageReceiver(MessageReceiver request, MessageReceiver response) { |
| + this.mRequest = request; |
| + this.mResponse = response; |
| + } |
| + |
| + /* |
| + * (non-Javadoc) |
|
ppi
2014/09/02 13:07:44
'git grep "non-Javadoc" | wc -l' returns 0, could
qsr
2014/09/02 13:28:53
This is an automatic comment for Eclipse. Removed.
|
| + * @see |
| + * org.chromium.mojo.bindings.MessageReceiver#accept(org.chromium.mojo.bindings.Message) |
| + */ |
| + @Override |
| + public boolean accept(Message message) { |
| + try { |
| + MessageHeader header = message.asMojoMessage().getHeader(); |
| + if (header.hasFlag(MessageHeader.MESSAGE_IS_RESPONSE_FLAG)) { |
| + return mResponse.accept(message); |
| + } else { |
| + return mRequest.accept(message); |
| + } |
| + } catch (DeserializationException e) { |
| + return false; |
| + } |
| + } |
| + |
| + /* |
| + * (non-Javadoc) |
| + * @see org.chromium.mojo.bindings.MessageReceiver#close() |
| + */ |
| + @Override |
| + public void close() { |
| + } |
| + |
| + } |
| + |
| + /** |
| + * A trivial message receiver that refuses all messages it receives. |
| + */ |
| + private static class SinkMessageReceiver implements MessageReceiverWithResponder { |
| + |
| + @Override |
| + public boolean accept(Message message) { |
| + return false; |
| + } |
| + |
| + @Override |
| + public void close() { |
| + } |
| + |
| + @Override |
| + public boolean acceptWithResponder(Message message, MessageReceiver responder) { |
| + return false; |
| + } |
| + } |
| + |
| + /** |
| + * Testing the conformance suite. |
| + */ |
| + @SmallTest |
| + public void testConformance() throws FileNotFoundException { |
| + runTest("conformance_", ConformanceTestInterface.MANAGER.buildStub(null, |
| + ConformanceTestInterface.MANAGER.buildProxy(null, new SinkMessageReceiver()))); |
| + } |
| + |
| + /** |
| + * Testing the integration suite. |
| + */ |
| + @SmallTest |
| + public void testIntegration() throws FileNotFoundException { |
| + runTest("integration_", |
|
ppi
2014/09/02 13:07:44
should this be extracted into a constant next to V
qsr
2014/09/02 13:28:53
I don't know. I feel this information fits in the
ppi
2014/09/02 13:57:39
I see, let's keep these where they are.
qsr
2014/09/02 14:15:22
Acknowledged.
|
| + new RoutingMessageReceiver(IntegrationTestInterface1.MANAGER.buildStub(null, |
| + IntegrationTestInterface1.MANAGER.buildProxy(null, |
| + new SinkMessageReceiver())), |
| + IntegrationTestInterface2TestHelper. |
| + newIntegrationTestInterface2MethodCallback())); |
| + } |
| +} |