1 /*
2  * Copyright (C) 2021 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.cts.packagemanager.stats.device;
18 
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.app.Instrumentation;
23 import android.os.Bundle;
24 
25 import androidx.test.InstrumentationRegistry;
26 
27 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
28 import org.apache.commons.compress.archivers.zip.ZipFile;
29 import org.junit.Test;
30 
31 import java.io.File;
32 import java.io.IOException;
33 
34 public class IncrementalAppErrorStatsTestsHelper {
35     private static final String HELPER_ARG = "remoteApkPath";
36     private static final String PAGE_INDEX_TO_BLOCK = "pageIndexToBlock";
37     // The apk contains a video resource file which has 9 pages. We only need to block 1 page
38     // such that the loading progress is never completed.
39     private static final String FILE_PAGE_TO_BLOCK = "res/raw/colors_video.mp4";
40     private static final int BLOCK_SIZE = 4096;
41     // Instrumentation status code used to write resolution to metrics
42     private static final int INST_STATUS_IN_PROGRESS = 2;
43 
44     @Test
getPageIndexToBlock()45     public void getPageIndexToBlock() throws IOException {
46         final Bundle testArgs = InstrumentationRegistry.getArguments();
47         final String apkPath = (String) testArgs.get(HELPER_ARG);
48         assertNotNull(apkPath);
49         assertTrue(new File(apkPath).exists());
50         ZipFile zip = new ZipFile(apkPath);
51         final ZipArchiveEntry info = zip.getEntry(FILE_PAGE_TO_BLOCK);
52         assertTrue(info.getSize() > BLOCK_SIZE);
53         assertTrue(info.getDataOffset() > BLOCK_SIZE * 2);
54         final int pageToBlock = (int) info.getDataOffset() / 4096 + 1;
55         // Pass data to the host-side test
56         Instrumentation inst = InstrumentationRegistry.getInstrumentation();
57         Bundle bundle = new Bundle();
58         bundle.putString(PAGE_INDEX_TO_BLOCK, String.valueOf(pageToBlock));
59         inst.sendStatus(INST_STATUS_IN_PROGRESS, bundle);
60     }
61 }
62