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

Commit 1f9d4974 authored by George Chang's avatar George Chang Committed by Android (Google) Code Review
Browse files

Merge "Don't restore nfc qs tile if it is not in the stock"

parents 5b876f52 173e49eb
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import javax.inject.Inject;
/** Quick settings tile: Enable/Disable NFC **/
public class NfcTile extends QSTileImpl<BooleanState> {

    private static final String NFC = "nfc";
    private final Icon mIcon = ResourceIcon.get(R.drawable.ic_qs_nfc);

    private NfcAdapter mAdapter;
@@ -89,8 +90,14 @@ public class NfcTile extends QSTileImpl<BooleanState> {

    @Override
    public boolean isAvailable() {
        String stockTiles = mContext.getString(R.string.quick_settings_tiles_stock);
        // For the restore from backup case
        // Return false when "nfc" is not listed in quick_settings_tiles_stock.
        if (stockTiles.contains(NFC)) {
            return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC);
        }
        return false;
    }

    @Override
    protected void handleUserSwitch(int newUserId) {
+110 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.qs.tiles;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;

import androidx.test.filters.SmallTest;

import com.android.internal.logging.MetricsLogger;
import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.qs.QSTileHost;
import com.android.systemui.qs.logging.QSLogger;

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

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
@SmallTest
public class NfcTileTest extends SysuiTestCase {

    private static final String TILES_STOCK_WITHOUT_NFC = "wifi,cell,battery,dnd,flashlight,bt";
    private static final String TILES_STOCK_WITH_NFC = "wifi,cell,battery,dnd,nfc,flashlight,bt";

    @Mock
    private Context mMockContext;
    @Mock
    private PackageManager mPackageManager;
    @Mock
    private ActivityStarter mActivityStarter;
    @Mock
    private QSTileHost mHost;
    @Mock
    private MetricsLogger mMetricsLogger;
    @Mock
    private StatusBarStateController mStatusBarStateController;
    @Mock
    private QSLogger mQSLogger;
    @Mock
    private BroadcastDispatcher mBroadcastDispatcher;

    private TestableLooper mTestableLooper;
    private NfcTile mNfcTile;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mTestableLooper = TestableLooper.get(this);

        when(mHost.getContext()).thenReturn(mMockContext);
        when(mMockContext.getPackageManager()).thenReturn(mPackageManager);

        mNfcTile = new NfcTile(
                mHost,
                mTestableLooper.getLooper(),
                new Handler(mTestableLooper.getLooper()),
                mMetricsLogger,
                mStatusBarStateController,
                mActivityStarter,
                mQSLogger,
                mBroadcastDispatcher
        );
    }

    @Test
    public void testIsAvailable_stockWithoutNfc_returnsFalse() {
        when(mMockContext.getString(R.string.quick_settings_tiles_stock)).thenReturn(
                TILES_STOCK_WITHOUT_NFC);
        when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
        assertFalse(mNfcTile.isAvailable());
    }

    @Test
    public void testIsAvailable_stockWithNfc_returnsTrue() {
        when(mMockContext.getString(R.string.quick_settings_tiles_stock)).thenReturn(
                TILES_STOCK_WITH_NFC);
        when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
        assertTrue(mNfcTile.isAvailable());
    }
}