blob: da5986f71f11676ca59974d8ecb1b139b3ae1e88 [file] [log] [blame]
Fyodor Kupolovfd6f4fb2016-11-18 14:39:20 -08001/*
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
17package com.android.server.pm;
18
19import android.content.pm.PackageParser;
Winson14ff7172019-10-23 10:42:27 -070020import android.content.pm.parsing.ParsedPackage;
Fyodor Kupolovfd6f4fb2016-11-18 14:39:20 -080021import android.util.Log;
22
Brett Chabota26eda92018-07-23 13:08:30 -070023import androidx.test.runner.AndroidJUnit4;
24
Fyodor Kupolovfd6f4fb2016-11-18 14:39:20 -080025import junit.framework.Assert;
26
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30
31import java.io.File;
32import java.util.HashSet;
33import java.util.Set;
Winson29974422020-01-21 17:17:04 -080034import java.util.concurrent.ExecutorService;
Fyodor Kupolovfd6f4fb2016-11-18 14:39:20 -080035
36/**
37 * Tests for {@link ParallelPackageParser}
38 */
39@RunWith(AndroidJUnit4.class)
40public class ParallelPackageParserTest {
41 private static final String TAG = ParallelPackageParserTest.class.getSimpleName();
42
43 private ParallelPackageParser mParser;
44
45 @Before
46 public void setUp() {
Winson29974422020-01-21 17:17:04 -080047 mParser = new TestParallelPackageParser(new PackageParser(),
48 ParallelPackageParser.makeExecutorService());
Fyodor Kupolovfd6f4fb2016-11-18 14:39:20 -080049 }
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
Winson29974422020-01-21 17:17:04 -080073 private class TestParallelPackageParser extends ParallelPackageParser {
Fyodor Kupolovfd6f4fb2016-11-18 14:39:20 -080074
Winson29974422020-01-21 17:17:04 -080075 TestParallelPackageParser(PackageParser packageParser, ExecutorService executorService) {
76 super(packageParser, executorService);
Fyodor Kupolovfd6f4fb2016-11-18 14:39:20 -080077 }
78
79 @Override
Winson29974422020-01-21 17:17:04 -080080 protected ParsedPackage parsePackage(File scanFile, int parseFlags) {
Fyodor Kupolovfd6f4fb2016-11-18 14:39:20 -080081 // Do not actually parse the package for testing
82 return null;
83 }
84 }
85}