Add tests to the Sound Picker app.
Bug: 275540178 Test: com.android.soundpicker.RingtonePickerActivityTest Change-Id: I57e80577f5786f3e6d9d4eff93666868b0c7b3e4
This commit is contained in:
@@ -7,21 +7,24 @@ package {
|
||||
default_applicable_licenses: ["frameworks_base_license"],
|
||||
}
|
||||
|
||||
android_app {
|
||||
name: "SoundPicker",
|
||||
defaults: ["platform_app_defaults"],
|
||||
manifest: "AndroidManifest.xml",
|
||||
|
||||
static_libs: [
|
||||
"androidx.appcompat_appcompat",
|
||||
android_library {
|
||||
name: "SoundPickerLib",
|
||||
srcs: [
|
||||
"src/**/*.java",
|
||||
],
|
||||
resource_dirs: [
|
||||
"res",
|
||||
],
|
||||
srcs: [
|
||||
"src/**/*.java",
|
||||
static_libs: [
|
||||
"androidx.appcompat_appcompat",
|
||||
],
|
||||
}
|
||||
|
||||
android_app {
|
||||
name: "SoundPicker",
|
||||
defaults: ["platform_app_defaults"],
|
||||
manifest: "AndroidManifest.xml",
|
||||
static_libs: ["SoundPickerLib"],
|
||||
platform_apis: true,
|
||||
certificate: "media",
|
||||
privileged: true,
|
||||
|
||||
@@ -35,7 +35,6 @@ import android.os.Handler;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.provider.MediaStore;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.util.TypedValue;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -194,16 +193,7 @@ public final class RingtonePickerActivity extends AlertActivity implements
|
||||
mHasDefaultItem = intent.getBooleanExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
|
||||
mUriForDefaultItem = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI);
|
||||
if (mUriForDefaultItem == null) {
|
||||
if (mType == RingtoneManager.TYPE_NOTIFICATION) {
|
||||
mUriForDefaultItem = Settings.System.DEFAULT_NOTIFICATION_URI;
|
||||
} else if (mType == RingtoneManager.TYPE_ALARM) {
|
||||
mUriForDefaultItem = Settings.System.DEFAULT_ALARM_ALERT_URI;
|
||||
} else if (mType == RingtoneManager.TYPE_RINGTONE) {
|
||||
mUriForDefaultItem = Settings.System.DEFAULT_RINGTONE_URI;
|
||||
} else {
|
||||
// or leave it null for silence.
|
||||
mUriForDefaultItem = Settings.System.DEFAULT_RINGTONE_URI;
|
||||
}
|
||||
mUriForDefaultItem = RingtonePickerViewModel.getDefaultItemUriByType(mType);
|
||||
}
|
||||
|
||||
// Get whether to show the 'Silent' item
|
||||
@@ -242,17 +232,9 @@ public final class RingtonePickerActivity extends AlertActivity implements
|
||||
p.mPositiveButtonListener = this;
|
||||
}
|
||||
p.mOnPrepareListViewListener = this;
|
||||
|
||||
p.mTitle = intent.getCharSequenceExtra(RingtoneManager.EXTRA_RINGTONE_TITLE);
|
||||
if (p.mTitle == null) {
|
||||
if (mType == RingtoneManager.TYPE_ALARM) {
|
||||
p.mTitle = getString(com.android.internal.R.string.ringtone_picker_title_alarm);
|
||||
} else if (mType == RingtoneManager.TYPE_NOTIFICATION) {
|
||||
p.mTitle =
|
||||
getString(com.android.internal.R.string.ringtone_picker_title_notification);
|
||||
} else {
|
||||
p.mTitle = getString(com.android.internal.R.string.ringtone_picker_title);
|
||||
}
|
||||
p.mTitle = getString(RingtonePickerViewModel.getTitleByType(mType));
|
||||
}
|
||||
|
||||
setupAlert();
|
||||
@@ -435,13 +417,8 @@ public final class RingtonePickerActivity extends AlertActivity implements
|
||||
}
|
||||
|
||||
private int addDefaultRingtoneItem(ListView listView) {
|
||||
if (mType == RingtoneManager.TYPE_NOTIFICATION) {
|
||||
return addStaticItem(listView, R.string.notification_sound_default);
|
||||
} else if (mType == RingtoneManager.TYPE_ALARM) {
|
||||
return addStaticItem(listView, R.string.alarm_sound_default);
|
||||
}
|
||||
|
||||
return addStaticItem(listView, R.string.ringtone_default);
|
||||
return addStaticItem(listView,
|
||||
RingtonePickerViewModel.getDefaultRingtoneItemTextByType(mType));
|
||||
}
|
||||
|
||||
private int addSilentItem(ListView listView) {
|
||||
@@ -453,13 +430,8 @@ public final class RingtonePickerActivity extends AlertActivity implements
|
||||
false /* attachToRoot */);
|
||||
TextView text = (TextView)view.findViewById(R.id.add_new_sound_text);
|
||||
|
||||
if (mType == RingtoneManager.TYPE_ALARM) {
|
||||
text.setText(R.string.add_alarm_text);
|
||||
} else if (mType == RingtoneManager.TYPE_NOTIFICATION) {
|
||||
text.setText(R.string.add_notification_text);
|
||||
} else {
|
||||
text.setText(R.string.add_ringtone_text);
|
||||
}
|
||||
text.setText(RingtonePickerViewModel.getAddNewItemTextByType(mType));
|
||||
|
||||
listView.addFooterView(view);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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.soundpicker;
|
||||
|
||||
import android.annotation.StringRes;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.provider.Settings;
|
||||
|
||||
/**
|
||||
* View model for {@link RingtonePickerActivity}.
|
||||
*/
|
||||
public final class RingtonePickerViewModel {
|
||||
|
||||
@StringRes
|
||||
static int getTitleByType(int ringtoneType) {
|
||||
switch (ringtoneType) {
|
||||
case RingtoneManager.TYPE_ALARM:
|
||||
return com.android.internal.R.string.ringtone_picker_title_alarm;
|
||||
case RingtoneManager.TYPE_NOTIFICATION:
|
||||
return com.android.internal.R.string.ringtone_picker_title_notification;
|
||||
default:
|
||||
return com.android.internal.R.string.ringtone_picker_title;
|
||||
}
|
||||
}
|
||||
|
||||
static Uri getDefaultItemUriByType(int ringtoneType) {
|
||||
switch (ringtoneType) {
|
||||
case RingtoneManager.TYPE_ALARM:
|
||||
return Settings.System.DEFAULT_ALARM_ALERT_URI;
|
||||
case RingtoneManager.TYPE_NOTIFICATION:
|
||||
return Settings.System.DEFAULT_NOTIFICATION_URI;
|
||||
default:
|
||||
return Settings.System.DEFAULT_RINGTONE_URI;
|
||||
}
|
||||
}
|
||||
|
||||
@StringRes
|
||||
static int getAddNewItemTextByType(int ringtoneType) {
|
||||
switch (ringtoneType) {
|
||||
case RingtoneManager.TYPE_ALARM:
|
||||
return R.string.add_alarm_text;
|
||||
case RingtoneManager.TYPE_NOTIFICATION:
|
||||
return R.string.add_notification_text;
|
||||
default:
|
||||
return R.string.add_ringtone_text;
|
||||
}
|
||||
}
|
||||
|
||||
@StringRes
|
||||
static int getDefaultRingtoneItemTextByType(int ringtoneType) {
|
||||
switch (ringtoneType) {
|
||||
case RingtoneManager.TYPE_ALARM:
|
||||
return R.string.alarm_sound_default;
|
||||
case RingtoneManager.TYPE_NOTIFICATION:
|
||||
return R.string.notification_sound_default;
|
||||
default:
|
||||
return R.string.ringtone_default;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
packages/SoundPicker/tests/Android.bp
Normal file
36
packages/SoundPicker/tests/Android.bp
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2023, 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 {
|
||||
default_applicable_licenses: ["frameworks_base_license"],
|
||||
}
|
||||
|
||||
android_test {
|
||||
name: "SoundPickerTests",
|
||||
certificate: "platform",
|
||||
libs: [
|
||||
"android.test.runner",
|
||||
"android.test.base",
|
||||
],
|
||||
static_libs: [
|
||||
"androidx.test.core",
|
||||
"androidx.test.rules",
|
||||
"androidx.test.ext.junit",
|
||||
"mockito-target-minus-junit4",
|
||||
"SoundPickerLib",
|
||||
],
|
||||
srcs: [
|
||||
"src/**/*.java",
|
||||
],
|
||||
}
|
||||
11
packages/SoundPicker/tests/AndroidManifest.xml
Normal file
11
packages/SoundPicker/tests/AndroidManifest.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.android.soundpicker.tests">
|
||||
|
||||
<application android:debuggable="true">
|
||||
<uses-library android:name="android.test.runner" />
|
||||
</application>
|
||||
<instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
|
||||
android:targetPackage="com.android.soundpicker.tests"
|
||||
android:label="Sound picker tests">
|
||||
</instrumentation>
|
||||
</manifest>
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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.soundpicker;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class RingtonePickerViewModelTest {
|
||||
|
||||
@Test
|
||||
public void testDefaultItemUri_withNotificationIntent_returnDefaultNotificationUri() {
|
||||
Uri uri = RingtonePickerViewModel.getDefaultItemUriByType(
|
||||
RingtoneManager.TYPE_NOTIFICATION);
|
||||
assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, uri);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultItemUri_withAlarmIntent_returnDefaultAlarmUri() {
|
||||
Uri uri = RingtonePickerViewModel.getDefaultItemUriByType(RingtoneManager.TYPE_ALARM);
|
||||
assertEquals(Settings.System.DEFAULT_ALARM_ALERT_URI, uri);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultItemUri_withRingtoneIntent_returnDefaultRingtoneUri() {
|
||||
Uri uri = RingtonePickerViewModel.getDefaultItemUriByType(RingtoneManager.TYPE_RINGTONE);
|
||||
assertEquals(Settings.System.DEFAULT_RINGTONE_URI, uri);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultItemUri_withInvalidRingtoneType_returnDefaultRingtoneUri() {
|
||||
Uri uri = RingtonePickerViewModel.getDefaultItemUriByType(-1);
|
||||
assertEquals(Settings.System.DEFAULT_RINGTONE_URI, uri);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTitle_withNotificationRingtoneType_returnRingtoneNotificationTitle() {
|
||||
int title = RingtonePickerViewModel.getTitleByType(RingtoneManager.TYPE_NOTIFICATION);
|
||||
assertEquals(com.android.internal.R.string.ringtone_picker_title_notification, title);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTitle_withAlarmRingtoneType_returnRingtoneAlarmTitle() {
|
||||
int title = RingtonePickerViewModel.getTitleByType(RingtoneManager.TYPE_ALARM);
|
||||
assertEquals(com.android.internal.R.string.ringtone_picker_title_alarm, title);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTitle_withInvalidRingtoneType_returnDefaultRingtoneTitle() {
|
||||
int title = RingtonePickerViewModel.getTitleByType(-1);
|
||||
assertEquals(com.android.internal.R.string.ringtone_picker_title, title);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddNewItemText_withAlarmType_returnAlarmAddItemText() {
|
||||
int addNewItemTextResId = RingtonePickerViewModel.getAddNewItemTextByType(
|
||||
RingtoneManager.TYPE_ALARM);
|
||||
assertEquals(R.string.add_alarm_text, addNewItemTextResId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddNewItemText_withNotificationType_returnNotificationAddItemText() {
|
||||
int addNewItemTextResId = RingtonePickerViewModel.getAddNewItemTextByType(
|
||||
RingtoneManager.TYPE_NOTIFICATION);
|
||||
assertEquals(R.string.add_notification_text, addNewItemTextResId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddNewItemText_withRingtoneType_returnRingtoneAddItemText() {
|
||||
int addNewItemTextResId = RingtonePickerViewModel.getAddNewItemTextByType(
|
||||
RingtoneManager.TYPE_RINGTONE);
|
||||
assertEquals(R.string.add_ringtone_text, addNewItemTextResId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddNewItemText_withInvalidType_returnRingtoneAddItemText() {
|
||||
int addNewItemTextResId = RingtonePickerViewModel.getAddNewItemTextByType(-1);
|
||||
assertEquals(R.string.add_ringtone_text, addNewItemTextResId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultItemText_withNotificationType_returnNotificationDefaultItemText() {
|
||||
int defaultRingtoneItemText = RingtonePickerViewModel.getDefaultRingtoneItemTextByType(
|
||||
RingtoneManager.TYPE_NOTIFICATION);
|
||||
assertEquals(R.string.notification_sound_default, defaultRingtoneItemText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultItemText_withAlarmType_returnAlarmDefaultItemText() {
|
||||
int defaultRingtoneItemText = RingtonePickerViewModel.getDefaultRingtoneItemTextByType(
|
||||
RingtoneManager.TYPE_NOTIFICATION);
|
||||
assertEquals(R.string.notification_sound_default, defaultRingtoneItemText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultItemText_withRingtoneType_returnRingtoneDefaultItemText() {
|
||||
int defaultRingtoneItemText = RingtonePickerViewModel.getDefaultRingtoneItemTextByType(
|
||||
RingtoneManager.TYPE_RINGTONE);
|
||||
assertEquals(R.string.ringtone_default, defaultRingtoneItemText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultItemText_withInvalidType_returnRingtoneDefaultItemText() {
|
||||
int defaultRingtoneItemText = RingtonePickerViewModel.getDefaultRingtoneItemTextByType(-1);
|
||||
assertEquals(R.string.ringtone_default, defaultRingtoneItemText);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user