Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [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.documentsui; |
| 18 | |
| 19 | import static com.android.documentsui.StressProvider.DEFAULT_AUTHORITY; |
| 20 | import static com.android.documentsui.StressProvider.STRESS_ROOT_0_ID; |
| 21 | import static com.android.documentsui.StressProvider.STRESS_ROOT_1_ID; |
| 22 | |
| 23 | import android.app.Activity; |
| 24 | import android.net.Uri; |
| 25 | import android.os.Bundle; |
| 26 | import android.os.RemoteException; |
| 27 | import android.test.suitebuilder.annotation.LargeTest; |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 28 | |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 29 | import com.android.documentsui.BaseActivity.EventListener; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 30 | import com.android.documentsui.base.RootInfo; |
Steve McKay | b6006b2 | 2016-09-29 09:23:45 -0700 | [diff] [blame] | 31 | import com.android.documentsui.files.FilesActivity; |
Steve McKay | ae84a18 | 2016-09-16 09:36:50 -0700 | [diff] [blame] | 32 | import com.android.documentsui.ActivityTest; |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.Arrays; |
| 36 | import java.util.List; |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 37 | import java.util.concurrent.CountDownLatch; |
| 38 | |
| 39 | @LargeTest |
Steve McKay | b6006b2 | 2016-09-29 09:23:45 -0700 | [diff] [blame] | 40 | public class FilesActivityPerfTest extends ActivityTest<FilesActivity> { |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 41 | |
| 42 | // Constants starting with KEY_ are used to report metrics to APCT. |
| 43 | private static final String KEY_FILES_LISTED_PERFORMANCE_FIRST = |
| 44 | "files-listed-performance-first"; |
| 45 | |
| 46 | private static final String KEY_FILES_LISTED_PERFORMANCE_MEDIAN = |
| 47 | "files-listed-performance-median"; |
| 48 | |
| 49 | private static final String TESTED_URI = |
| 50 | "content://com.android.documentsui.stressprovider/document/STRESS_ROOT_1_DOC"; |
| 51 | |
| 52 | private static final int NUM_MEASUREMENTS = 10; |
| 53 | |
| 54 | public FilesActivityPerfTest() { |
Steve McKay | b6006b2 | 2016-09-29 09:23:45 -0700 | [diff] [blame] | 55 | super(FilesActivity.class); |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected RootInfo getInitialRoot() { |
| 60 | return rootDir0; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected String getTestingProviderAuthority() { |
| 65 | return DEFAULT_AUTHORITY; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void setupTestingRoots() throws RemoteException { |
| 70 | rootDir0 = mDocsHelper.getRoot(STRESS_ROOT_0_ID); |
| 71 | rootDir1 = mDocsHelper.getRoot(STRESS_ROOT_1_ID); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void initTestFiles() throws RemoteException { |
| 76 | // Nothing to create, already done by StressProvider. |
| 77 | } |
| 78 | |
| 79 | public void testFilesListedPerformance() throws Exception { |
| 80 | final BaseActivity activity = getActivity(); |
| 81 | |
| 82 | final List<Long> measurements = new ArrayList<Long>(); |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 83 | EventListener listener; |
| 84 | for (int i = 0; i < 10; i++) { |
Tomasz Mikolajewski | 575425a | 2016-03-02 17:57:46 +0900 | [diff] [blame] | 85 | final CountDownLatch signal = new CountDownLatch(1); |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 86 | listener = new EventListener() { |
| 87 | @Override |
| 88 | public void onDirectoryNavigated(Uri uri) { |
| 89 | if (uri != null && TESTED_URI.equals(uri.toString())) { |
| 90 | mStartTime = System.currentTimeMillis(); |
| 91 | } else { |
| 92 | mStartTime = -1; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onDirectoryLoaded(Uri uri) { |
| 98 | if (uri == null || !TESTED_URI.equals(uri.toString())) { |
| 99 | return; |
| 100 | } |
| 101 | assertTrue(mStartTime != -1); |
| 102 | getInstrumentation().waitForIdle(new Runnable() { |
| 103 | @Override |
| 104 | public void run() { |
| 105 | assertTrue(mStartTime != -1); |
| 106 | measurements.add(System.currentTimeMillis() - mStartTime); |
| 107 | signal.countDown(); |
| 108 | } |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | private long mStartTime = -1; |
| 113 | }; |
| 114 | |
| 115 | try { |
| 116 | activity.addEventListener(listener); |
| 117 | bots.roots.openRoot(STRESS_ROOT_1_ID); |
| 118 | signal.await(); |
| 119 | } finally { |
| 120 | activity.removeEventListener(listener); |
| 121 | } |
| 122 | |
Tomasz Mikolajewski | 7a57c51 | 2016-03-03 18:26:56 +0900 | [diff] [blame] | 123 | assertEquals(i + 1, measurements.size()); |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 124 | |
| 125 | // Go back to the empty root. |
| 126 | bots.roots.openRoot(STRESS_ROOT_0_ID); |
| 127 | } |
| 128 | |
| 129 | assertEquals(NUM_MEASUREMENTS, measurements.size()); |
| 130 | |
| 131 | final Bundle status = new Bundle(); |
| 132 | status.putDouble(KEY_FILES_LISTED_PERFORMANCE_FIRST, measurements.get(0)); |
| 133 | |
| 134 | final Long[] rawMeasurements = measurements.toArray(new Long[NUM_MEASUREMENTS]); |
| 135 | Arrays.sort(rawMeasurements); |
| 136 | |
| 137 | final long median = rawMeasurements[NUM_MEASUREMENTS / 2 - 1]; |
| 138 | status.putDouble(KEY_FILES_LISTED_PERFORMANCE_MEDIAN, median); |
| 139 | |
| 140 | getInstrumentation().sendStatus(Activity.RESULT_OK, status); |
| 141 | } |
| 142 | } |