Home | History | Annotate | Download | only in intent
      1 /*
      2  * Copyright (C) 2019 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 android.server.wm.intent;
     18 
     19 import static androidx.test.InstrumentationRegistry.getInstrumentation;
     20 
     21 import android.content.ComponentName;
     22 import android.content.Context;
     23 import android.os.Environment;
     24 import android.server.wm.intent.Persistence.IntentFlag;
     25 import android.server.wm.intent.Persistence.TestCase;
     26 
     27 import org.json.JSONException;
     28 import org.json.JSONObject;
     29 import org.junit.Ignore;
     30 import org.junit.Test;
     31 
     32 import java.io.File;
     33 import java.io.IOException;
     34 import java.nio.charset.StandardCharsets;
     35 import java.nio.file.Files;
     36 import java.nio.file.Path;
     37 import java.util.Collections;
     38 import java.util.List;
     39 import java.util.Map;
     40 
     41 /**
     42  * The {@link IntentGenerationTests#generate()} method runs a very long generation process that
     43  * takes over 10 minutes.
     44  *
     45  * The generated tests and results observed on a reference device can then be used in CTS for
     46  * verification.
     47  *
     48  * It writes a bunch of files out to the directory obtained from
     49  * {@link Environment#getExternalStoragePublicDirectory(String)} which is currently
     50  * /storage/emulated/0/Documents/.
     51  *
     52  * These can then be pulled out via adb and put into the intent_tests asset directory.
     53  * These are the tests that are checked by atest CtsWindowManagerDeviceTestCases:IntentTests
     54  *
     55  * Because this process takes so much time, certain timeouts in the AndroidTest.xml need to be
     56  * raised.
     57  *
     58  * <option name="run-command" value="settings put system screen_off_timeout 1200000000" /> in the
     59  * target preparer and <option name="test-timeout" value="3600000" /> in the <test class=></test>
     60  *
     61  * Since this test only exists to trigger this process using the test infrastructure
     62  * it is ignored by default.
     63  *
     64  * Build/Install/Run:
     65  * atest CtsWindowManagerDeviceTestCases:IntentGenerationTests
     66  */
     67 public class IntentGenerationTests extends IntentTestBase {
     68     private static final Cases CASES = new Cases();
     69 
     70     /**
     71      * The flag parsing table used to parse the json files.
     72      */
     73     private static final Map<String, IntentFlag> TABLE = CASES.createFlagParsingTable();
     74 
     75     /**
     76      * Runner used to record testCases.
     77      */
     78     private LaunchRunner mLaunchRunner;
     79 
     80     /**
     81      * The target context we can launch the first activity from.
     82      */
     83     private Context mTargetContext = getInstrumentation().getTargetContext();
     84 
     85     //20 minute timeout.
     86     @Test(timeout = 1_200_000)
     87     @Ignore
     88     public void generate() throws Exception {
     89         mLaunchRunner.runAndWrite(mTargetContext, "forResult", CASES.forResultCases());
     90         mLaunchRunner.runAndWrite(mTargetContext, "newTask", CASES.newTaskCases());
     91         mLaunchRunner.runAndWrite(mTargetContext, "newDocumentCases", CASES.newDocumentCases());
     92         mLaunchRunner.runAndWrite(mTargetContext, "resetTaskIfNeeded", CASES.resetTaskIfNeeded());
     93         mLaunchRunner.runAndWrite(mTargetContext, "clearCases", CASES.clearCases());
     94     }
     95 
     96     @Override
     97     public void setUp() throws Exception {
     98         super.setUp();
     99         mLaunchRunner = new LaunchRunner(this);
    100     }
    101 
    102     /**
    103      * Debugging utility to verify a single test in the assets folder.
    104      *
    105      * @throws IOException   if writing to storage fails
    106      * @throws JSONException if the file has invalid json in it.
    107      */
    108     @Test
    109     @Ignore
    110     public void verifySingle() throws IOException, JSONException {
    111         String test = "forResult/test-1.json";
    112         TestCase testCase = readFromStorage(test);
    113         mLaunchRunner.verify(mTargetContext, testCase);
    114     }
    115 
    116     private TestCase readFromStorage(String fileName) throws IOException, JSONException {
    117         File documentsDirectory = Environment.getExternalStoragePublicDirectory(
    118                 Environment.DIRECTORY_DOCUMENTS);
    119         Path testsFilePath = documentsDirectory.toPath().resolve(fileName);
    120 
    121         JSONObject jsonInTestFile = new JSONObject(
    122                 new String(Files.readAllBytes(testsFilePath), StandardCharsets.UTF_8));
    123         return TestCase.fromJson(jsonInTestFile, TABLE, fileName);
    124     }
    125 
    126     /**
    127      * This class runs multiple {@link TestCase}-s in a single test.
    128      * Therefore the list of componentNames that occur in the test is not known here.
    129      *
    130      * Instead {@link IntentTestBase#cleanUp(List)} is called from {@link
    131      * LaunchRunner#runAndWrite(Context, String, List)} instead.
    132      *
    133      * @return an emptyList
    134      */
    135     @Override
    136     List<ComponentName> activitiesUsedInTest() {
    137         return Collections.emptyList();
    138     }
    139 }
    140