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