summaryrefslogtreecommitdiff
path: root/src/android/support/test/jank/JankUtil.java
blob: 7f63174bda9e7e663bf7f7cc2de49ea80cf54118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.support.test.jank;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Instrumentation;
import android.app.UiAutomation;
import android.util.Log;
import android.view.accessibility.AccessibilityWindowInfo;
import android.view.FrameStats;

import java.util.HashMap;
import java.util.Map;

/** The {@link JankUtil} class provides functionality for monitoring jank. */
public class JankUtil {

    private static final String TAG = JankUtil.class.getSimpleName();

    // Singleton instance
    private static JankUtil sInstance;

    private UiAutomation mUiAutomation;
    private JankMonitor mMonitor;

    /** Private constructor. Clients should use {@link JankUtil#getInstance(Instrumentation)}. */
    private JankUtil(UiAutomation automation) {
        mUiAutomation = automation;

        // Subscribe to window information
        AccessibilityServiceInfo info = mUiAutomation.getServiceInfo();
        info.flags |= AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS;
        mUiAutomation.setServiceInfo(info);
    }

    /** Returns a {@link JankUtil} instance. */
    public static JankUtil getInstance(Instrumentation instrumentation) {
        if (sInstance == null) {
            sInstance = new JankUtil(instrumentation.getUiAutomation());
        }
        return sInstance;
    }

    /** Starts monitoring for janky frames of the given {@code type}. */
    public void startMonitor(JankType type) {
        if (mMonitor != null) {
            throw new IllegalStateException("Monitor already started");
        }

        if (type == JankType.CONTENT_FRAMES) {
            mMonitor = new WindowContentJankMonitor();
        } else if (type == JankType.ANIMATION_FRAMES) {
            mMonitor = new WindowAnimationJankMonitor();
        } else {
            throw new RuntimeException("Invalid type");
        }

        mMonitor.clear();
    }

    /** Stops monitoring and returns the {@link JankResult} for this monitoring session. */
    public JankResult stopMonitor() {
        FrameStats stats = mMonitor.getStats();
        mMonitor = null;
        return JankResult.analyze(stats);
    }

    /** Returns the id of the current application window. */
    private int getCurrentWindow() {
        for (AccessibilityWindowInfo window : mUiAutomation.getWindows()) {
            if (window.getType() == AccessibilityWindowInfo.TYPE_APPLICATION) {
                return window.getId();
            }
        }
        throw new RuntimeException("No application window found");
    }

    /** Generic monitoring interface */
    private static interface JankMonitor {
        public void clear();
        public FrameStats getStats();
    }

    /** Monitor for detecting window content frame jank. */
    private class WindowContentJankMonitor implements JankMonitor {
        private int mWindowId = -1;

        @Override
        public void clear() {
            mWindowId = getCurrentWindow();
            mUiAutomation.clearWindowContentFrameStats(mWindowId);
        }

        @Override
        public FrameStats getStats() {
            int currentWindow = getCurrentWindow();
            if (currentWindow != mWindowId) {
                Log.w(TAG, "Current window changed during the test. Did you mean to measure " +
                        "ANIMATION_FRAMES?");
            }
            mWindowId = -1;
            return mUiAutomation.getWindowContentFrameStats(currentWindow);
        }
    }

    /** Monitor for detecting window animation frame jank. */
    private class WindowAnimationJankMonitor implements JankMonitor {
        @Override
        public void clear() {
            mUiAutomation.clearWindowAnimationFrameStats();
        }

        @Override
        public FrameStats getStats() {
            return mUiAutomation.getWindowAnimationFrameStats();
        }
    }
}