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

Commit d59d32e1 authored by govenliu's avatar govenliu
Browse files

[Wi-Fi] Add new unit test file WifiP2pSettingsTest.java.

Add new unit test file WifiP2pSettingsTest.java.

Bug: 151696220
Test: make RunSettingsRoboTests ROBOTEST_FILTER=WifiP2pSettingsTest
Change-Id: I686658969a801e385a102022c6854d8bb8972d3c
parent 9903bca4
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@@ -79,9 +80,9 @@ public class WifiP2pSettings extends DashboardFragment
    private OnClickListener mDisconnectListener;
    private OnClickListener mCancelConnectListener;
    private OnClickListener mDeleteGroupListener;
    private WifiP2pPeer mSelectedWifiPeer;
    @VisibleForTesting WifiP2pPeer mSelectedWifiPeer;
    private WifiP2pPersistentGroup mSelectedGroup;
    private String mSelectedGroupName;
    @VisibleForTesting String mSelectedGroupName;
    private EditText mDeviceNameText;

    private boolean mWifiP2pEnabled;
@@ -100,13 +101,13 @@ public class WifiP2pSettings extends DashboardFragment
    private static final int DIALOG_DELETE_GROUP = 4;

    private static final String SAVE_DIALOG_PEER = "PEER_STATE";
    private static final String SAVE_DEVICE_NAME = "DEV_NAME";
    private static final String SAVE_SELECTED_GROUP = "GROUP_NAME";
    @VisibleForTesting static final String SAVE_DEVICE_NAME = "DEV_NAME";
    @VisibleForTesting static final String SAVE_SELECTED_GROUP = "GROUP_NAME";

    private WifiP2pDevice mThisDevice;
    private WifiP2pDeviceList mPeers = new WifiP2pDeviceList();

    private String mSavedDeviceName;
    @VisibleForTesting String mSavedDeviceName;

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
+105 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.settings.wifi.p2p;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.FragmentActivity;

import com.android.settings.testutils.XmlTestUtils;
import com.android.settingslib.core.AbstractPreferenceController;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

import java.util.ArrayList;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
public class WifiP2pSettingsTest {

    private Context mContext;
    private FragmentActivity mActivity;
    private WifiP2pSettings mFragment;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        mActivity = Robolectric.setupActivity(FragmentActivity.class);
        mFragment = spy(new WifiP2pSettings());
    }

    @Test
    public void preferenceScreenKey_shouldContainsAllControllerKeys() {
        final List<String> preferenceScreenKeys = XmlTestUtils.getKeysFromPreferenceXml(mContext,
                mFragment.getPreferenceScreenResId());
        final List<String> preferenceKeys = new ArrayList<>();

        for (AbstractPreferenceController controller : mFragment.createPreferenceControllers(
                mContext)) {
            preferenceKeys.add(controller.getPreferenceKey());
        }

        assertThat(preferenceScreenKeys).containsAllIn(preferenceKeys);
    }

    @Test
    public void onActivityCreate_withNullBundle_canNotGetValue() {
        when(mFragment.getActivity()).thenReturn(mActivity);

        mFragment.onActivityCreated(null);

        assertThat(mFragment.mSelectedWifiPeer).isNull();
    }

    @Test
    public void onActivityCreate_withDeviceName_shouldGetDeviceName() {
        when(mFragment.getActivity()).thenReturn(mActivity);
        final String fakeDeviceName = "fakename";
        final Bundle bundle = new Bundle();
        bundle.putString(WifiP2pSettings.SAVE_DEVICE_NAME, fakeDeviceName);

        mFragment.onActivityCreated(bundle);

        assertThat(mFragment.mSavedDeviceName).isEqualTo(fakeDeviceName);
    }

    @Test
    public void onActivityCreate_withGroupName_shouldGetGroupName() {
        when(mFragment.getActivity()).thenReturn(mActivity);
        final String fakeGroupName = "fakegroup";
        final Bundle bundle = new Bundle();
        bundle.putString(WifiP2pSettings.SAVE_SELECTED_GROUP, fakeGroupName);

        mFragment.onActivityCreated(bundle);

        assertThat(mFragment.mSelectedGroupName).isEqualTo(fakeGroupName);
        assertThat(mFragment.mSavedDeviceName).isNull();
    }
}