1/*
2 * Copyright (C) 2015 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 */
16package com.android.tv.tests.ui;
17
18import static com.android.tv.testing.uihelper.UiDeviceAsserts.assertWaitForCondition;
19
20import android.os.SystemClock;
21import android.support.test.uiautomator.Until;
22import android.test.suitebuilder.annotation.LargeTest;
23import android.util.Log;
24
25import com.android.tv.R;
26import com.android.tv.testing.uihelper.Constants;
27
28/**
29 * Tests for {@link com.android.tv.MainActivity}.
30 */
31@LargeTest
32public class ProgramGuidePerformanceTest extends LiveChannelsTestCase {
33    private static final String TAG = "ProgramGuidePerformance";
34
35    public static final int SHOW_MENU_MAX_DURATION_MS = 1500;
36    public void testShowMenu() {
37        mLiveChannelsHelper.assertAppStarted();
38        mMenuHelper.showMenu();
39        mMenuHelper.assertNavigateToMenuItem(R.string.menu_title_channels,
40                R.string.channels_item_program_guide);
41        //TODO: build a simple performance framework like JankTest
42        long start = SystemClock.elapsedRealtime();
43        Log.v(TAG, "start " + start + " milliSeconds");
44        mDevice.pressDPadCenter();
45        assertWaitForCondition(mDevice, Until.hasObject(Constants.PROGRAM_GUIDE));
46        long end = SystemClock.elapsedRealtime();
47        Log.v(TAG, "end " + end + " milliSeconds");
48        long duration = end - start;
49        assertDuration("ShowMenu", SHOW_MENU_MAX_DURATION_MS, duration);
50        mDevice.pressBack();
51    }
52
53    private void assertDuration(String msg, long expectedMaxMilliSeconds, long actualMilliSeconds) {
54        Log.d(TAG, msg + " duration " + actualMilliSeconds + " milliSeconds");
55        assertTrue(msg + " duration expected to be <= " + expectedMaxMilliSeconds
56                + " milliSeconds but was " + actualMilliSeconds + " milliSeconds.",
57                actualMilliSeconds <= expectedMaxMilliSeconds);
58    }
59}
60