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