Fyodor Kupolov | fd6f4fb | 2016-11-18 14:39:20 -0800 | [diff] [blame] | 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.content.pm.PackageParser; |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 20 | import android.content.pm.parsing.ParsedPackage; |
Fyodor Kupolov | fd6f4fb | 2016-11-18 14:39:20 -0800 | [diff] [blame] | 21 | import android.util.Log; |
| 22 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 23 | import androidx.test.runner.AndroidJUnit4; |
| 24 | |
Fyodor Kupolov | fd6f4fb | 2016-11-18 14:39:20 -0800 | [diff] [blame] | 25 | import junit.framework.Assert; |
| 26 | |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | |
| 31 | import java.io.File; |
| 32 | import java.util.HashSet; |
| 33 | import java.util.Set; |
Winson | 2997442 | 2020-01-21 17:17:04 -0800 | [diff] [blame] | 34 | import java.util.concurrent.ExecutorService; |
Fyodor Kupolov | fd6f4fb | 2016-11-18 14:39:20 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Tests for {@link ParallelPackageParser} |
| 38 | */ |
| 39 | @RunWith(AndroidJUnit4.class) |
| 40 | public class ParallelPackageParserTest { |
| 41 | private static final String TAG = ParallelPackageParserTest.class.getSimpleName(); |
| 42 | |
| 43 | private ParallelPackageParser mParser; |
| 44 | |
| 45 | @Before |
| 46 | public void setUp() { |
Winson | 2997442 | 2020-01-21 17:17:04 -0800 | [diff] [blame] | 47 | mParser = new TestParallelPackageParser(new PackageParser(), |
| 48 | ParallelPackageParser.makeExecutorService()); |
Fyodor Kupolov | fd6f4fb | 2016-11-18 14:39:20 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Test(timeout = 1000) |
| 52 | public void test() { |
| 53 | Set<File> submittedFiles = new HashSet<>(); |
| 54 | int fileCount = 15; |
| 55 | for (int i = 0; i < fileCount; i++) { |
| 56 | File file = new File("f" + i); |
| 57 | mParser.submit(file, 0); |
| 58 | submittedFiles.add(file); |
| 59 | Log.d(TAG, "submitting " + file); |
| 60 | } |
| 61 | for (int i = 0; i < fileCount; i++) { |
| 62 | ParallelPackageParser.ParseResult result = mParser.take(); |
| 63 | Assert.assertNotNull(result); |
| 64 | File parsedFile = result.scanFile; |
| 65 | Log.d(TAG, "took " + parsedFile); |
| 66 | Assert.assertNotNull(parsedFile); |
| 67 | boolean removeSuccessful = submittedFiles.remove(parsedFile); |
| 68 | Assert.assertTrue("Unexpected file " + parsedFile + ". Expected submitted files: " |
| 69 | + submittedFiles, removeSuccessful); |
| 70 | } |
| 71 | } |
| 72 | |
Winson | 2997442 | 2020-01-21 17:17:04 -0800 | [diff] [blame] | 73 | private class TestParallelPackageParser extends ParallelPackageParser { |
Fyodor Kupolov | fd6f4fb | 2016-11-18 14:39:20 -0800 | [diff] [blame] | 74 | |
Winson | 2997442 | 2020-01-21 17:17:04 -0800 | [diff] [blame] | 75 | TestParallelPackageParser(PackageParser packageParser, ExecutorService executorService) { |
| 76 | super(packageParser, executorService); |
Fyodor Kupolov | fd6f4fb | 2016-11-18 14:39:20 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
Winson | 2997442 | 2020-01-21 17:17:04 -0800 | [diff] [blame] | 80 | protected ParsedPackage parsePackage(File scanFile, int parseFlags) { |
Fyodor Kupolov | fd6f4fb | 2016-11-18 14:39:20 -0800 | [diff] [blame] | 81 | // Do not actually parse the package for testing |
| 82 | return null; |
| 83 | } |
| 84 | } |
| 85 | } |