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

Commit a1a4860f authored by omarmt's avatar omarmt
Browse files

Add support for Predictive Back in WorkLockActivity

Test: atest WorkLockActivityTest
Bug: 255923588
Change-Id: If51dd2a9f5c167ce1aa5a68938059c4bf698c595
parent 71bf4aaf
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.widget.ImageView;
import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;

import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.R;
@@ -61,6 +63,7 @@ public class WorkLockActivity extends Activity {
    private UserManager mUserManager;
    private PackageManager mPackageManager;
    private final BroadcastDispatcher mBroadcastDispatcher;
    private final OnBackInvokedCallback mBackCallback = this::onBackInvoked;

    @Inject
    public WorkLockActivity(BroadcastDispatcher broadcastDispatcher, UserManager userManager,
@@ -95,6 +98,10 @@ public class WorkLockActivity extends Activity {
        if (badgedIcon != null) {
            ((ImageView) findViewById(R.id.icon)).setImageDrawable(badgedIcon);
        }

        getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
                OnBackInvokedDispatcher.PRIORITY_DEFAULT,
                mBackCallback);
    }

    @VisibleForTesting
@@ -134,11 +141,16 @@ public class WorkLockActivity extends Activity {
    @Override
    public void onDestroy() {
        unregisterBroadcastReceiver();
        getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(mBackCallback);
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        onBackInvoked();
    }

    private void onBackInvoked() {
        // Ignore back presses.
    }

+0 −106
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.systemui.keyguard;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.annotation.UserIdInt;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.os.UserHandle;
import android.os.UserManager;

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

import com.android.systemui.SysuiTestCase;
import com.android.systemui.broadcast.BroadcastDispatcher;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/**
 * runtest systemui -c com.android.systemui.keyguard.WorkLockActivityTest
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
public class WorkLockActivityTest extends SysuiTestCase {
    private static final @UserIdInt int USER_ID = 270;
    private static final String CALLING_PACKAGE_NAME = "com.android.test";

    private @Mock UserManager mUserManager;
    private @Mock PackageManager mPackageManager;
    private @Mock Context mContext;
    private @Mock BroadcastDispatcher mBroadcastDispatcher;
    private @Mock Drawable mDrawable;
    private @Mock Drawable mBadgedDrawable;

    private WorkLockActivity mActivity;

    private static class WorkLockActivityTestable extends WorkLockActivity {
        WorkLockActivityTestable(Context baseContext, BroadcastDispatcher broadcastDispatcher,
                UserManager userManager, PackageManager packageManager) {
            super(broadcastDispatcher, userManager, packageManager);
            attachBaseContext(baseContext);
        }
    }

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
        mActivity = new WorkLockActivityTestable(mContext, mBroadcastDispatcher, mUserManager,
                mPackageManager);
    }

    @Test
    public void testGetBadgedIcon() throws Exception {
        ApplicationInfo info = new ApplicationInfo();
        when(mPackageManager.getApplicationInfoAsUser(eq(CALLING_PACKAGE_NAME), any(),
                eq(USER_ID))).thenReturn(info);
        when(mPackageManager.getApplicationIcon(eq(info))).thenReturn(mDrawable);
        when(mUserManager.getBadgedIconForUser(any(), eq(UserHandle.of(USER_ID)))).thenReturn(
                mBadgedDrawable);
        mActivity.setIntent(new Intent()
                .putExtra(Intent.EXTRA_USER_ID, USER_ID)
                .putExtra(Intent.EXTRA_PACKAGE_NAME, CALLING_PACKAGE_NAME));

        assertEquals(mBadgedDrawable, mActivity.getBadgedIcon());
    }

    @Test
    public void testUnregisteredFromDispatcher() {
        mActivity.unregisterBroadcastReceiver();
        verify(mBroadcastDispatcher).unregisterReceiver(any());
        verify(mContext, never()).unregisterReceiver(any());
    }
}
+125 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.systemui.keyguard

import android.annotation.UserIdInt
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.PackageManager.ApplicationInfoFlags
import android.graphics.drawable.Drawable
import android.os.Looper
import android.os.UserHandle
import android.os.UserManager
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.systemui.SysuiTestCase
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.mock
import com.android.systemui.util.mockito.nullable
import com.android.systemui.util.mockito.whenever
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.never
import org.mockito.Mockito.verify

/** runtest systemui -c com.android.systemui.keyguard.WorkLockActivityTest */
@SmallTest
@RunWith(AndroidJUnit4::class)
class WorkLockActivityTest : SysuiTestCase() {
    private val context: Context = mock()
    private val userManager: UserManager = mock()
    private val packageManager: PackageManager = mock()
    private val broadcastDispatcher: BroadcastDispatcher = mock()
    private val drawable: Drawable = mock()
    private val badgedDrawable: Drawable = mock()
    private lateinit var activity: WorkLockActivity

    private class WorkLockActivityTestable
    constructor(
        baseContext: Context,
        broadcastDispatcher: BroadcastDispatcher,
        userManager: UserManager,
        packageManager: PackageManager,
    ) : WorkLockActivity(broadcastDispatcher, userManager, packageManager) {
        init {
            attachBaseContext(baseContext)
        }
    }

    @Before
    fun setUp() {
        if (Looper.myLooper() == null) {
            Looper.prepare()
        }
        activity =
            WorkLockActivityTestable(
                baseContext = context,
                broadcastDispatcher = broadcastDispatcher,
                userManager = userManager,
                packageManager = packageManager
            )
    }

    @Test
    fun testGetBadgedIcon() {
        val info = ApplicationInfo()
        whenever(
                packageManager.getApplicationInfoAsUser(
                    eq(CALLING_PACKAGE_NAME),
                    any<ApplicationInfoFlags>(),
                    eq(USER_ID)
                )
            )
            .thenReturn(info)
        whenever(packageManager.getApplicationIcon(ArgumentMatchers.eq(info))).thenReturn(drawable)
        whenever(userManager.getBadgedIconForUser(any(), eq(UserHandle.of(USER_ID))))
            .thenReturn(badgedDrawable)
        activity.intent =
            Intent()
                .putExtra(Intent.EXTRA_USER_ID, USER_ID)
                .putExtra(Intent.EXTRA_PACKAGE_NAME, CALLING_PACKAGE_NAME)
        assertEquals(badgedDrawable, activity.badgedIcon)
    }

    @Test
    fun testUnregisteredFromDispatcher() {
        activity.unregisterBroadcastReceiver()
        verify(broadcastDispatcher).unregisterReceiver(any())
        verify(context, never()).unregisterReceiver(nullable())
    }

    @Test
    fun onBackPressed_finishActivity() {
        assertFalse(activity.isFinishing)

        activity.onBackPressed()

        assertFalse(activity.isFinishing)
    }

    companion object {
        @UserIdInt private val USER_ID = 270
        private const val CALLING_PACKAGE_NAME = "com.android.test"
    }
}