From 4225944fc09064695365cbc742c38aba22ebd290 Mon Sep 17 00:00:00 2001 From: Syaoran Kuo Date: Wed, 23 Jun 2021 12:10:05 +0800 Subject: [PATCH] Add test case for on off airplane mode repeatly Bug: 191831494 Test: atest com.android.settings.network.AirplaneModePreferenceControllerComponentTest Change-Id: I407f4435bb02fed77fc91458e732b8a6573539cd --- ...ModePreferenceControllerComponentTest.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tests/componenttests/src/com/android/settings/network/AirplaneModePreferenceControllerComponentTest.java diff --git a/tests/componenttests/src/com/android/settings/network/AirplaneModePreferenceControllerComponentTest.java b/tests/componenttests/src/com/android/settings/network/AirplaneModePreferenceControllerComponentTest.java new file mode 100644 index 00000000000..27e67e586c7 --- /dev/null +++ b/tests/componenttests/src/com/android/settings/network/AirplaneModePreferenceControllerComponentTest.java @@ -0,0 +1,96 @@ +/* + * 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.settings.network; + +import static com.google.common.truth.Truth.assertThat; + +import android.app.Instrumentation; +import android.provider.Settings; +import android.provider.SettingsSlicesContract; +import android.util.Log; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; +import androidx.test.platform.app.InstrumentationRegistry; + +import com.android.settings.testutils.UiUtils; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class AirplaneModePreferenceControllerComponentTest { + // Airplane on off status + private static final int ON = 1; + private static final int OFF = 0; + public final String TAG = this.getClass().getName(); + private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation(); + private boolean mOriginAirplaneModeIsOn; + + @Before + public void setUp() { + // Make sure origin airplane mode is OFF. + mOriginAirplaneModeIsOn = is_airplane_mode_on(); + if (mOriginAirplaneModeIsOn) { + Log.d(TAG, "Origin airplane mode is on, turn it off."); + Settings.Global.putInt(mInstrumentation.getTargetContext().getContentResolver(), + Settings.Global.AIRPLANE_MODE_ON, OFF); + } + } + + /** + * Tests on/off airplane mode repeatedly. + * Previously, a bug describe that crash issue if user on off airplane mode repeatedly. + * This case try to switch on & off airplane mode for 10 times to check crash issue. + */ + @Test + public void test_on_off_airplane_mode_multiple_times() { + AirplaneModePreferenceController controller = + new AirplaneModePreferenceController(mInstrumentation.getTargetContext(), + SettingsSlicesContract.KEY_AIRPLANE_MODE); + + for (int i = 0; i < 10; ++i) { + Log.d(TAG, "Test #" + (i + 1)); + controller.setChecked(true); + assertThat(UiUtils.waitUntilCondition(1000, + () -> is_airplane_mode_on())).isTrue(); + + controller.setChecked(false); + assertThat(UiUtils.waitUntilCondition(1000, + () -> !is_airplane_mode_on())).isTrue(); + } + + } + + private boolean is_airplane_mode_on() { + return Settings.System.getInt( + mInstrumentation.getTargetContext().getContentResolver(), + Settings.Global.AIRPLANE_MODE_ON, OFF) != 0; + } + + @After + public void tearDown() { + if (is_airplane_mode_on() != mOriginAirplaneModeIsOn) { + Settings.Global.putInt(mInstrumentation.getTargetContext().getContentResolver(), + Settings.Global.AIRPLANE_MODE_ON, ( + mOriginAirplaneModeIsOn ? ON : OFF)); + } + } +}