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