blob: 33ef73e5eaa2b4af817edde011a139e58ba74ae5 [file] [log] [blame]
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +09001/*
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.documentsui;
18
19import static com.android.documentsui.StressProvider.DEFAULT_AUTHORITY;
20import static com.android.documentsui.StressProvider.STRESS_ROOT_0_ID;
21import static com.android.documentsui.StressProvider.STRESS_ROOT_1_ID;
22
23import android.app.Activity;
24import android.net.Uri;
25import android.os.Bundle;
26import android.os.RemoteException;
27import android.test.suitebuilder.annotation.LargeTest;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090028
Steve McKayd9caa6a2016-09-15 16:36:45 -070029import com.android.documentsui.BaseActivity.EventListener;
Steve McKayd0805062016-09-15 14:30:38 -070030import com.android.documentsui.base.RootInfo;
Steve McKayb6006b22016-09-29 09:23:45 -070031import com.android.documentsui.files.FilesActivity;
Steve McKayae84a182016-09-16 09:36:50 -070032import com.android.documentsui.ActivityTest;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090033
34import java.util.ArrayList;
35import java.util.Arrays;
36import java.util.List;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090037import java.util.concurrent.CountDownLatch;
38
39@LargeTest
Steve McKayb6006b22016-09-29 09:23:45 -070040public class FilesActivityPerfTest extends ActivityTest<FilesActivity> {
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090041
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 McKayb6006b22016-09-29 09:23:45 -070055 super(FilesActivity.class);
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090056 }
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 Mikolajewskie29e3412016-02-24 12:53:44 +090083 EventListener listener;
84 for (int i = 0; i < 10; i++) {
Tomasz Mikolajewski575425a2016-03-02 17:57:46 +090085 final CountDownLatch signal = new CountDownLatch(1);
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090086 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 Mikolajewski7a57c512016-03-03 18:26:56 +0900123 assertEquals(i + 1, measurements.size());
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900124
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}