Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      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  */
     16 
     17 package com.android.cts;
     18 
     19 import java.io.BufferedReader;
     20 import java.io.IOException;
     21 import java.io.InputStreamReader;
     22 import java.security.NoSuchAlgorithmException;
     23 import java.util.ArrayList;
     24 import java.util.Collection;
     25 import java.util.HashMap;
     26 import java.util.Iterator;
     27 import java.util.List;
     28 
     29 import javax.xml.parsers.ParserConfigurationException;
     30 import javax.xml.transform.TransformerException;
     31 import javax.xml.transform.TransformerFactoryConfigurationError;
     32 
     33 /**
     34  * Test the test plan builder.
     35  *
     36  */
     37 public class TestPlanBuilderTests extends CtsTestBase {
     38     public static boolean DEBUG = false;
     39 
     40     private String mTestPackageBinaryName = "CtsTestPackage";
     41 
     42     /** {@inheritDoc} */
     43     @Override
     44     public void tearDown() {
     45         HostConfig.getInstance().removeTestPacakges();
     46         deleteTestPackage(mTestPackageBinaryName);
     47 
     48         super.tearDown();
     49     }
     50 
     51     /**
     52      * Test getting test suite name list contained within a test package.
     53      */
     54     public void testGetTestSuiteNames() throws IOException, NoSuchAlgorithmException {
     55         final String appPackageName = "com.google.android.cts";
     56         final String fullName = mTestPackageBinaryName + ".CtsTest";
     57         final String caseName = "CtsTestHello";
     58         final String testName = "testHello";
     59 
     60         final String descriptionConfigStr = "<TestPackage name=\"" + mTestPackageBinaryName + "\""
     61                 + " appPackageName=\"" + appPackageName + "\""
     62                 + " version=\"1.0\" AndroidFramework=\"Android 1.0\""
     63                 + " runner=\"android.test.InstrumentationTestRunner\" >\n"
     64                 + "  <Description>something extracted from java doc</Description>\n"
     65                 + "  <TestSuite name=\"com.google\">\n"
     66                 + "     <TestCase name=\"CtsTestHello\" priority=\"mandatory\">\n"
     67                 + "         <Description>" + "something extracted from java doc"
     68                 + "         </Description>\n"
     69                 + "         <!-- Test Methods -->\n"
     70                 + "         <Test name=\"testHello\"" + " type=\"automatic\"" + "/>\n"
     71                 + "     </TestCase>\n"
     72                 + "     <TestSuite name=\"TestSuiteName\">\n"
     73                 + "         <TestCase name=\"TestCaseName\" priority=\"mandatory\">\n"
     74                 + "             <Description>" + "something extracted from java doc"
     75                 + "             </Description>\n"
     76                 + "             <!-- Test Methods -->\n"
     77                 + "             <Test name=\"testName1\"" + " type=\"automatic\"" + "/>\n"
     78                 + "         </TestCase>\n"
     79                 + "     </TestSuite>\n"
     80                 + "  </TestSuite>\n"
     81                 + "</TestPackage>\n";
     82 
     83         HostConfig.getInstance().removeTestPacakges();
     84         createTestPackage(descriptionConfigStr, mTestPackageBinaryName);
     85 
     86         HostConfig.getInstance().loadTestPackages();
     87         Collection<TestPackage> testPackages = HostConfig.getInstance().getTestPackages();
     88         assertEquals(1, testPackages.size());
     89         TestPackage testPackage = testPackages.iterator().next();
     90 
     91         String suiteFullName = "com.google";
     92         TestSuite suite = testPackage.getTestSuiteByName(suiteFullName);
     93         assertEquals(suiteFullName, suite.getFullName());
     94 
     95         String caseFullName = "com.google.CtsTestHello";
     96         TestCase cazz = testPackage.getTestCaseByName(caseFullName);
     97         assertEquals(caseFullName, cazz.getFullName());
     98 
     99         List<String> testNames = testPackage.getAllTestNames(caseFullName);
    100         String testFullName = "com.google.CtsTestHello#testHello";
    101         assertEquals(1, testNames.size());
    102         assertEquals(testFullName, testNames.get(0));
    103 
    104         suiteFullName = "com.google.TestSuiteName";
    105         suite = testPackage.getTestSuiteByName(suiteFullName);
    106         assertEquals(suiteFullName, suite.getFullName());
    107 
    108         caseFullName = "com.google.TestSuiteName.TestCaseName";
    109         cazz = testPackage.getTestCaseByName(caseFullName);
    110         assertEquals(caseFullName, cazz.getFullName());
    111 
    112         testNames = testPackage.getAllTestNames(caseFullName);
    113         testFullName = "com.google.TestSuiteName.TestCaseName#testName1";
    114         assertEquals(1, testNames.size());
    115         assertEquals(testFullName, testNames.get(0));
    116     }
    117 
    118     /**
    119      * Test building test plan.
    120      */
    121     public void testPlanBuilder() throws IOException,
    122             ParserConfigurationException,
    123             TransformerFactoryConfigurationError, TransformerException, NoSuchAlgorithmException {
    124 
    125         if (DEBUG) {
    126             final String appPackageName = "com.google";
    127             final String suiteName1 = "com.google.SuiteName1";
    128             final String caseName1 = "CtsTestHello";
    129             final String testName1 = "testHello";
    130 
    131             final String cName2 = "CtsTestHello2";
    132             final String testName2 = "testHello2";
    133             final String testName3 = "testHello3";
    134             final String testName4 = "testHello4";
    135             final String testName5 = "testHello5";
    136             final String nestedSuiteName1 = "com.google";
    137             final String nestedSuiteName2 = "CtsTest.SuiteName2";
    138 
    139             final String descriptionConfigStr =
    140                     "<TestPackage name=\"" + mTestPackageBinaryName + "\""
    141                     + " appPackageName=\"" + appPackageName + "\""
    142                     + " version=\"1.0\" AndroidFramework=\"Android 1.0\""
    143                     + " runner=\"android.test.InstrumentationTestRunner\" >\n"
    144                     + " <Description>something extracted from java doc</Description>\n"
    145                     + " <TestSuite name=\"" + suiteName1 + "\"" + ">\n"
    146                     + "     <TestCase name=\"" + caseName1 + "\"" + " category=\"mandatory\">\n"
    147                     + "         <Description>" + "something extracted from java doc"
    148                     + "         </Description>\n"
    149                     + "         <!-- Test Methods -->\n"
    150                     + "         <Test name=\"" + testName1 + "\" type=\"automatic\"" + "/>\n"
    151                     + "     </TestCase>\n"
    152                     + " </TestSuite>\n"
    153                     + " <TestSuite name=\"" + nestedSuiteName1+ "\"" + ">\n"
    154                     + "     <TestSuite name=\"" + nestedSuiteName2+ "\"" + ">\n"
    155                     + "         <TestCase name=\"" + cName2 + "\"" + " priority=\"mandatory\">\n"
    156                     + "             <Test name=\"" + testName2 +"\" type=\"automatic\" />\n"
    157                     + "             <Test name=\"" + testName3 +"\" type=\"automatic\" />\n"
    158                     + "             <Test name=\"" + testName4 +"\" type=\"automatic\" />\n"
    159                     + "             <Test name=\"" + testName5 +"\" type=\"automatic\" />\n"
    160                     + "         </TestCase>\n"
    161                     + "     </TestSuite>\n"
    162                     + " </TestSuite>\n"
    163                     + "</TestPackage>\n";
    164 
    165             createTestPackage(descriptionConfigStr, mTestPackageBinaryName);
    166             HostConfig.getInstance().loadTestPackages();
    167             ArrayList<String> packageNames = new ArrayList<String>();
    168             packageNames.add(appPackageName);
    169             PlanBuilder planBuilder = new PlanBuilder(packageNames);
    170             BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    171             planBuilder.setInputStream(in);
    172             HashMap<String, ArrayList<String>> results = planBuilder.doSelect();
    173             String planName = "plan_test";
    174             if (results != null) {
    175                 Log.i("result=" + results.toString());
    176                 ArrayList<String> excludedList = results.get(appPackageName);
    177                 if (excludedList != null) {
    178                     for(String str : excludedList) {
    179                         Log.i(str);
    180                     }
    181                 } else {
    182                     Log.i("Nothing is excluded at all!");
    183                 }
    184 
    185                 String planPath =
    186                               HostConfig.getInstance().getPlanRepository().getPlanPath(planName);
    187                 TestSessionBuilder.getInstance().serialize(planName, packageNames, results);
    188                 Log.i("planPath=" + planPath);
    189             } else {
    190                 Log.i("Selected nothing for the plan of "
    191                         + planName + ". The plan is not created!");
    192             }
    193         }
    194     }
    195 
    196     /**
    197      * Test get excluded list.
    198      */
    199     public void testGetExcludedList() throws IOException,
    200             TransformerFactoryConfigurationError, NoSuchAlgorithmException {
    201 
    202         final String appPackageName = "com.google";
    203         final String suiteName1 = "com.google.SuiteName1";
    204         final String caseName1 = "CtsTestHello";
    205         final String testName1 = "testHello";
    206 
    207         final String cName2 = "CtsTestHello2";
    208         final String testName2 = "testHello2";
    209         final String testName3 = "testHello3";
    210         final String testName4 = "testHello4";
    211         final String testName5 = "testHello5";
    212         final String nestedSuiteName1 = "com.google";
    213         final String nestedSuiteName2 = "CtsTest.SuiteName2";
    214 
    215         final String descriptionConfigStr =
    216                     "<TestPackage name=\"" + mTestPackageBinaryName + "\""
    217                     + " appPackageName=\"" + appPackageName + "\""
    218                     + " version=\"1.0\" AndroidFramework=\"Android 1.0\""
    219                     + " runner=\"android.test.InstrumentationTestRunner\" >\n"
    220                     + " <Description>something extracted from java doc</Description>\n"
    221                     + " <TestSuite name=\"" + suiteName1 + "\"" + ">\n"
    222                     + "     <TestCase name=\"" + caseName1 + "\"" + " category=\"mandatory\">\n"
    223                     + "         <Description>" + "something extracted from java doc"
    224                     + "         </Description>\n"
    225                     + "         <!-- Test Methods -->\n"
    226                     + "         <Test name=\"" + testName1 + "\" type=\"automatic\"" + "/>\n"
    227                     + "     </TestCase>\n"
    228                     + " </TestSuite>\n"
    229                     + " <TestSuite name=\"" + nestedSuiteName1+ "\"" + ">\n"
    230                     + "     <TestSuite name=\"" + nestedSuiteName2+ "\"" + ">\n"
    231                     + "         <TestCase name=\"" + cName2 + "\"" + " priority=\"mandatory\">\n"
    232                     + "             <Test name=\"" + testName2 +"\" type=\"automatic\" />\n"
    233                     + "             <Test name=\"" + testName3 +"\" type=\"automatic\" />\n"
    234                     + "         </TestCase>\n"
    235                     + "     </TestSuite>\n"
    236                     + "     <TestCase name=\"" + caseName1 + "\"" + " category=\"mandatory\">\n"
    237                     + "         <Description>" + "something extracted from java doc"
    238                     + "         </Description>\n"
    239                     + "         <!-- Test Methods -->\n"
    240                     + "         <Test name=\"" + testName1 + "\" type=\"automatic\"" + "/>\n"
    241                     + "     </TestCase>\n"
    242                     + " </TestSuite>\n"
    243                     + "</TestPackage>\n";
    244 
    245         createTestPackage(descriptionConfigStr, mTestPackageBinaryName);
    246         HostConfig hf = HostConfig.getInstance();
    247         hf.loadTestPackages();
    248         TestPackage pkg = hf.getTestPackage(appPackageName);
    249         assertNotNull(pkg);
    250         String caseOneName = suiteName1 + "." + caseName1;
    251         String caseTwoName = nestedSuiteName1 + "." + nestedSuiteName2 + "." + cName2;
    252         Collection<Test> tests = pkg.getTests();
    253         assertEquals(4, tests.size());
    254         Iterator<Test> iterator = tests.iterator();
    255         Test test1 = iterator.next();
    256         Test test2 = iterator.next();
    257         Test test3 = iterator.next();
    258         Test test4 = iterator.next();
    259         ArrayList<String> excludedList = null;
    260 
    261         //scenario 1: all tests are not executed and get excluded list with notExecuted
    262         excludedList = pkg.getExcludedList(CtsTestResult.STR_NOT_EXECUTED);
    263         assertNotNull(excludedList);
    264         assertEquals(0, excludedList.size());
    265 
    266         //scenario 2: all tests are not executed and get excluded list with timeout
    267         excludedList = pkg.getExcludedList(CtsTestResult.STR_TIMEOUT);
    268         assertNull(excludedList);
    269 
    270         //scenario 3: all tests are not executed and get excluded list with nothing
    271         excludedList = pkg.getExcludedList(null);
    272         assertNotNull(excludedList);
    273         assertEquals(0, excludedList.size());
    274 
    275         //scenario 4: not all all tests are executed and get excluded list with notExecuted
    276         test2.setResult(new CtsTestResult(CtsTestResult.CODE_PASS, null, null));
    277         excludedList = pkg.getExcludedList(CtsTestResult.STR_NOT_EXECUTED);
    278         assertNotNull(excludedList);
    279         assertEquals(1, excludedList.size());
    280         assertEquals(test2.getFullName(), excludedList.get(0));
    281         test2.setResult(new CtsTestResult(CtsTestResult.CODE_NOT_EXECUTED, null, null));
    282 
    283         //scenario 5: excluded a whole suite
    284         test1.setResult(new CtsTestResult(CtsTestResult.CODE_PASS, null, null));
    285         excludedList = pkg.getExcludedList(CtsTestResult.STR_NOT_EXECUTED);
    286         assertNotNull(excludedList);
    287         assertEquals(1, excludedList.size());
    288         assertEquals(suiteName1, excludedList.get(0));
    289         test1.setResult(new CtsTestResult(CtsTestResult.CODE_NOT_EXECUTED, null, null));
    290 
    291         //scenario 6: excluded the test case directly under the embedded test suite
    292         String caseName = nestedSuiteName1 + "." + caseName1;
    293         test4.setResult(new CtsTestResult(CtsTestResult.CODE_PASS, null, null));
    294         excludedList = pkg.getExcludedList(CtsTestResult.STR_NOT_EXECUTED);
    295         assertNotNull(excludedList);
    296         assertEquals(1, excludedList.size());
    297         assertEquals(caseName, excludedList.get(0));
    298         test4.setResult(new CtsTestResult(CtsTestResult.CODE_NOT_EXECUTED, null, null));
    299     }
    300 }
    301