Merge "Migrate robolectric tests to junit tests"

This commit is contained in:
Arc Wang
2020-12-16 13:34:25 +00:00
committed by Android (Google) Code Review
108 changed files with 3082 additions and 338 deletions

View File

@@ -1,120 +0,0 @@
/*
* 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.development;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.SwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class OverlaySettingsPreferenceControllerTest {
private Context mContext;
private SwitchPreference mPreference;
private OverlaySettingsPreferenceController mController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mController = new OverlaySettingsPreferenceController(mContext);
mPreference = new SwitchPreference(mContext);
}
@Test
public void isAvailable_shouldReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateState_isOverlaySettingsEnabled_shouldCheckPreference() {
OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, true);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_isOverlaySettingsDisabled_shouldUncheckPreference() {
OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, false);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void onPreferenceChange_preferenceChecked_shouldEnableSettings() {
mController.onPreferenceChange(mPreference, true);
assertThat(OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isTrue();
}
@Test
public void onPreferenceChange_preferenceUnchecked_shouldDisableSettings() {
mController.onPreferenceChange(mPreference, false);
assertThat(
OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isFalse();
}
@Test
public void isOverlaySettingsEnabled_sharePreferenceSetTrue_shouldReturnTrue() {
final SharedPreferences editor = mContext.getSharedPreferences(
OverlaySettingsPreferenceController.SHARE_PERFS,
Context.MODE_PRIVATE);
editor.edit().putBoolean(OverlaySettingsPreferenceController.SHARE_PERFS, true).apply();
assertThat(OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isTrue();
}
@Test
public void isOverlaySettingsEnabled_sharePreferenceSetFalse_shouldReturnFalse() {
final SharedPreferences editor = mContext.getSharedPreferences(
OverlaySettingsPreferenceController.SHARE_PERFS,
Context.MODE_PRIVATE);
editor.edit().putBoolean(OverlaySettingsPreferenceController.SHARE_PERFS, false).apply();
assertThat(
OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isFalse();
}
@Test
public void setOverlaySettingsEnabled_setTrue_shouldStoreTrue() {
OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, true);
assertThat(
OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isTrue();
}
@Test
public void setOverlaySettingsEnabled_setFalse_shouldStoreTrue() {
OverlaySettingsPreferenceController.setOverlaySettingsEnabled(mContext, false);
assertThat(
OverlaySettingsPreferenceController.isOverlaySettingsEnabled(mContext)).isFalse();
}
}

View File

@@ -1,157 +0,0 @@
/*
* Copyright (C) 2018 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.development.featureflags;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.os.SystemProperties;
import android.provider.Settings;
import android.util.FeatureFlagUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class FeatureFlagPersistentTest {
private static final String TEST_FEATURE_NAME = "test_feature";
private static final String PERSISTENT_FALSE_NAME = "false_persistent";
private static final String PERSISTENT_TRUE_NAME = "true_persistent";
private static final String VOLATILE_FALSE_NAME = "volatile_false_volatile";
private static final String VOLATILE_TRUE_NAME = "volatile_true_volatile";
private Context mContext;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
FeatureFlagPersistent.getAllPersistentFlags().add(TEST_FEATURE_NAME);
FeatureFlagUtils.getAllFeatureFlags().put(TEST_FEATURE_NAME, "false");
FeatureFlagUtils.getAllFeatureFlags().put(VOLATILE_FALSE_NAME, "false");
FeatureFlagUtils.getAllFeatureFlags().put(VOLATILE_TRUE_NAME, "true");
FeatureFlagPersistent.getAllPersistentFlags().add(PERSISTENT_FALSE_NAME);
FeatureFlagUtils.getAllFeatureFlags().put(PERSISTENT_FALSE_NAME, "false");
FeatureFlagPersistent.getAllPersistentFlags().add(PERSISTENT_TRUE_NAME);
FeatureFlagUtils.getAllFeatureFlags().put(PERSISTENT_TRUE_NAME, "true");
}
@After
public void tearDown() {
cleanup(PERSISTENT_FALSE_NAME);
cleanup(PERSISTENT_TRUE_NAME);
cleanup(VOLATILE_FALSE_NAME);
cleanup(VOLATILE_TRUE_NAME);
cleanup(TEST_FEATURE_NAME);
}
private void cleanup(String flagName) {
Settings.Global.putString(mContext.getContentResolver(), flagName, "");
SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + flagName, "");
SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + flagName, "");
SystemProperties.set(FeatureFlagUtils.PERSIST_PREFIX + flagName, "");
}
/**
* Test to verify a non-persistent flag is indeed not persistent.
*/
@Test
public void isPersistent_notPersistent_shouldReturnFalse() {
assertThat(FeatureFlagPersistent.isPersistent(VOLATILE_FALSE_NAME)).isFalse();
}
/**
* Test to verify a persistent flag is indeed persistent.
*/
@Test
public void isPersistent_persistent_shouldReturnTrue() {
assertThat(FeatureFlagPersistent.isPersistent(PERSISTENT_TRUE_NAME)).isTrue();
}
/**
* Test to verify a persistent flag that is enabled should return true.
*/
@Test
public void isEnabled_enabled_shouldReturnTrue() {
assertThat(FeatureFlagPersistent.isEnabled(mContext, PERSISTENT_TRUE_NAME)).isTrue();
}
/**
* Test to verify a persistent flag that is disabled should return false.
*/
@Test
public void isEnabled_disabled_shouldReturnFalse() {
assertThat(FeatureFlagPersistent.isEnabled(mContext, PERSISTENT_FALSE_NAME)).isFalse();
}
/**
* Test to verify a persistent flag that has an enabled in system property should return true.
*/
@Test
public void isEnabled_sysPropEnabled_shouldReturnTrue() {
SystemProperties.set(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME, "true");
FeatureFlagUtils.setEnabled(mContext, TEST_FEATURE_NAME, false);
assertThat(FeatureFlagPersistent.isEnabled(mContext, TEST_FEATURE_NAME)).isTrue();
}
/**
* Test to verify a persistent flag that is disabled in system property should return false.
*/
@Test
public void isEnabled_sysPropDisabled_shouldReturnFalse() {
SystemProperties.set(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME, "false");
FeatureFlagUtils.setEnabled(mContext, TEST_FEATURE_NAME, true);
assertThat(FeatureFlagPersistent.isEnabled(mContext, TEST_FEATURE_NAME)).isFalse();
}
/**
* Test to verify setting persistent flag to enable works.
*/
@Test
public void setEnabled_sysPropTrue_shouldSetValues() {
SystemProperties.set(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME, "");
FeatureFlagPersistent.setEnabled(mContext, TEST_FEATURE_NAME, true);
assertThat(SystemProperties.get(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME)).isEqualTo("true");
assertThat(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME)).isTrue();
}
/**
* Test to verify setting persistent flag to disable works.
*/
@Test
public void setEnabled_sysPropFalse_shouldSetValues() {
SystemProperties.set(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME, "");
FeatureFlagPersistent.setEnabled(mContext, TEST_FEATURE_NAME, false);
assertThat(SystemProperties.get(FeatureFlagUtils.PERSIST_PREFIX + TEST_FEATURE_NAME)).isEqualTo("false");
assertThat(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME)).isFalse();
}
}

View File

@@ -1,77 +0,0 @@
/*
* 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.development.transcode;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.os.SystemProperties;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class TranscodeDefaultOptionPreferenceControllerTest {
private static final String TRANSCODE_DEFAULT_SYS_PROP_KEY =
"persist.sys.fuse.transcode_default";
private TranscodeDefaultOptionPreferenceController mUnderTest;
@Before
public void setUp() {
Context context = ApplicationProvider.getApplicationContext();
mUnderTest = new TranscodeDefaultOptionPreferenceController(context, "some_key");
}
@Test
public void isChecked_whenSysPropSet_shouldReturnFalse() {
SystemProperties.set(TRANSCODE_DEFAULT_SYS_PROP_KEY, "true");
assertThat(mUnderTest.isChecked()).isFalse();
}
@Test
public void isChecked_whenSysPropUnset_shouldReturnTrue() {
SystemProperties.set(TRANSCODE_DEFAULT_SYS_PROP_KEY, "false");
assertThat(mUnderTest.isChecked()).isTrue();
}
@Test
public void setChecked_withTrue_shouldUnsetSysProp() {
mUnderTest.setChecked(true);
assertThat(
SystemProperties.getBoolean(TRANSCODE_DEFAULT_SYS_PROP_KEY, true)).isFalse();
}
@Test
public void setChecked_withFalse_shouldSetSysProp() {
mUnderTest.setChecked(false);
assertThat(
SystemProperties.getBoolean(TRANSCODE_DEFAULT_SYS_PROP_KEY, false)).isTrue();
}
@Test
public void getAvailabilityStatus_shouldReturn_isAvailable() {
assertThat(mUnderTest.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
}

View File

@@ -1,78 +0,0 @@
/*
* 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.development.transcode;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.os.SystemProperties;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class TranscodeGlobalTogglePreferenceControllerTest {
private static final String TRANSCODE_ENABLED_PROP_KEY = "persist.sys.fuse.transcode_enabled";
private TranscodeGlobalTogglePreferenceController mController;
@Before
public void setUp() {
Context context = ApplicationProvider.getApplicationContext();
mController = new TranscodeGlobalTogglePreferenceController(context, "test_key");
}
@Test
public void isAvailable_shouldReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isChecked_whenDisabled_shouldReturnFalse() {
SystemProperties.set(TRANSCODE_ENABLED_PROP_KEY, "false");
assertThat(mController.isChecked()).isFalse();
}
@Test
public void isChecked_whenEnabled_shouldReturnTrue() {
SystemProperties.set(TRANSCODE_ENABLED_PROP_KEY, "true");
assertThat(mController.isChecked()).isTrue();
}
@Test
public void setChecked_withTrue_shouldUpdateSystemProperty() {
// Simulate the UI being clicked.
mController.setChecked(true);
// Verify the system property was updated.
assertThat(SystemProperties.getBoolean(TRANSCODE_ENABLED_PROP_KEY, false)).isTrue();
}
@Test
public void setChecked_withFalse_shouldUpdateSystemProperty() {
// Simulate the UI being clicked.
mController.setChecked(false);
// Verify the system property was updated.
assertThat(SystemProperties.getBoolean(TRANSCODE_ENABLED_PROP_KEY, true)).isFalse();
}
}

View File

@@ -1,77 +0,0 @@
/*
* 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.development.transcode;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.os.SystemProperties;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class TranscodeUserControlPreferenceControllerTest {
private static final String TRANSCODE_USER_CONTROL_SYS_PROP_KEY =
"persist.sys.fuse.transcode_user_control";
private TranscodeUserControlPreferenceController mUnderTest;
@Before
public void setUp() {
Context context = ApplicationProvider.getApplicationContext();
mUnderTest = new TranscodeUserControlPreferenceController(context, "some_key");
}
@Test
public void isChecked_whenSysPropSet_shouldReturnTrue() {
SystemProperties.set(TRANSCODE_USER_CONTROL_SYS_PROP_KEY, "true");
assertThat(mUnderTest.isChecked()).isTrue();
}
@Test
public void isChecked_whenSysPropUnset_shouldReturnFalse() {
SystemProperties.set(TRANSCODE_USER_CONTROL_SYS_PROP_KEY, "false");
assertThat(mUnderTest.isChecked()).isFalse();
}
@Test
public void setChecked_withTrue_shouldSetSysProp() {
mUnderTest.setChecked(true);
assertThat(
SystemProperties.getBoolean(TRANSCODE_USER_CONTROL_SYS_PROP_KEY, false)).isTrue();
}
@Test
public void setChecked_withFalse_shouldUnsetSysProp() {
mUnderTest.setChecked(false);
assertThat(
SystemProperties.getBoolean(TRANSCODE_USER_CONTROL_SYS_PROP_KEY, true)).isFalse();
}
@Test
public void getAvailabilityStatus_shouldReturn_isAvailable() {
assertThat(mUnderTest.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
}