Java Code Examples for com.android.tradefed.config.ConfigurationDescriptor

Following code examples demonstrate how to use com.android.tradefed.config.ConfigurationDescriptorfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use com.android.tradefed.config.ConfigurationDescriptorand various code implementation of this class.

                if (test instanceof HostTest) {
                    HostTest hostTest = (HostTest) test;
                    hostTest.setBuild(stubFolder);
                    int testCount = hostTest.countTestCases();
                    if (testCount == 0) {
                        throw new ConfigurationException(
                                String.format("%s: %s reports 0 test cases.", config, test));
                    }
                } 


    public void testSerialize() throws Exception {
        assertNotNull(mContext.getDeviceBuildMap());
        ITestDevice device = EasyMock.createMock(ITestDevice.class);
        IBuildInfo info = new BuildInfo("1234", "test-target");
        mContext.addAllocatedDevice("test-device", device);
        mContext.addDeviceBuildInfo("test-device", info);
        mContext.setConfigurationDescriptor(new ConfigurationDescriptor());
        assertEquals(info, mContext.getBuildInfo(device));
        File ser = SerializationUtil.serialize(mContext);
        try {
            InvocationContext deserialized =
                    (InvocationContext) SerializationUtil.deserialize(ser, true);
            assertNotNull(deserialized.getDeviceBuildMap());
            assertNotNull(deserialized.getConfigurationDescriptor());
            assertEquals(info, deserialized.getBuildInfo("test-device"));

            assertTrue(deserialized.getDevices().isEmpty());
            deserialized.addAllocatedDevice("test-device", device);
            assertEquals(info, mContext.getBuildInfo(device));
        } finally {
            FileUtil.deleteFile(ser);
        }
    } 

    public void testCount_2subModules() {
        List<IModuleDef> list = new ArrayList<>();
        list.add(new ModuleDef("moduleA", new Abi("arm32", "32"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        list.add(new ModuleDef("moduleA", new Abi("arm32", "32"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        assertEquals(1, UniqueModuleCountUtil.countUniqueModules(list));
    } 

    public void testCount_mix() {
        List<IModuleDef> list = new ArrayList<>();
        list.add(new ModuleDef("moduleA", new Abi("arm64", "64"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        list.add(new ModuleDef("moduleA", new Abi("arm32", "32"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        list.add(new ModuleDef("moduleC", new Abi("arm32", "32"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        list.add(new ModuleDef("moduleB", new Abi("arm64", "64"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        list.add(new ModuleDef("moduleB", new Abi("arm32", "32"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        list.add(new ModuleDef("moduleC", new Abi("arm64", "64"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        list.add(new ModuleDef("moduleA", new Abi("arm32", "32"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        list.add(new ModuleDef("moduleC", new Abi("arm32", "32"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        assertEquals(6, UniqueModuleCountUtil.countUniqueModules(list));
    } 

    public void testCount_2uniquesModules() {
        List<IModuleDef> list = new ArrayList<>();
        list.add(new ModuleDef("moduleA", new Abi("arm64", "64"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        list.add(new ModuleDef("moduleA", new Abi("arm32", "32"), new TestStub(),
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor()));
        assertEquals(2, UniqueModuleCountUtil.countUniqueModules(list));
    } 

    public void testModuleFinisher() throws Exception {
        IAbi abi = new Abi(ABI, "");
        MockRemoteTest mockTest = new MockRemoteTest();
        IModuleDef def = new ModuleDef(NAME, abi, mockTest,
                new ArrayList<ITargetPreparer>(), new ConfigurationDescriptor());
        ITestInvocationListener mockListener = EasyMock.createMock(ITestInvocationListener.class);
        mockListener.testRunStarted(ID, 0);
        EasyMock.expectLastCall().once();
        mockListener.testRunEnded(0, new HashMap<String, Metric>());
        EasyMock.expectLastCall().once();
        EasyMock.replay(mockListener);
        def.run(mockListener);
        EasyMock.verify(mockListener);
    } 

    public void testAccessors() throws Exception {
        IAbi abi = new Abi(ABI, "");
        MockRemoteTest mockTest = new MockRemoteTest();
        IModuleDef def = new ModuleDef(NAME, abi, mockTest, new ArrayList<ITargetPreparer>(),
                new ConfigurationDescriptor());
        assertEquals("Incorrect ID", ID, def.getId());
        assertEquals("Incorrect ABI", ABI, def.getAbi().getName());
        assertEquals("Incorrect Name", NAME, def.getName());
    } 

    public void testWriteResults() throws Exception {
        assertEquals(0L, mOutput.length());
        IInvocationContext context = new InvocationContext();
        context.setConfigurationDescriptor(new ConfigurationDescriptor());
        context.addInvocationAttribute("test", "test");
        mReporter.invocationStarted(context);
        mReporter.invocationEnded(500L);

        assertTrue(mOutput.length() != 0L);
        TestRecord record = TestRecordProtoUtil.readFromFile(mOutput);

        Any anyDescription = record.getDescription();
        assertTrue(anyDescription.is(Context.class));

        IInvocationContext endContext =
                InvocationContext.fromProto(anyDescription.unpack(Context.class));
        assertEquals("test", endContext.getAttributes().get("test").get(0));
    } 

    
    public static String getInstruction(
            ConfigurationDescriptor configDescriptor,
            LocalTestRunner runner,
            TestDescription testId) {
        if (runner == null) {
            return null;
        }
        switch (runner) {
            case ATEST:
                return getAtestInstruction(configDescriptor, testId);
            default:
                CLog.v("Runner %s is not supported yet, no instruction will be built.", runner);
                return null;
        }
    } 

    
    private static String getAtestInstruction(
            ConfigurationDescriptor configDescriptor, TestDescription testId) {
        StringBuilder instruction = new StringBuilder();
        instruction.append("Run following command to try the test in a local setup:\n");
        StringBuilder testName = new StringBuilder(configDescriptor.getModuleName());
        boolean testMethodAdded = false;
        if (testId != null) {
            testName.append(String.format(":%s", testId.getClassName()));
            if (testId.getTestName().equals(testId.getTestNameWithoutParams())) {
                testName.append(String.format("#%s", testId.getTestName()));
                testMethodAdded = true;
            }
        }

        instruction.append(String.format("atest %s --", testName));
        if (configDescriptor.getAbi() != null) {
            instruction.append(String.format(" --abi %s", configDescriptor.getAbi().getName()));
        }
        if (!testMethodAdded) {
            for (OptionDef optionDef : configDescriptor.getRerunOptions()) {
                StringBuilder option =
                        new StringBuilder(
                                String.format(
                                        "--module-arg %s:%s:",
                                        configDescriptor.getModuleName(), optionDef.name));
                if (optionDef.key == null) {
                    option.append(String.format("%s", optionDef.value));
                } else {
                    option.append(String.format("%s:=%s", optionDef.key, optionDef.value));
                }
                instruction.append(" " + option);
            }
        }
        return instruction.toString();
    } 

    
    public static String getInstruction(
            ConfigurationDescriptor configDescriptor, LocalTestRunner runner) {
        return getInstruction(configDescriptor, runner, null);
    } 

    public void testGetInstruction() {
        ConfigurationDescriptor configDescriptor = new ConfigurationDescriptor();
        configDescriptor.addRerunOption(
                new OptionDef(OPTION_NAME, OPTION_KEY, OPTION_VALUE, OPTION_SOURCE));
        configDescriptor.addRerunOption(
                new OptionDef(OPTION_NAME_ONLY, null, OPTION_VALUE_ONLY, OPTION_SOURCE));
        configDescriptor.setAbi(new Abi(ABI_NAME, "32"));
        configDescriptor.setModuleName(OPTION_SOURCE);
        String instruction =
                LocalRunInstructionBuilder.getInstruction(
                        configDescriptor, LocalTestRunner.ATEST, null);
        assertEquals(
                "Run following command to try the test in a local setup:\n"
                        + "atest module_name -- --abi arm "
                        + "--module-arg module_name:option:key:=value "
                        + "--module-arg module_name:option_only:value_only",
                instruction);
    } 

    public void testGetInstruction_withTestId() {
        ConfigurationDescriptor configDescriptor = new ConfigurationDescriptor();
        configDescriptor.setAbi(new Abi(ABI_NAME, "32"));
        configDescriptor.setModuleName(OPTION_SOURCE);
        TestDescription testId = new TestDescription(CLASS_NAME, METHOD_NAME);
        String instruction =
                LocalRunInstructionBuilder.getInstruction(
                        configDescriptor, LocalTestRunner.ATEST, testId);
        assertEquals(
                "Run following command to try the test in a local setup:\n"
                        + "atest module_name:class_name#method_name -- --abi arm",
                instruction);
    } 

    public void testGetInstruction_withTestIdAndParameter() {
        ConfigurationDescriptor configDescriptor = new ConfigurationDescriptor();
        configDescriptor.setAbi(new Abi(ABI_NAME, "32"));
        configDescriptor.setModuleName(OPTION_SOURCE);
        TestDescription testId = new TestDescription(CLASS_NAME, METHOD_NAME_WITH_PARAMETER);
        String instruction =
                LocalRunInstructionBuilder.getInstruction(
                        configDescriptor, LocalTestRunner.ATEST, testId);
        assertEquals(
                "Run following command to try the test in a local setup:\n"
                        + "atest module_name:class_name -- --abi arm",
                instruction);
    } 

Advertisement
Javadoc
Configuration Object that describes some aspect of the configuration itself. Like a membership test-

suite-tag. This class cannot receive option values via command line. Only directly in the xml.

Read More
Advertisement