Home | History | Annotate | Download | only in documentsui
      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;
     28 
     29 import com.android.documentsui.BaseActivity.EventListener;
     30 import com.android.documentsui.base.RootInfo;
     31 import com.android.documentsui.files.FilesActivity;
     32 import com.android.documentsui.ActivityTest;
     33 
     34 import java.util.ArrayList;
     35 import java.util.Arrays;
     36 import java.util.List;
     37 import java.util.concurrent.CountDownLatch;
     38 
     39 @LargeTest
     40 public class FilesActivityPerfTest extends ActivityTest<FilesActivity> {
     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() {
     55         super(FilesActivity.class);
     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>();
     83         EventListener listener;
     84         for (int i = 0; i < 10; i++) {
     85             final CountDownLatch signal = new CountDownLatch(1);
     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 
    123             assertEquals(i + 1, measurements.size());
    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 }
    143