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.content.res.AssetManager; 24 import android.os.Environment; 25 import android.platform.test.annotations.Presubmit; 26 import android.server.wm.intent.Persistence.IntentFlag; 27 import android.server.wm.intent.Persistence.TestCase; 28 29 import androidx.test.filters.LargeTest; 30 31 import com.google.common.collect.Lists; 32 33 import org.json.JSONException; 34 import org.json.JSONObject; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.junit.runners.Parameterized; 38 39 import java.io.BufferedInputStream; 40 import java.io.BufferedReader; 41 import java.io.File; 42 import java.io.IOException; 43 import java.io.InputStreamReader; 44 import java.nio.file.Files; 45 import java.nio.file.Path; 46 import java.util.Collection; 47 import java.util.List; 48 import java.util.Map; 49 import java.util.stream.Collectors; 50 51 /** 52 * This test will verify every intent_test json file in a separately run test, through JUnits 53 * Parameterized runner. 54 * 55 * Running a single test using this class is not supported. 56 * For this use case look at {@link IntentGenerationTests#verifySingle()} 57 * 58 * Note: atest CtsWindowManagerDeviceTestCases:IntentTests#verify does not work because the 59 * Parameterized runner won't expose the test that way to atest. 60 * 61 * Build/Install/Run: 62 * atest CtsWindowManagerDeviceTestCases:IntentTests 63 */ 64 @Presubmit 65 @LargeTest 66 @RunWith(Parameterized.class) 67 public class IntentTests 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 private static Context TARGET_CONTEXT = getInstrumentation().getTargetContext(); 75 76 private static final int JSON_INDENTATION_LEVEL = 4; 77 78 /** 79 * The runner used to verify the recorded test cases. 80 */ 81 private LaunchRunner mLaunchRunner; 82 83 /** 84 * The Test case we are currently verifying. 85 */ 86 private TestCase mTestCase; 87 IntentTests(TestCase testCase, String name)88 public IntentTests(TestCase testCase, String name) { 89 mTestCase = testCase; 90 } 91 92 /** 93 * Read all the tests in the assets of the application and create a separate test to run for 94 * each of them using the {@link org.junit.runners.Parameterized} 95 */ 96 @Parameterized.Parameters(name = "{1}") data()97 public static Collection<Object[]> data() throws IOException, JSONException { 98 return readAllFromAssets().stream().map( 99 test -> new Object[]{test, test.getName()}).collect( 100 Collectors.toList()); 101 } 102 103 @Test verify()104 public void verify() { 105 mLaunchRunner.verify(TARGET_CONTEXT, mTestCase); 106 } 107 108 109 @Override setUp()110 public void setUp() throws Exception { 111 super.setUp(); 112 mLaunchRunner = new LaunchRunner(this); 113 } 114 115 @Override activitiesUsedInTest()116 List<ComponentName> activitiesUsedInTest() { 117 return mTestCase.getSetup().componentsInCase(); 118 } 119 readAllFromAssets()120 public static List<TestCase> readAllFromAssets() throws IOException, JSONException { 121 List<TestCase> testCases = Lists.newArrayList(); 122 AssetManager assets = TARGET_CONTEXT.getAssets(); 123 124 List<String> testFiles = Lists.newArrayList(); 125 for (String dir : assets.list("")) { 126 for (String file : assets.list(dir)) { 127 if (file.endsWith(".json")) { 128 testFiles.add(dir + "/" + file); 129 } 130 } 131 } 132 133 for (String testFile : testFiles) { 134 try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( 135 new BufferedInputStream(assets.open(testFile))))) { 136 String jsonData = bufferedReader.lines().collect( 137 Collectors.joining("\n")); 138 139 TestCase testCase = TestCase.fromJson( 140 new JSONObject(jsonData), TABLE, testFile); 141 142 testCases.add(testCase); 143 } 144 } 145 146 return testCases; 147 } 148 writeToDocumentsStorage(TestCase testCase, int number, String dirName)149 static void writeToDocumentsStorage(TestCase testCase, int number, String dirName) 150 throws JSONException, IOException { 151 File documentsDirectory = Environment.getExternalStoragePublicDirectory( 152 Environment.DIRECTORY_DOCUMENTS); 153 Path testsFilePath = documentsDirectory.toPath().resolve(dirName); 154 155 if (!Files.exists(testsFilePath)) { 156 Files.createDirectories(testsFilePath); 157 } 158 Files.write(testsFilePath.resolve("test-" + number + ".json"), 159 Lists.newArrayList(testCase.toJson().toString(JSON_INDENTATION_LEVEL))); 160 } 161 } 162 163