|
Android example source code file (LayoutTestsAutoTest.java)
The LayoutTestsAutoTest.java Android example source code/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.dumprendertree; import com.android.dumprendertree.TestShellActivity.DumpDataType; import com.android.dumprendertree.forwarder.AdbUtils; import com.android.dumprendertree.forwarder.ForwardService; import android.app.Instrumentation; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.test.ActivityInstrumentationTestCase2; import android.util.Log; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Vector; // TestRecorder creates four files ... // - passing tests // - failing tests // - tests for which results are ignored // - tests with no text results available // TestRecorder does not have the ability to clear the results. class MyTestRecorder { private BufferedOutputStream mBufferedOutputPassedStream; private BufferedOutputStream mBufferedOutputFailedStream; private BufferedOutputStream mBufferedOutputIgnoreResultStream; private BufferedOutputStream mBufferedOutputNoResultStream; public void passed(String layout_file) { try { mBufferedOutputPassedStream.write(layout_file.getBytes()); mBufferedOutputPassedStream.write('\n'); mBufferedOutputPassedStream.flush(); } catch(Exception e) { e.printStackTrace(); } } public void failed(String layout_file) { try { mBufferedOutputFailedStream.write(layout_file.getBytes()); mBufferedOutputFailedStream.write('\n'); mBufferedOutputFailedStream.flush(); } catch(Exception e) { e.printStackTrace(); } } public void ignoreResult(String layout_file) { try { mBufferedOutputIgnoreResultStream.write(layout_file.getBytes()); mBufferedOutputIgnoreResultStream.write('\n'); mBufferedOutputIgnoreResultStream.flush(); } catch(Exception e) { e.printStackTrace(); } } public void noResult(String layout_file) { try { mBufferedOutputNoResultStream.write(layout_file.getBytes()); mBufferedOutputNoResultStream.write('\n'); mBufferedOutputNoResultStream.flush(); } catch(Exception e) { e.printStackTrace(); } } public MyTestRecorder(boolean resume) { try { File externalDir = Environment.getExternalStorageDirectory(); File resultsPassedFile = new File(externalDir, "layout_tests_passed.txt"); File resultsFailedFile = new File(externalDir, "layout_tests_failed.txt"); File resultsIgnoreResultFile = new File(externalDir, "layout_tests_ignored.txt"); File noExpectedResultFile = new File(externalDir, "layout_tests_nontext.txt"); mBufferedOutputPassedStream = new BufferedOutputStream(new FileOutputStream(resultsPassedFile, resume)); mBufferedOutputFailedStream = new BufferedOutputStream(new FileOutputStream(resultsFailedFile, resume)); mBufferedOutputIgnoreResultStream = new BufferedOutputStream(new FileOutputStream(resultsIgnoreResultFile, resume)); mBufferedOutputNoResultStream = new BufferedOutputStream(new FileOutputStream(noExpectedResultFile, resume)); } catch (Exception e) { e.printStackTrace(); } } public void close() { try { mBufferedOutputPassedStream.close(); mBufferedOutputFailedStream.close(); mBufferedOutputIgnoreResultStream.close(); mBufferedOutputNoResultStream.close(); } catch (Exception e) { e.printStackTrace(); } } } public class LayoutTestsAutoTest extends ActivityInstrumentationTestCase2<TestShellActivity> { private static final String LOGTAG = "LayoutTests"; static final int DEFAULT_TIMEOUT_IN_MILLIS = 5000; static final String EXTERNAL_DIR = Environment.getExternalStorageDirectory().toString(); static final String LAYOUT_TESTS_ROOT = EXTERNAL_DIR + "/android/layout_tests/"; static final String LAYOUT_TESTS_RESULT_DIR = EXTERNAL_DIR + "/android/layout_tests_results/"; static final String ANDROID_EXPECTED_RESULT_DIR = EXTERNAL_DIR + "/android/expected_results/"; static final String LAYOUT_TESTS_LIST_FILE = EXTERNAL_DIR + "/android/layout_tests_list.txt"; static final String TEST_STATUS_FILE = EXTERNAL_DIR + "/android/running_test.txt"; static final String LAYOUT_TESTS_RESULTS_REFERENCE_FILES[] = { "results/layout_tests_passed.txt", "results/layout_tests_failed.txt", "results/layout_tests_nontext.txt", "results/layout_tests_crashed.txt", "run_layout_tests.py" }; static final String LAYOUT_RESULTS_FAILED_RESULT_FILE = "results/layout_tests_failed.txt"; static final String LAYOUT_RESULTS_NONTEXT_RESULT_FILE = "results/layout_tests_nontext.txt"; static final String LAYOUT_RESULTS_CRASHED_RESULT_FILE = "results/layout_tests_crashed.txt"; static final String LAYOUT_TESTS_RUNNER = "run_layout_tests.py"; private MyTestRecorder mResultRecorder; private Vector<String> mTestList; // Whether we should ignore the result for the corresponding test. Ordered same as mTestList. private Vector<Boolean> mTestListIgnoreResult; private boolean mRebaselineResults; // The JavaScript engine currently in use. This determines which set of Android-specific // expected test results we use. private String mJsEngine; private String mTestPathPrefix; private boolean mFinished; public LayoutTestsAutoTest() { super("com.android.dumprendertree", TestShellActivity.class); } // This function writes the result of the layout test to // Am status so that it can be picked up from a script. private void passOrFailCallback(String file, boolean result) { Instrumentation inst = getInstrumentation(); Bundle bundle = new Bundle(); bundle.putBoolean(file, result); inst.sendStatus(0, bundle); } private void getTestList() { // Read test list. try { BufferedReader inReader = new BufferedReader(new FileReader(LAYOUT_TESTS_LIST_FILE)); String line = inReader.readLine(); while (line != null) { if (line.startsWith(mTestPathPrefix)) { String[] components = line.split(" "); mTestList.add(components[0]); mTestListIgnoreResult.add(components.length > 1 && components[1].equals("IGNORE_RESULT")); } line = inReader.readLine(); } inReader.close(); Log.v(LOGTAG, "Test list has " + mTestList.size() + " test(s)."); } catch (Exception e) { Log.e(LOGTAG, "Error while reading test list : " + e.getMessage()); } } private void resumeTestList() { // read out the test name it stoped last time. try { String line = FsUtils.readTestStatus(TEST_STATUS_FILE); for (int i = 0; i < mTestList.size(); i++) { if (mTestList.elementAt(i).equals(line)) { mTestList = new Vector<String>(mTestList.subList(i+1, mTestList.size())); mTestListIgnoreResult = new Vector<Boolean>(mTestListIgnoreResult.subList(i+1, mTestListIgnoreResult.size())); break; } } } catch (Exception e) { Log.e(LOGTAG, "Error reading " + TEST_STATUS_FILE); } } private void clearTestStatus() { // Delete TEST_STATUS_FILE try { File f = new File(TEST_STATUS_FILE); if (f.delete()) Log.v(LOGTAG, "Deleted " + TEST_STATUS_FILE); else Log.e(LOGTAG, "Fail to delete " + TEST_STATUS_FILE); } catch (Exception e) { Log.e(LOGTAG, "Fail to delete " + TEST_STATUS_FILE + " : " + e.getMessage()); } } private String getResultFile(String test) { String shortName = test.substring(0, test.lastIndexOf('.')); // Write actual results to result directory. return shortName.replaceFirst(LAYOUT_TESTS_ROOT, LAYOUT_TESTS_RESULT_DIR) + "-result.txt"; } // Gets the file which contains WebKit's expected results for this test. private String getExpectedResultFile(String test) { // The generic result is at <path>/ Other Android examples (source code examples)Here is a short list of links related to this Android LayoutTestsAutoTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.