1/**
2 * Copyright 2006-2017 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.objenesis.tck.android;
17
18import android.app.Activity;
19import android.app.Instrumentation;
20import android.os.Bundle;
21import org.objenesis.tck.Main;
22import org.objenesis.tck.TextReporter;
23
24import java.io.ByteArrayOutputStream;
25import java.io.IOException;
26import java.io.PrintStream;
27
28/**
29 * Wraps the Objenesis TCK so that it can be invoked on Android as an {@link Instrumentation}.
30 *
31 * @author Ian Parkinson (Google Inc.)
32 */
33public class TckInstrumentation extends Instrumentation {
34
35   @Override
36   public void onCreate(Bundle arguments) {
37      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
38      PrintStream printStream = new PrintStream(outputStream);
39      System.setOut(printStream);
40      System.setErr(printStream);
41
42      try {
43         launch();
44      } catch (IOException e) {
45         e.printStackTrace();
46      }
47
48      Bundle bundle = new Bundle();
49      String fromStdout = outputStream.toString();
50      bundle.putString(Instrumentation.REPORT_KEY_STREAMRESULT, fromStdout);
51      finish(Activity.RESULT_OK, bundle);
52   }
53
54   private void launch() throws IOException {
55      TextReporter reporter = new TextReporter(System.out, System.err);
56
57      boolean parentConstructorTestSuccessful = Main.run(reporter);
58
59      reporter.printResult(parentConstructorTestSuccessful);
60
61   }
62}
63