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.platform.test.annotations.Presubmit; 25 import android.server.wm.intent.Persistence.IntentFlag; 26 import android.server.wm.intent.Persistence.TestCase; 27 import android.view.Display; 28 29 import androidx.test.filters.LargeTest; 30 31 import com.android.compatibility.common.util.UserHelper; 32 33 import com.google.common.collect.Lists; 34 35 import org.json.JSONException; 36 import org.json.JSONObject; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.junit.runners.Parameterized; 40 41 import java.io.BufferedInputStream; 42 import java.io.BufferedReader; 43 import java.io.IOException; 44 import java.io.InputStreamReader; 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 CtsWindowManagerDeviceActivity: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 CtsWindowManagerDeviceActivity:IntentTests 62 */ 63 @Presubmit 64 @LargeTest 65 @RunWith(Parameterized.class) 66 public class IntentTests extends IntentTestBase { 67 private static final Cases CASES = new Cases(); 68 69 /** 70 * The flag parsing table used to parse the json files. 71 */ 72 private static final Map<String, IntentFlag> TABLE = CASES.createFlagParsingTable(); 73 private static Context TARGET_CONTEXT = getInstrumentation().getTargetContext(); 74 75 private static final int JSON_INDENTATION_LEVEL = 4; 76 77 /** 78 * The runner used to verify the recorded test cases. 79 */ 80 private LaunchRunner mLaunchRunner; 81 82 /** 83 * The Test case we are currently verifying. 84 */ 85 private TestCase mTestCase; 86 87 /** 88 * The id of the display where the test will be performed 89 */ 90 private int mLaunchDisplayId = Display.DEFAULT_DISPLAY; 91 IntentTests(TestCase testCase, String name)92 public IntentTests(TestCase testCase, String name) { 93 mTestCase = testCase; 94 mLaunchDisplayId = new UserHelper(TARGET_CONTEXT).getMainDisplayId(); 95 } 96 97 /** 98 * Read all the tests in the assets of the application and create a separate test to run for 99 * each of them using the {@link org.junit.runners.Parameterized} 100 */ 101 @Parameterized.Parameters(name = "{1}") data()102 public static Collection<Object[]> data() throws IOException, JSONException { 103 return readAllFromAssets().stream().map( 104 test -> new Object[]{test, test.getName()}).collect( 105 Collectors.toList()); 106 } 107 108 @Test verify()109 public void verify() { 110 mLaunchRunner.verify(TARGET_CONTEXT, mTestCase, mLaunchDisplayId); 111 } 112 113 114 @Override setUp()115 public void setUp() throws Exception { 116 super.setUp(); 117 mLaunchRunner = new LaunchRunner(this); 118 } 119 120 @Override activitiesUsedInTest()121 List<ComponentName> activitiesUsedInTest() { 122 return mTestCase.getSetup().componentsInCase(); 123 } 124 readAllFromAssets()125 public static List<TestCase> readAllFromAssets() throws IOException, JSONException { 126 List<TestCase> testCases = Lists.newArrayList(); 127 AssetManager assets = TARGET_CONTEXT.getAssets(); 128 129 List<String> testFiles = Lists.newArrayList(); 130 for (String dir : assets.list("")) { 131 for (String file : assets.list(dir)) { 132 if (file.endsWith(".json")) { 133 testFiles.add(dir + "/" + file); 134 } 135 } 136 } 137 138 for (String testFile : testFiles) { 139 try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( 140 new BufferedInputStream(assets.open(testFile))))) { 141 String jsonData = bufferedReader.lines().collect( 142 Collectors.joining("\n")); 143 144 TestCase testCase = TestCase.fromJson( 145 new JSONObject(jsonData), TABLE, testFile); 146 147 testCases.add(testCase); 148 } 149 } 150 151 return testCases; 152 } 153 } 154