Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5f89ebe authored by Brett Chabot's avatar Brett Chabot Committed by Android (Google) Code Review
Browse files

Merge "Test runner cleanup: delete unused old classes."

parents 8806be91 877d428e
Loading
Loading
Loading
Loading
+0 −79
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 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 android.test;

import java.io.PrintStream;

import android.os.Bundle;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.runner.BaseTestRunner;
import junit.textui.ResultPrinter;


/**
 * Subclass of ResultPrinter that adds test case results to a bundle.
 * 
 * {@hide} - This class is deprecated, and will be going away.  Please don't use it.
 */
public class BundlePrinter extends ResultPrinter {

    private Bundle mResults;
    private boolean mFailure;
    private boolean mError;
    
    public BundlePrinter(PrintStream writer, Bundle result) {
        super(writer);
        mResults = result;
    }
    
    @Override
    public void addError(Test test, Throwable t) {
        mResults.putString(getComboName(test), BaseTestRunner.getFilteredTrace(t));
        mFailure = true;
        super.addError(test, t);
    }

    @Override
    public void addFailure(Test test, AssertionFailedError t) {
        mResults.putString(getComboName(test), BaseTestRunner.getFilteredTrace(t));
        mError = true;
        super.addFailure(test, t);
    }

    @Override
    public void endTest(Test test) {
        if (!mFailure && !mError) {
            mResults.putString(getComboName(test), "passed");
        }
        super.endTest(test);
    }

    @Override
    public void startTest(Test test) {
        mFailure = false;
        mError = false;
        super.startTest(test);
    }
    
    private String getComboName(Test test) {
        return test.getClass().getName() + ":" + ((TestCase) test).getName();
    }
    
}
+0 −63
Original line number Diff line number Diff line
/*
 * 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 android.test;

import junit.framework.*;
import junit.framework.TestCase;
import junit.runner.BaseTestRunner;
import android.os.Bundle;

/**
 * A {@link TestListener} that adds test case results to a bundle.
 * 
 * {@hide} - This class is deprecated, and will be going away.  Please don't use it.
 */
public class BundleTestListener implements TestListener {

    private Bundle mBundle;
    private boolean mFailed;

    public BundleTestListener(Bundle bundle) {
        mBundle = bundle;
    }


    public void addError(Test test, Throwable t) {
        mBundle.putString(getComboName(test), BaseTestRunner.getFilteredTrace(t));
        mFailed = true;
    }

    public void addFailure(Test test, junit.framework.AssertionFailedError t) {
        mBundle.putString(getComboName(test), BaseTestRunner.getFilteredTrace(t));
        mFailed = true;
    }

    public void endTest(Test test) {
        if (!mFailed) {
            mBundle.putString(getComboName(test), "passed");
        }
    }

    public void startTest(Test test) {
        mFailed = false;
    }

    private String getComboName(Test test) {
        return test.getClass().getName() + ":" + ((TestCase) test).getName();
    }

}
+0 −35
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 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 android.test;

/**
 * @hide - This is part of a framework that is under development and should not be used for
 * active development.
 */
public class ServiceLocator {

    private static TestBrowserController mTestBrowserController =
            new TestBrowserControllerImpl();

    public static TestBrowserController getTestBrowserController() {
        return mTestBrowserController;
    }

    static void setTestBrowserController(TestBrowserController testBrowserController) {
        mTestBrowserController = testBrowserController;
    }
}
+0 −127
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 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 android.test;

import com.android.internal.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import junit.framework.Test;
import junit.framework.TestSuite;

import java.util.List;

/**
 * @hide - This is part of a framework that is under development and should not be used for
 * active development.
 */
public abstract class TestBrowserActivity extends ListActivity
        implements android.test.TestBrowserView, AdapterView.OnItemClickListener,
        TestSuiteProvider {

    private TestBrowserController mTestBrowserController;
    public static final String BUNDLE_EXTRA_PACKAGE = "package";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getListView().setOnItemClickListener(this);

        mTestBrowserController = ServiceLocator.getTestBrowserController();
        mTestBrowserController.setTargetPackageName(getPackageName());
        mTestBrowserController.registerView(this);
        mTestBrowserController.setTargetBrowserActivityClassName(this.getClass().getName());

        // Apk paths used to search for test classes when using TestSuiteBuilders.
        String[] apkPaths = {getPackageCodePath()};
        ClassPathPackageInfoSource.setApkPaths(apkPaths);
    }

    @Override
    protected void onStart() {
        super.onStart();
        TestSuite testSuite = getTestSuiteToBrowse();
        mTestBrowserController.setTestSuite(testSuite);
        
        String name = testSuite.getName();
        if (name != null) {
            setTitle(name.substring(name.lastIndexOf(".") + 1));
        }
    }

    /**
     * Subclasses will override this method and return the TestSuite specific to their .apk.
     * When this method is invoked due to an intent fired from
     * {@link #onItemClick(android.widget.AdapterView, android.view.View, int, long)} then get the
     * targeted TestSuite from the intent.
     *
     * @return testSuite to browse
     */
    @SuppressWarnings("unchecked")
    private TestSuite getTestSuiteToBrowse() {
        Intent intent = getIntent();
        if (Intent.ACTION_RUN.equals(intent.getAction())) {
            String testClassName = intent.getData().toString();

            try {
                Class<Test> testClass = (Class<Test>) getClassLoader().loadClass(testClassName);
                return TestCaseUtil.createTestSuite(testClass);
            } catch (ClassNotFoundException e) {
                Log.e("TestBrowserActivity", "ClassNotFoundException for " + testClassName, e);
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                Log.e("TestBrowserActivity", "IllegalAccessException for " + testClassName, e);
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                Log.e("TestBrowserActivity", "InstantiationException for " + testClassName, e);
                throw new RuntimeException(e);
            }
        } else {
            // get test classes to browwes from subclass
            return getTopTestSuite();
        }

    }

    public TestSuite getTestSuite() {
        return getTopTestSuite();
    }

    /**
     * @return A TestSuite that should be run for a given application.
     */
    public abstract TestSuite getTopTestSuite();

    public void onItemClick(AdapterView parent, View v, int position, long id) {
        Intent intent = mTestBrowserController.getIntentForTestAt(position);
        intent.putExtra(BUNDLE_EXTRA_PACKAGE, getPackageName());
        startActivity(intent);
    }

    public void setTestNames(List<String> testNames) {
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                R.layout.test_list_item, testNames);
        setListAdapter(arrayAdapter);
    }
}
+0 −38
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 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 android.test;

import android.content.Intent;
import junit.framework.TestSuite;

/**
 * @hide - This is part of a framework that is under development and should not be used for
 * active development.
 */
public interface TestBrowserController {
    String BUNDLE_EXTRA_TEST_METHOD_NAME = "testMethodName";

    Intent getIntentForTestAt(int position);

    void setTestSuite(TestSuite testSuite);

    void registerView(TestBrowserView testBrowserView);

    void setTargetBrowserActivityClassName(String targetBrowserActivityClassName);

    void setTargetPackageName(String targetPackageName);
}
Loading