/*
 * Copyright (C) 2016 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 com.android.documentsui;

import static com.android.documentsui.StressProvider.STRESS_ROOT_2_ID;

import android.app.Activity;
import android.app.UiAutomation;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.support.test.jank.GfxMonitor;
import android.support.test.jank.JankTest;
import android.support.test.jank.JankTestBase;

import androidx.test.filters.LargeTest;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiScrollable;

import com.android.documentsui.bots.DirectoryListBot;
import com.android.documentsui.bots.SidebarBot;
import com.android.documentsui.files.FilesActivity;

@LargeTest
public class FilesJankPerfTest extends JankTestBase {
    private static final String DOCUMENTSUI_PACKAGE = "com.android.documentsui";
    private static final int MAX_FLINGS = 10;
    private static final int BOT_TIMEOUT = 5000;

    private SidebarBot mRootsListBot;
    private DirectoryListBot mDirListBot;
    private Activity mActivity = null;

    public void setUpInLoop() {
        final UiDevice device = UiDevice.getInstance(getInstrumentation());
        final Context context = getInstrumentation().getTargetContext();
        final UiAutomation automation = getInstrumentation().getUiAutomation();
        mRootsListBot = new SidebarBot(device, context, BOT_TIMEOUT);
        mDirListBot = new DirectoryListBot(device, automation, context, BOT_TIMEOUT);

        final Intent intent = new Intent(context, FilesActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mActivity = getInstrumentation().startActivitySync(intent);
        try {
            device.setOrientationNatural();
        } catch (RemoteException e) {
        }
    }

    public void tearDownInLoop() {
        if (mActivity != null) {
            mActivity.finish();
            mActivity = null;
        }
        try {
            final UiDevice device = UiDevice.getInstance(getInstrumentation());
            device.unfreezeRotation();
        } catch (RemoteException e) {
        }
    }

    public void setupAndOpenInLoop() throws Exception {
        setUpInLoop();
        openRoot();
    }

    public void openRoot() throws Exception {
        mRootsListBot.openRoot(STRESS_ROOT_2_ID);
    }

    @JankTest(expectedFrames=0, beforeLoop="setUpInLoop", afterLoop="tearDownInLoop")
    @GfxMonitor(processName=DOCUMENTSUI_PACKAGE)
    public void testOpenRootJankPerformance() throws Exception {
        openRoot();
        getInstrumentation().waitForIdleSync();
    }

    @JankTest(expectedFrames=0, beforeLoop="setupAndOpenInLoop", afterLoop="tearDownInLoop")
    @GfxMonitor(processName=DOCUMENTSUI_PACKAGE)
    public void testFlingJankPerformance() throws Exception {
        new UiScrollable(mDirListBot.findDocumentsList().getSelector()).flingToEnd(MAX_FLINGS);
        getInstrumentation().waitForIdleSync();
    }
}
