Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bd977f35 authored by sfufa@google.com's avatar sfufa@google.com
Browse files

[Work Profile] Enable work profile tests

Bug: 159671700
Test: presbumit
Change-Id: Icfe39c30d603a4ae4cac576bc44037f545437ced
parent 01e0009b
Loading
Loading
Loading
Loading
+4 −0
Original line number Original line Diff line number Diff line
@@ -172,4 +172,8 @@ public class WorkProfileManager implements PersonalWorkSlidingTabStrip.OnActiveP
    private AllAppsContainerView.AdapterHolder getAH() {
    private AllAppsContainerView.AdapterHolder getAH() {
        return mAllApps.mAH[AllAppsContainerView.AdapterHolder.WORK];
        return mAllApps.mAH[AllAppsContainerView.AdapterHolder.WORK];
    }
    }

    public int getCurrentState() {
        return mCurrentState;
    }
}
}
+173 −0
Original line number Original line Diff line number Diff line
/*
/*
 * Copyright 2018, The Android Open Source Project
 * Copyright (C) 2021 The Android Open Source Project
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may not use this file except in compliance with the License.
@@ -22,42 +22,30 @@ import static com.android.launcher3.allapps.AllAppsStore.DEFER_UPDATES_TEST;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;


import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;
import android.util.Log;
import android.widget.TextView;
import android.view.View;

import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;


import com.android.launcher3.R;
import com.android.launcher3.R;
import com.android.launcher3.allapps.AllAppsContainerView;
import com.android.launcher3.allapps.AllAppsContainerView;
import com.android.launcher3.allapps.AllAppsPagedView;
import com.android.launcher3.allapps.AllAppsPagedView;
import com.android.launcher3.allapps.WorkModeSwitch;
import com.android.launcher3.allapps.WorkAdapterProvider;
import com.android.launcher3.dragndrop.DragLayer;
import com.android.launcher3.allapps.WorkEduCard;
import com.android.launcher3.allapps.WorkProfileManager;
import com.android.launcher3.tapl.LauncherInstrumentation;
import com.android.launcher3.tapl.LauncherInstrumentation;
import com.android.launcher3.testing.TestProtocol;
import com.android.launcher3.testing.TestProtocol;
import com.android.launcher3.views.WorkEduView;


import org.junit.After;
import org.junit.After;
import org.junit.Before;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;


import java.util.List;
import java.util.Objects;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class WorkTabTest extends AbstractLauncherUiTest {


    private int mProfileUserId;
public class WorkProfileTest extends AbstractLauncherUiTest {


    private static final int WORK_PAGE = AllAppsContainerView.AdapterHolder.WORK;
    private static final int WORK_PAGE = AllAppsContainerView.AdapterHolder.WORK;


    private int mProfileUserId;

    @Before
    @Before
    @Override
    @Override
    public void setUp() throws Exception {
    public void setUp() throws Exception {
@@ -91,146 +79,95 @@ public class WorkTabTest extends AbstractLauncherUiTest {
        });
        });
    }
    }


    @Ignore("b/182844465")
    private void waitForWorkTabSetup() {
        waitForLauncherCondition("Work tab not setup", launcher -> {
            if (launcher.getAppsView().getContentView() instanceof AllAppsPagedView) {
                launcher.getAppsView().getAppsStore().enableDeferUpdates(DEFER_UPDATES_TEST);
                return true;
            }
            return false;
        }, LauncherInstrumentation.WAIT_TIME_MS);
    }

    @Test
    @Test
    public void workTabExists() {
    public void workTabExists() {
        mDevice.pressHome();
        mDevice.pressHome();
        waitForLauncherCondition("Launcher didn't start", Objects::nonNull);
        waitForLauncherCondition("Launcher didn't start", Objects::nonNull);
        waitForState("Launcher internal state didn't switch to Normal", () -> NORMAL);
        executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));
        executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));
        waitForState("Launcher internal state didn't switch to All Apps", () -> ALL_APPS);
        waitForState("Launcher internal state didn't switch to All Apps", () -> ALL_APPS);
        waitForLauncherCondition("Personal tab is missing",
        waitForLauncherCondition("Personal tab is missing",
                launcher -> launcher.getAppsView().isPersonalTabVisible(), 60000);
                launcher -> launcher.getAppsView().isPersonalTabVisible(),
                LauncherInstrumentation.WAIT_TIME_MS);
        waitForLauncherCondition("Work tab is missing",
        waitForLauncherCondition("Work tab is missing",
                launcher -> launcher.getAppsView().isWorkTabVisible(), 60000);
                launcher -> launcher.getAppsView().isWorkTabVisible(),
                LauncherInstrumentation.WAIT_TIME_MS);
    }
    }


    @Ignore("b/182844465")
    @Test
    @Test
    public void toggleWorks() {
    public void toggleWorks() {
        mDevice.pressHome();
        mDevice.pressHome();
        waitForLauncherCondition("Launcher didn't start", Objects::nonNull);
        waitForLauncherCondition("Launcher didn't start", Objects::nonNull);
        waitForState("Launcher internal state didn't switch to Normal", () -> NORMAL);
        executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));
        executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));
        waitForState("Launcher internal state didn't switch to All Apps", () -> ALL_APPS);
        waitForState("Launcher internal state didn't switch to All Apps", () -> ALL_APPS);
        getOnceNotNull("Apps view did not bind",
                launcher -> launcher.getAppsView().getWorkModeSwitch(), 60000);

        UserManager userManager = getFromLauncher(l -> l.getSystemService(UserManager.class));
        assertEquals(2, userManager.getUserProfiles().size());
        UserHandle workProfile = getFromLauncher(l -> {
            UserHandle myHandle = Process.myUserHandle();
            List<UserHandle> userProfiles = userManager.getUserProfiles();
            return userProfiles.get(0) == myHandle ? userProfiles.get(1) : userProfiles.get(0);
        });


        waitForLauncherCondition("work profile can't be turned off",
        waitForWorkTabSetup();
                l -> userManager.requestQuietModeEnabled(true, workProfile));


        assertTrue(userManager.isQuietModeEnabled(workProfile));
        executeOnLauncher(launcher -> {
        executeOnLauncher(launcher -> {
            WorkModeSwitch wf = launcher.getAppsView().getWorkModeSwitch();
            AllAppsPagedView pagedView = (AllAppsPagedView) launcher.getAppsView().getContentView();
            ((AllAppsPagedView) launcher.getAppsView().getContentView()).snapToPageImmediately(
            pagedView.setCurrentPage(WORK_PAGE);
                    AllAppsContainerView.AdapterHolder.WORK);
            wf.toggle();
        });
        });
        waitForLauncherCondition("Work toggle did not work",
                l -> l.getSystemService(UserManager.class).isQuietModeEnabled(workProfile));
    }


    @Ignore("b/182844465")
        WorkProfileManager manager = getFromLauncher(l -> l.getAppsView().getWorkManager());
    @Test
    public void testWorkEduFlow() {
        mDevice.pressHome();
        waitForLauncherCondition("Launcher didn't start", Objects::nonNull);
        executeOnLauncher(launcher -> launcher.getSharedPrefs().edit().remove(
                WorkEduView.KEY_WORK_EDU_STEP).remove(
                WorkEduView.KEY_LEGACY_WORK_EDU_SEEN).commit());


        waitForLauncherCondition("Work tab not setup", launcher -> {

            if (launcher.getAppsView().getContentView() instanceof AllAppsPagedView) {
        waitForLauncherCondition("work profile initial state check failed", launcher ->
                launcher.getAppsView().getAppsStore().enableDeferUpdates(DEFER_UPDATES_TEST);
                        manager.getWorkModeSwitch() != null
                return true;
                                && manager.getCurrentState() == WorkProfileManager.STATE_ENABLED
            }
                                && manager.getWorkModeSwitch().isEnabled(),
            return false;
                LauncherInstrumentation.WAIT_TIME_MS);

        //start work profile toggle OFF test
        executeOnLauncher(l -> l.getAppsView().getWorkManager().getWorkModeSwitch().performClick());

        waitForLauncherCondition("Work profile toggle OFF failed", launcher -> {
            manager.reset(); // pulls current state from system
            return manager.getCurrentState() == WorkProfileManager.STATE_DISABLED;
        }, LauncherInstrumentation.WAIT_TIME_MS);
        }, LauncherInstrumentation.WAIT_TIME_MS);


        executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));
        // start work profile toggle ON test
        WorkEduView workEduView = getEduView();
        // verify personal app edu is seen first and click "next"
        executeOnLauncher(l -> {
        executeOnLauncher(l -> {
            assertEquals(((TextView) workEduView.findViewById(R.id.content_text)).getText(),
            AllAppsContainerView allApps = l.getAppsView();
                    l.getResources().getString(R.string.work_profile_edu_personal_apps));
            assertEquals("Work tab is not focused", allApps.getCurrentPage(), WORK_PAGE);
            workEduView.findViewById(R.id.proceed).callOnClick();
            View workPausedCard = allApps.getActiveRecyclerView().findViewHolderForAdapterPosition(
                    0).itemView;
            workPausedCard.findViewById(R.id.enable_work_apps).performClick();
        });
        });
        waitForLauncherCondition("Work profile toggle ON failed", launcher -> {
            manager.reset(); // pulls current state from system
            return manager.getCurrentState() == WorkProfileManager.STATE_ENABLED;
        }, LauncherInstrumentation.WAIT_TIME_MS);


        AtomicInteger attempt = new AtomicInteger(0);
        // verify work edu is seen next
        waitForLauncherCondition("Launcher did not show the next edu screen", l -> {
            Log.d(TestProtocol.WORK_PROFILE_REMOVED,
                    "running test attempt" + attempt.getAndIncrement());
            if (!(l.getAppsView().getContentView() instanceof AllAppsPagedView)) {
                Log.d(TestProtocol.WORK_PROFILE_REMOVED, "Work tab not setup. Skipping test");
                return false;
            }
            if (((AllAppsPagedView) l.getAppsView().getContentView()).getCurrentPage()
                    != WORK_PAGE) {
                Log.d(TestProtocol.WORK_PROFILE_REMOVED, "Work page not highlighted");
            }
            return ((TextView) workEduView.findViewById(R.id.content_text)).getText().equals(
                    l.getResources().getString(R.string.work_profile_edu_work_apps));
        });
    }
    }


    @Ignore("b/182844465")
    @Test
    @Test
    public void testWorkEduIntermittent() {
    public void testEdu() {
        mDevice.pressHome();
        mDevice.pressHome();
        waitForLauncherCondition("Launcher didn't start", Objects::nonNull);
        waitForLauncherCondition("Launcher didn't start", Objects::nonNull);
        executeOnLauncher(launcher -> launcher.getSharedPrefs().edit().remove(
        waitForState("Launcher internal state didn't switch to Normal", () -> NORMAL);
                WorkEduView.KEY_WORK_EDU_STEP).remove(
                WorkEduView.KEY_LEGACY_WORK_EDU_SEEN).commit());


        waitForLauncherCondition("Work tab not setup",
                launcher -> launcher.getAppsView().getContentView() instanceof AllAppsPagedView,
                60000);
        executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));
        executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));

        waitForState("Launcher internal state didn't switch to All Apps", () -> ALL_APPS);
        // verify personal app edu is seen
        waitForWorkTabSetup();
        getEduView();
        executeOnLauncher(l -> {

            l.getSharedPrefs().edit().putInt(WorkAdapterProvider.KEY_WORK_EDU_STEP, 0).commit();
        // dismiss personal edu
            ((AllAppsPagedView) l.getAppsView().getContentView()).setCurrentPage(WORK_PAGE);
        mDevice.pressHome();
            l.getAppsView().getWorkManager().reset();
        waitForState("Launcher did not go home", () -> NORMAL);

        // open work tab
        executeOnLauncher(launcher -> launcher.getStateManager().goToState(ALL_APPS));
        waitForState("Launcher did not switch to all apps", () -> ALL_APPS);
        waitForLauncherCondition("Work tab not setup",
                launcher -> launcher.getAppsView().getContentView() instanceof AllAppsPagedView,
                60000);

        executeOnLauncher(launcher -> {
            AllAppsPagedView pagedView = (AllAppsPagedView) launcher.getAppsView().getContentView();
            pagedView.setCurrentPage(WORK_PAGE);
        });
        });


        WorkEduView workEduView = getEduView();
        waitForLauncherCondition("Work profile education not shown",

                l -> l.getAppsView().getActiveRecyclerView()
        // verify work tab edu is shown
                        .findViewHolderForAdapterPosition(0).itemView instanceof WorkEduCard,
        waitForLauncherCondition("Launcher did not show the next edu screen",
                LauncherInstrumentation.WAIT_TIME_MS);
                l -> ((TextView) workEduView.findViewById(R.id.content_text)).getText().equals(
                        l.getResources().getString(R.string.work_profile_edu_work_apps)));
    }


    private WorkEduView getEduView() {
        waitForLauncherCondition("Edu did not show", l -> {
            DragLayer dragLayer = l.getDragLayer();
            return dragLayer.getChildCount() > 0 && dragLayer.getChildAt(
                    dragLayer.getChildCount() - 1) instanceof WorkEduView;
        }, 6000);
        return getFromLauncher(launcher -> (WorkEduView) launcher.getDragLayer().getChildAt(
                launcher.getDragLayer().getChildCount() - 1));
    }
    }

}
}