1 /* 2 * Copyright (C) 2016 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.server.pm; 18 19 import android.platform.test.annotations.Presubmit; 20 import android.util.Log; 21 22 import androidx.test.runner.AndroidJUnit4; 23 24 import com.android.internal.pm.parsing.PackageParser2; 25 import com.android.internal.pm.parsing.pkg.ParsedPackage; 26 import com.android.server.pm.parsing.TestPackageParser2; 27 28 import junit.framework.Assert; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.io.File; 35 import java.util.HashSet; 36 import java.util.Set; 37 import java.util.concurrent.ExecutorService; 38 39 /** 40 * Tests for {@link ParallelPackageParser} 41 */ 42 @Presubmit 43 @RunWith(AndroidJUnit4.class) 44 public class ParallelPackageParserTest { 45 private static final String TAG = ParallelPackageParserTest.class.getSimpleName(); 46 47 private ParallelPackageParser mParser; 48 49 @Before setUp()50 public void setUp() { 51 mParser = new TestParallelPackageParser(new TestPackageParser2(), 52 ParallelPackageParser.makeExecutorService()); 53 } 54 55 @Test(timeout = 1000) test()56 public void test() { 57 Set<File> submittedFiles = new HashSet<>(); 58 int fileCount = 15; 59 for (int i = 0; i < fileCount; i++) { 60 File file = new File("f" + i); 61 mParser.submit(file, 0); 62 submittedFiles.add(file); 63 Log.d(TAG, "submitting " + file); 64 } 65 for (int i = 0; i < fileCount; i++) { 66 ParallelPackageParser.ParseResult result = mParser.take(); 67 Assert.assertNotNull(result); 68 File parsedFile = result.scanFile; 69 Log.d(TAG, "took " + parsedFile); 70 Assert.assertNotNull(parsedFile); 71 boolean removeSuccessful = submittedFiles.remove(parsedFile); 72 Assert.assertTrue("Unexpected file " + parsedFile + ". Expected submitted files: " 73 + submittedFiles, removeSuccessful); 74 } 75 } 76 77 private class TestParallelPackageParser extends ParallelPackageParser { 78 TestParallelPackageParser(PackageParser2 packageParser, ExecutorService executorService)79 TestParallelPackageParser(PackageParser2 packageParser, ExecutorService executorService) { 80 super(packageParser, executorService); 81 } 82 83 @Override parsePackage(File scanFile, int parseFlags)84 protected ParsedPackage parsePackage(File scanFile, int parseFlags) { 85 // Do not actually parse the package for testing 86 return null; 87 } 88 } 89 } 90