MediaRoute2Info: Reorder methods / fields

Changes the order of methods / fields (important ones first)
Also, this CL adds some missing javadoc.

Bug: 147467641
Test: atest mediaroutertest
Change-Id: I80852bf3a7e2624c8ded1c29440a274709c6f393
This commit is contained in:
Hyundo Moon
2020-01-14 20:27:22 +09:00
parent 38ada9b3d5
commit 0d934524d7
4 changed files with 692 additions and 379 deletions

View File

@@ -134,59 +134,204 @@ public final class MediaRoute2Info implements Parcelable {
*/
public static final int DEVICE_TYPE_BLUETOOTH = 3;
@NonNull
final String mId;
@Nullable
final String mProviderId;
@NonNull
final CharSequence mName;
@Nullable
final CharSequence mDescription;
@Nullable
final @ConnectionState int mConnectionState;
@Nullable
final Uri mIconUri;
@Nullable
final String mClientPackageName;
@NonNull
final List<String> mFeatures;
@DeviceType
final int mDeviceType;
final Uri mIconUri;
final CharSequence mDescription;
@ConnectionState
final int mConnectionState;
final String mClientPackageName;
final int mVolume;
final int mVolumeMax;
final int mVolumeHandling;
final @DeviceType int mDeviceType;
@Nullable
final Bundle mExtras;
final String mProviderId;
MediaRoute2Info(@NonNull Builder builder) {
mId = builder.mId;
mProviderId = builder.mProviderId;
mName = builder.mName;
mFeatures = builder.mFeatures;
mDeviceType = builder.mDeviceType;
mIconUri = builder.mIconUri;
mDescription = builder.mDescription;
mConnectionState = builder.mConnectionState;
mIconUri = builder.mIconUri;
mClientPackageName = builder.mClientPackageName;
mFeatures = builder.mFeatures;
mVolume = builder.mVolume;
mVolumeMax = builder.mVolumeMax;
mVolumeHandling = builder.mVolumeHandling;
mDeviceType = builder.mDeviceType;
mVolumeMax = builder.mVolumeMax;
mVolume = builder.mVolume;
mExtras = builder.mExtras;
mProviderId = builder.mProviderId;
}
MediaRoute2Info(@NonNull Parcel in) {
mId = in.readString();
mProviderId = in.readString();
mName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
mFeatures = in.createStringArrayList();
mDeviceType = in.readInt();
mIconUri = in.readParcelable(null);
mDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
mConnectionState = in.readInt();
mIconUri = in.readParcelable(null);
mClientPackageName = in.readString();
mFeatures = in.createStringArrayList();
mVolume = in.readInt();
mVolumeMax = in.readInt();
mVolumeHandling = in.readInt();
mDeviceType = in.readInt();
mVolumeMax = in.readInt();
mVolume = in.readInt();
mExtras = in.readBundle();
mProviderId = in.readString();
}
/**
* Gets the id of the route. The routes which are given by {@link MediaRouter2} will have
* unique IDs.
* <p>
* In order to ensure uniqueness in {@link MediaRouter2} side, the value of this method
* can be different from what was set in {@link MediaRoute2ProviderService}.
*
* @see Builder#Builder(String, CharSequence)
*/
@NonNull
public String getId() {
if (mProviderId != null) {
return toUniqueId(mProviderId, mId);
} else {
return mId;
}
}
/**
* Gets the user-visible name of the route.
*/
@NonNull
public CharSequence getName() {
return mName;
}
/**
* Gets the supported features of the route.
*/
@NonNull
public List<String> getFeatures() {
return mFeatures;
}
/**
* Gets the type of the receiver device associated with this route.
*
* @return The type of the receiver device associated with this route:
* {@link #DEVICE_TYPE_REMOTE_TV}, {@link #DEVICE_TYPE_REMOTE_SPEAKER},
* {@link #DEVICE_TYPE_BLUETOOTH}.
*/
@DeviceType
public int getDeviceType() {
return mDeviceType;
}
/**
* Gets the URI of the icon representing this route.
* <p>
* This icon will be used in picker UIs if available.
*
* @return The URI of the icon representing this route, or null if none.
*/
@Nullable
public Uri getIconUri() {
return mIconUri;
}
/**
* Gets the user-visible description of the route.
*/
@Nullable
public CharSequence getDescription() {
return mDescription;
}
/**
* Gets the connection state of the route.
*
* @return The connection state of this route: {@link #CONNECTION_STATE_DISCONNECTED},
* {@link #CONNECTION_STATE_CONNECTING}, or {@link #CONNECTION_STATE_CONNECTED}.
*/
@ConnectionState
public int getConnectionState() {
return mConnectionState;
}
/**
* Gets the package name of the client that uses the route.
* Returns null if no clients use this route.
* @hide
*/
@Nullable
public String getClientPackageName() {
return mClientPackageName;
}
/**
* Gets information about how volume is handled on the route.
*
* @return {@link #PLAYBACK_VOLUME_FIXED} or {@link #PLAYBACK_VOLUME_VARIABLE}
*/
public int getVolumeHandling() {
return mVolumeHandling;
}
/**
* Gets the maximum volume of the route.
*/
public int getVolumeMax() {
return mVolumeMax;
}
/**
* Gets the current volume of the route. This may be invalid if the route is not selected.
*/
public int getVolume() {
return mVolume;
}
@Nullable
public Bundle getExtras() {
return mExtras == null ? null : new Bundle(mExtras);
}
/**
* Gets the original id set by {@link Builder#Builder(String, CharSequence)}.
* @hide
*/
@NonNull
public String getOriginalId() {
return mId;
}
/**
* Gets the provider id of the route. It is assigned automatically by
* {@link com.android.server.media.MediaRouterService}.
*
* @return provider id of the route or null if it's not set.
* @hide
*/
@Nullable
public String getProviderId() {
return mProviderId;
}
/**
* Returns if the route has at least one of the specified route features.
*
* @param features the list of route features to consider
* @return true if the route has at least one feature in the list
*/
public boolean hasAnyFeatures(@NonNull Collection<String> features) {
Objects.requireNonNull(features, "features must not be null");
for (String feature : features) {
if (getFeatures().contains(feature)) {
return true;
}
}
return false;
}
/**
@@ -213,172 +358,49 @@ public final class MediaRoute2Info implements Parcelable {
return false;
}
MediaRoute2Info other = (MediaRoute2Info) obj;
// Note: mExtras is not included.
return Objects.equals(mId, other.mId)
&& Objects.equals(mProviderId, other.mProviderId)
&& Objects.equals(mName, other.mName)
&& Objects.equals(mFeatures, other.mFeatures)
&& (mDeviceType == other.mDeviceType)
&& Objects.equals(mIconUri, other.mIconUri)
&& Objects.equals(mDescription, other.mDescription)
&& (mConnectionState == other.mConnectionState)
&& Objects.equals(mIconUri, other.mIconUri)
&& Objects.equals(mClientPackageName, other.mClientPackageName)
&& Objects.equals(mFeatures, other.mFeatures)
&& (mVolume == other.mVolume)
&& (mVolumeMax == other.mVolumeMax)
&& (mVolumeHandling == other.mVolumeHandling)
&& (mDeviceType == other.mDeviceType)
//TODO: This will be evaluated as false in most cases. Try not to.
&& Objects.equals(mExtras, other.mExtras);
&& (mVolumeMax == other.mVolumeMax)
&& (mVolume == other.mVolume)
&& Objects.equals(mProviderId, other.mProviderId);
}
@Override
public int hashCode() {
return Objects.hash(mId, mName, mDescription, mConnectionState, mIconUri,
mFeatures, mVolume, mVolumeMax, mVolumeHandling, mDeviceType);
// Note: mExtras is not included.
return Objects.hash(mId, mName, mFeatures, mDeviceType, mIconUri, mDescription,
mConnectionState, mClientPackageName, mVolumeHandling, mVolumeMax, mVolume,
mProviderId);
}
/**
* Gets the id of the route. The routes which are given by {@link MediaRouter2} will have
* unique IDs.
* <p>
* In order to ensure uniqueness in {@link MediaRouter2} side, the value of this method
* can be different from what was set in {@link MediaRoute2ProviderService}.
*
* @see Builder#Builder(String, CharSequence)
*/
@NonNull
public String getId() {
if (mProviderId != null) {
return toUniqueId(mProviderId, mId);
} else {
return mId;
}
}
/**
* Gets the original id set by {@link Builder#Builder(String, CharSequence)}.
* @hide
*/
@NonNull
public String getOriginalId() {
return mId;
}
/**
* Gets the provider id of the route. It is assigned automatically by
* {@link com.android.server.media.MediaRouterService}.
*
* @return provider id of the route or null if it's not set.
* @hide
*/
@Nullable
public String getProviderId() {
return mProviderId;
}
@NonNull
public CharSequence getName() {
return mName;
}
@Nullable
public CharSequence getDescription() {
return mDescription;
}
/**
* Gets the connection state of the route.
*
* @return The connection state of this route: {@link #CONNECTION_STATE_DISCONNECTED},
* {@link #CONNECTION_STATE_CONNECTING}, or {@link #CONNECTION_STATE_CONNECTED}.
*/
@ConnectionState
public int getConnectionState() {
return mConnectionState;
}
/**
* Gets the URI of the icon representing this route.
* <p>
* This icon will be used in picker UIs if available.
*
* @return The URI of the icon representing this route, or null if none.
*/
@Nullable
public Uri getIconUri() {
return mIconUri;
}
/**
* Gets the package name of the client that uses the route.
* Returns null if no clients use this.
* @hide
*/
@Nullable
public String getClientPackageName() {
return mClientPackageName;
}
/**
* Gets the supported categories of the route.
*/
@NonNull
public List<String> getFeatures() {
return mFeatures;
}
/**
* Gets the type of the receiver device associated with this route.
*
* @return The type of the receiver device associated with this route:
* {@link #DEVICE_TYPE_REMOTE_TV}, {@link #DEVICE_TYPE_REMOTE_SPEAKER},
* {@link #DEVICE_TYPE_BLUETOOTH}.
*/
@DeviceType
public int getDeviceType() {
return mDeviceType;
}
/**
* Gets the current volume of the route. This may be invalid if the route is not selected.
*/
public int getVolume() {
return mVolume;
}
/**
* Gets the maximum volume of the route.
*/
public int getVolumeMax() {
return mVolumeMax;
}
/**
* Gets information about how volume is handled on the route.
*
* @return {@link #PLAYBACK_VOLUME_FIXED} or {@link #PLAYBACK_VOLUME_VARIABLE}
*/
public int getVolumeHandling() {
return mVolumeHandling;
}
@Nullable
public Bundle getExtras() {
return mExtras;
}
/**
* Returns if the route has at least one of the specified route features.
*
* @param features the list of route features to consider
* @return true if the route has at least one feature in the list
*/
public boolean hasAnyFeatures(@NonNull Collection<String> features) {
Objects.requireNonNull(features, "features must not be null");
for (String feature : features) {
if (getFeatures().contains(feature)) {
return true;
}
}
return false;
@Override
public String toString() {
// Note: mExtras is not printed here.
StringBuilder result = new StringBuilder()
.append("MediaRoute2Info{ ")
.append("id=").append(getId())
.append(", name=").append(getName())
.append(", features=").append(getFeatures())
.append(", deviceType=").append(getDeviceType())
.append(", iconUri=").append(getIconUri())
.append(", description=").append(getDescription())
.append(", connectionState=").append(getConnectionState())
.append(", clientPackageName=").append(getClientPackageName())
.append(", volumeHandling=").append(getVolumeHandling())
.append(", volumeMax=").append(getVolumeMax())
.append(", volume=").append(getVolume())
.append(", providerId=").append(getProviderId())
.append(" }");
return result.toString();
}
@Override
@@ -389,36 +411,18 @@ public final class MediaRoute2Info implements Parcelable {
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeString(mId);
dest.writeString(mProviderId);
TextUtils.writeToParcel(mName, dest, flags);
dest.writeStringList(mFeatures);
dest.writeInt(mDeviceType);
dest.writeParcelable(mIconUri, flags);
TextUtils.writeToParcel(mDescription, dest, flags);
dest.writeInt(mConnectionState);
dest.writeParcelable(mIconUri, flags);
dest.writeString(mClientPackageName);
dest.writeStringList(mFeatures);
dest.writeInt(mVolume);
dest.writeInt(mVolumeMax);
dest.writeInt(mVolumeHandling);
dest.writeInt(mDeviceType);
dest.writeInt(mVolumeMax);
dest.writeInt(mVolume);
dest.writeBundle(mExtras);
}
@Override
public String toString() {
StringBuilder result = new StringBuilder()
.append("MediaRouteInfo{ ")
.append("id=").append(getId())
.append(", name=").append(getName())
.append(", description=").append(getDescription())
.append(", connectionState=").append(getConnectionState())
.append(", iconUri=").append(getIconUri())
.append(", volume=").append(getVolume())
.append(", volumeMax=").append(getVolumeMax())
.append(", volumeHandling=").append(getVolumeHandling())
.append(", deviceType=").append(getDeviceType())
.append(", providerId=").append(getProviderId())
.append(" }");
return result.toString();
dest.writeString(mProviderId);
}
/**
@@ -426,20 +430,21 @@ public final class MediaRoute2Info implements Parcelable {
*/
public static final class Builder {
final String mId;
String mProviderId;
final CharSequence mName;
final List<String> mFeatures;
@DeviceType
int mDeviceType = DEVICE_TYPE_UNKNOWN;
Uri mIconUri;
CharSequence mDescription;
@ConnectionState
int mConnectionState;
Uri mIconUri;
String mClientPackageName;
List<String> mFeatures;
int mVolume;
int mVolumeMax;
int mVolumeHandling = PLAYBACK_VOLUME_FIXED;
@DeviceType
int mDeviceType = DEVICE_TYPE_UNKNOWN;
int mVolumeMax;
int mVolume;
Bundle mExtras;
String mProviderId;
/**
* Constructor for builder to create {@link MediaRoute2Info}.
@@ -448,8 +453,8 @@ public final class MediaRoute2Info implements Parcelable {
* obtained from {@link MediaRouter2} can be different from what was set in
* {@link MediaRoute2ProviderService}.
* </p>
* @param id
* @param name
* @param id The ID of the route. Must not be empty.
* @param name The user-visible name of the route.
*/
public Builder(@NonNull String id, @NonNull CharSequence name) {
if (TextUtils.isEmpty(id)) {
@@ -463,40 +468,91 @@ public final class MediaRoute2Info implements Parcelable {
mFeatures = new ArrayList<>();
}
/**
* Constructor for builder to create {@link MediaRoute2Info} with
* existing {@link MediaRoute2Info} instance.
*
* @param routeInfo the existing instance to copy data from.
*/
public Builder(@NonNull MediaRoute2Info routeInfo) {
if (routeInfo == null) {
throw new IllegalArgumentException("route info must not be null");
}
Objects.requireNonNull(routeInfo, "routeInfo must not be null");
mId = routeInfo.mId;
mName = routeInfo.mName;
if (!TextUtils.isEmpty(routeInfo.mProviderId)) {
setProviderId(routeInfo.mProviderId);
}
mFeatures = new ArrayList<>(routeInfo.mFeatures);
mDeviceType = routeInfo.mDeviceType;
mIconUri = routeInfo.mIconUri;
mDescription = routeInfo.mDescription;
mConnectionState = routeInfo.mConnectionState;
mIconUri = routeInfo.mIconUri;
setClientPackageName(routeInfo.mClientPackageName);
mFeatures = new ArrayList<>(routeInfo.mFeatures);
setVolume(routeInfo.mVolume);
setVolumeMax(routeInfo.mVolumeMax);
setVolumeHandling(routeInfo.mVolumeHandling);
setDeviceType(routeInfo.mDeviceType);
mClientPackageName = routeInfo.mClientPackageName;
mVolumeHandling = routeInfo.mVolumeHandling;
mVolumeMax = routeInfo.mVolumeMax;
mVolume = routeInfo.mVolume;
if (routeInfo.mExtras != null) {
mExtras = new Bundle(routeInfo.mExtras);
}
mProviderId = routeInfo.mProviderId;
}
/**
* Sets the provider id of the route.
* @hide
* Adds a feature for the route.
*/
@NonNull
public Builder setProviderId(@NonNull String providerId) {
if (TextUtils.isEmpty(providerId)) {
throw new IllegalArgumentException("providerId must not be null or empty");
public Builder addFeature(@NonNull String feature) {
if (TextUtils.isEmpty(feature)) {
throw new IllegalArgumentException("feature must not be null or empty");
}
mProviderId = providerId;
mFeatures.add(feature);
return this;
}
/**
* Adds features for the route. A route must support at least one route type.
*/
@NonNull
public Builder addFeatures(@NonNull Collection<String> features) {
Objects.requireNonNull(features, "features must not be null");
for (String feature : features) {
addFeature(feature);
}
return this;
}
/**
* Clears the features of the route. A route must support at least one route type.
*/
@NonNull
public Builder clearFeatures() {
mFeatures.clear();
return this;
}
/**
* Sets the route's device type.
*/
@NonNull
public Builder setDeviceType(@DeviceType int deviceType) {
mDeviceType = deviceType;
return this;
}
/**
* Sets the URI of the icon representing this route.
* <p>
* This icon will be used in picker UIs if available.
* </p><p>
* The URI must be one of the following formats:
* <ul>
* <li>content ({@link android.content.ContentResolver#SCHEME_CONTENT})</li>
* <li>android.resource ({@link android.content.ContentResolver#SCHEME_ANDROID_RESOURCE})
* </li>
* <li>file ({@link android.content.ContentResolver#SCHEME_FILE})</li>
* </ul>
* </p>
*/
@NonNull
public Builder setIconUri(@Nullable Uri iconUri) {
mIconUri = iconUri;
return this;
}
@@ -522,26 +578,6 @@ public final class MediaRoute2Info implements Parcelable {
return this;
}
/**
* Sets the URI of the icon representing this route.
* <p>
* This icon will be used in picker UIs if available.
* </p><p>
* The URI must be one of the following formats:
* <ul>
* <li>content ({@link android.content.ContentResolver#SCHEME_CONTENT})</li>
* <li>android.resource ({@link android.content.ContentResolver#SCHEME_ANDROID_RESOURCE})
* </li>
* <li>file ({@link android.content.ContentResolver#SCHEME_FILE})</li>
* </ul>
* </p>
*/
@NonNull
public Builder setIconUri(@Nullable Uri iconUri) {
mIconUri = iconUri;
return this;
}
/**
* Sets the package name of the app using the route.
*/
@@ -551,57 +587,6 @@ public final class MediaRoute2Info implements Parcelable {
return this;
}
/**
* Clears the features of the route.
*/
@NonNull
public Builder clearFeatures() {
mFeatures = new ArrayList<>();
return this;
}
/**
* Adds features for the route.
*/
@NonNull
public Builder addFeatures(@NonNull Collection<String> features) {
Objects.requireNonNull(features, "features must not be null");
for (String feature : features) {
addFeature(feature);
}
return this;
}
/**
* Adds a feature for the route.
*/
@NonNull
public Builder addFeature(@NonNull String feature) {
if (TextUtils.isEmpty(feature)) {
throw new IllegalArgumentException("feature must not be null or empty");
}
mFeatures.add(feature);
return this;
}
/**
* Sets the route's current volume, or 0 if unknown.
*/
@NonNull
public Builder setVolume(int volume) {
mVolume = volume;
return this;
}
/**
* Sets the route's maximum volume, or 0 if unknown.
*/
@NonNull
public Builder setVolumeMax(int volumeMax) {
mVolumeMax = volumeMax;
return this;
}
/**
* Sets the route's volume handling.
*/
@@ -612,28 +597,61 @@ public final class MediaRoute2Info implements Parcelable {
}
/**
* Sets the route's device type.
* Sets the route's maximum volume, or 0 if unknown.
*/
@NonNull
public Builder setDeviceType(@DeviceType int deviceType) {
mDeviceType = deviceType;
public Builder setVolumeMax(int volumeMax) {
mVolumeMax = volumeMax;
return this;
}
/**
* Sets the route's current volume, or 0 if unknown.
*/
@NonNull
public Builder setVolume(int volume) {
mVolume = volume;
return this;
}
/**
* Sets a bundle of extras for the route.
* <p>
* Note: The extras will not affect the result of {@link MediaRoute2Info#equals(Object)}.
*/
@NonNull
public Builder setExtras(@Nullable Bundle extras) {
if (extras == null) {
mExtras = null;
return this;
}
mExtras = new Bundle(extras);
return this;
}
/**
* Sets the provider id of the route.
* @hide
*/
@NonNull
public Builder setProviderId(@NonNull String providerId) {
if (TextUtils.isEmpty(providerId)) {
throw new IllegalArgumentException("providerId must not be null or empty");
}
mProviderId = providerId;
return this;
}
/**
* Builds the {@link MediaRoute2Info media route info}.
*
* @throws IllegalArgumentException if no features are added.
*/
@NonNull
public MediaRoute2Info build() {
if (mFeatures.isEmpty()) {
throw new IllegalArgumentException("features must not be empty!");
}
return new MediaRoute2Info(this);
}
}

View File

@@ -0,0 +1,363 @@
/*
* Copyright 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.mediaroutertest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.testng.Assert.assertThrows;
import android.media.MediaRoute2Info;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.List;
/**
* Tests {@link MediaRoute2Info} and its {@link MediaRoute2Info.Builder builder}.
*/
@RunWith(AndroidJUnit4.class)
@SmallTest
public class MediaRoute2InfoTest {
public static final String TEST_ID = "test_id";
public static final String TEST_NAME = "test_name";
public static final String TEST_ROUTE_TYPE_0 = "test_route_type_0";
public static final String TEST_ROUTE_TYPE_1 = "test_route_type_1";
public static final int TEST_DEVICE_TYPE = MediaRoute2Info.DEVICE_TYPE_REMOTE_SPEAKER;
public static final Uri TEST_ICON_URI = Uri.parse("https://developer.android.com");
public static final String TEST_DESCRIPTION = "test_description";
public static final int TEST_CONNECTION_STATE = MediaRoute2Info.CONNECTION_STATE_CONNECTING;
public static final String TEST_CLIENT_PACKAGE_NAME = "com.test.client.package.name";
public static final int TEST_VOLUME_HANDLING = MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE;
public static final int TEST_VOLUME_MAX = 100;
public static final int TEST_VOLUME = 65;
public static final String TEST_KEY = "test_key";
public static final String TEST_VALUE = "test_value";
@Test
public void testBuilderConstructorWithInvalidValues() {
final String nullId = null;
final String nullName = null;
final String emptyId = "";
final String emptyName = "";
final String validId = "valid_id";
final String validName = "valid_name";
// ID is invalid
assertThrows(IllegalArgumentException.class,
() -> new MediaRoute2Info.Builder(nullId, validName));
assertThrows(IllegalArgumentException.class,
() -> new MediaRoute2Info.Builder(emptyId, validName));
// name is invalid
assertThrows(IllegalArgumentException.class,
() -> new MediaRoute2Info.Builder(validId, nullName));
assertThrows(IllegalArgumentException.class,
() -> new MediaRoute2Info.Builder(validId, emptyName));
// Both are invalid
assertThrows(IllegalArgumentException.class,
() -> new MediaRoute2Info.Builder(nullId, nullName));
assertThrows(IllegalArgumentException.class,
() -> new MediaRoute2Info.Builder(nullId, emptyName));
assertThrows(IllegalArgumentException.class,
() -> new MediaRoute2Info.Builder(emptyId, nullName));
assertThrows(IllegalArgumentException.class,
() -> new MediaRoute2Info.Builder(emptyId, emptyName));
// Null RouteInfo (1-argument constructor)
final MediaRoute2Info nullRouteInfo = null;
assertThrows(NullPointerException.class,
() -> new MediaRoute2Info.Builder(nullRouteInfo));
}
@Test
public void testBuilderBuildWithEmptyRouteTypesShouldThrowIAE() {
MediaRoute2Info.Builder builder = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME);
assertThrows(IllegalArgumentException.class, () -> builder.build());
}
@Test
public void testBuilderAndGettersOfMediaRoute2Info() {
Bundle extras = new Bundle();
extras.putString(TEST_KEY, TEST_VALUE);
MediaRoute2Info routeInfo = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.addFeature(TEST_ROUTE_TYPE_1)
.setDeviceType(TEST_DEVICE_TYPE)
.setIconUri(TEST_ICON_URI)
.setDescription(TEST_DESCRIPTION)
.setConnectionState(TEST_CONNECTION_STATE)
.setClientPackageName(TEST_CLIENT_PACKAGE_NAME)
.setVolumeHandling(TEST_VOLUME_HANDLING)
.setVolumeMax(TEST_VOLUME_MAX)
.setVolume(TEST_VOLUME)
.setExtras(extras)
.build();
assertEquals(TEST_ID, routeInfo.getId());
assertEquals(TEST_NAME, routeInfo.getName());
assertEquals(2, routeInfo.getFeatures().size());
assertEquals(TEST_ROUTE_TYPE_0, routeInfo.getFeatures().get(0));
assertEquals(TEST_ROUTE_TYPE_1, routeInfo.getFeatures().get(1));
assertEquals(TEST_DEVICE_TYPE, routeInfo.getDeviceType());
assertEquals(TEST_ICON_URI, routeInfo.getIconUri());
assertEquals(TEST_DESCRIPTION, routeInfo.getDescription());
assertEquals(TEST_CONNECTION_STATE, routeInfo.getConnectionState());
assertEquals(TEST_CLIENT_PACKAGE_NAME, routeInfo.getClientPackageName());
assertEquals(TEST_VOLUME_HANDLING, routeInfo.getVolumeHandling());
assertEquals(TEST_VOLUME_MAX, routeInfo.getVolumeMax());
assertEquals(TEST_VOLUME, routeInfo.getVolume());
Bundle extrasOut = routeInfo.getExtras();
assertNotNull(extrasOut);
assertTrue(extrasOut.containsKey(TEST_KEY));
assertEquals(TEST_VALUE, extrasOut.getString(TEST_KEY));
}
@Test
public void testBuilderSetExtrasWithNull() {
MediaRoute2Info routeInfo = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.setExtras(null)
.build();
assertNull(routeInfo.getExtras());
}
@Test
public void testBuilderaddFeatures() {
List<String> routeTypes = new ArrayList<>();
routeTypes.add(TEST_ROUTE_TYPE_0);
routeTypes.add(TEST_ROUTE_TYPE_1);
MediaRoute2Info routeInfo = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeatures(routeTypes)
.build();
assertEquals(routeTypes, routeInfo.getFeatures());
}
@Test
public void testBuilderclearFeatures() {
MediaRoute2Info routeInfo = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.addFeature(TEST_ROUTE_TYPE_1)
// clearFeatures should clear the route types.
.clearFeatures()
.addFeature(TEST_ROUTE_TYPE_1)
.build();
assertEquals(1, routeInfo.getFeatures().size());
assertEquals(TEST_ROUTE_TYPE_1, routeInfo.getFeatures().get(0));
}
@Test
public void testhasAnyFeaturesReturnsFalse() {
MediaRoute2Info routeInfo = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.addFeature(TEST_ROUTE_TYPE_1)
.build();
List<String> testRouteTypes = new ArrayList<>();
testRouteTypes.add("non_matching_route_type_1");
testRouteTypes.add("non_matching_route_type_2");
testRouteTypes.add("non_matching_route_type_3");
testRouteTypes.add("non_matching_route_type_4");
assertFalse(routeInfo.hasAnyFeatures(testRouteTypes));
}
@Test
public void testhasAnyFeaturesReturnsTrue() {
MediaRoute2Info routeInfo = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.addFeature(TEST_ROUTE_TYPE_1)
.build();
List<String> testRouteTypes = new ArrayList<>();
testRouteTypes.add("non_matching_route_type_1");
testRouteTypes.add("non_matching_route_type_2");
testRouteTypes.add("non_matching_route_type_3");
testRouteTypes.add(TEST_ROUTE_TYPE_1);
assertTrue(routeInfo.hasAnyFeatures(testRouteTypes));
}
@Test
public void testEqualsCreatedWithSameArguments() {
Bundle extras = new Bundle();
extras.putString(TEST_KEY, TEST_VALUE);
MediaRoute2Info routeInfo1 = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.addFeature(TEST_ROUTE_TYPE_1)
.setDeviceType(TEST_DEVICE_TYPE)
.setIconUri(TEST_ICON_URI)
.setDescription(TEST_DESCRIPTION)
.setConnectionState(TEST_CONNECTION_STATE)
.setClientPackageName(TEST_CLIENT_PACKAGE_NAME)
.setVolumeHandling(TEST_VOLUME_HANDLING)
.setVolumeMax(TEST_VOLUME_MAX)
.setVolume(TEST_VOLUME)
.setExtras(extras)
.build();
MediaRoute2Info routeInfo2 = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.addFeature(TEST_ROUTE_TYPE_1)
.setDeviceType(TEST_DEVICE_TYPE)
.setIconUri(TEST_ICON_URI)
.setDescription(TEST_DESCRIPTION)
.setConnectionState(TEST_CONNECTION_STATE)
.setClientPackageName(TEST_CLIENT_PACKAGE_NAME)
.setVolumeHandling(TEST_VOLUME_HANDLING)
.setVolumeMax(TEST_VOLUME_MAX)
.setVolume(TEST_VOLUME)
.setExtras(extras)
.build();
assertEquals(routeInfo1, routeInfo2);
assertEquals(routeInfo1.hashCode(), routeInfo2.hashCode());
}
@Test
public void testEqualsCreatedWithBuilderCopyConstructor() {
Bundle extras = new Bundle();
extras.putString(TEST_KEY, TEST_VALUE);
MediaRoute2Info routeInfo1 = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.addFeature(TEST_ROUTE_TYPE_1)
.setDeviceType(TEST_DEVICE_TYPE)
.setIconUri(TEST_ICON_URI)
.setDescription(TEST_DESCRIPTION)
.setConnectionState(TEST_CONNECTION_STATE)
.setClientPackageName(TEST_CLIENT_PACKAGE_NAME)
.setVolumeHandling(TEST_VOLUME_HANDLING)
.setVolumeMax(TEST_VOLUME_MAX)
.setVolume(TEST_VOLUME)
.setExtras(extras)
.build();
MediaRoute2Info routeInfo2 = new MediaRoute2Info.Builder(routeInfo1).build();
assertEquals(routeInfo1, routeInfo2);
assertEquals(routeInfo1.hashCode(), routeInfo2.hashCode());
}
@Test
public void testEqualsReturnFalse() {
Bundle extras = new Bundle();
extras.putString(TEST_KEY, TEST_VALUE);
MediaRoute2Info routeInfo = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.addFeature(TEST_ROUTE_TYPE_1)
.setDeviceType(TEST_DEVICE_TYPE)
.setIconUri(TEST_ICON_URI)
.setDescription(TEST_DESCRIPTION)
.setConnectionState(TEST_CONNECTION_STATE)
.setClientPackageName(TEST_CLIENT_PACKAGE_NAME)
.setVolumeHandling(TEST_VOLUME_HANDLING)
.setVolumeMax(TEST_VOLUME_MAX)
.setVolume(TEST_VOLUME)
.setExtras(extras)
.build();
// Now, we will use copy constructor
assertNotEquals(routeInfo, new MediaRoute2Info.Builder(routeInfo)
.addFeature("randomRouteType")
.build());
assertNotEquals(routeInfo, new MediaRoute2Info.Builder(routeInfo)
.setDeviceType(TEST_DEVICE_TYPE + 1)
.build());
assertNotEquals(routeInfo, new MediaRoute2Info.Builder(routeInfo)
.setIconUri(Uri.parse("randomUri"))
.build());
assertNotEquals(routeInfo, new MediaRoute2Info.Builder(routeInfo)
.setDescription("randomDescription")
.build());
assertNotEquals(routeInfo, new MediaRoute2Info.Builder(routeInfo)
.setConnectionState(TEST_CONNECTION_STATE + 1)
.build());
assertNotEquals(routeInfo, new MediaRoute2Info.Builder(routeInfo)
.setClientPackageName("randomPackageName")
.build());
assertNotEquals(routeInfo, new MediaRoute2Info.Builder(routeInfo)
.setVolumeHandling(TEST_VOLUME_HANDLING + 1)
.build());
assertNotEquals(routeInfo, new MediaRoute2Info.Builder(routeInfo)
.setVolumeMax(TEST_VOLUME_MAX + 100)
.build());
assertNotEquals(routeInfo, new MediaRoute2Info.Builder(routeInfo)
.setVolume(TEST_VOLUME + 10)
.build());
// Note: Extras will not affect the equals.
}
@Test
public void testParcelingAndUnParceling() {
Bundle extras = new Bundle();
extras.putString(TEST_KEY, TEST_VALUE);
MediaRoute2Info routeInfo = new MediaRoute2Info.Builder(TEST_ID, TEST_NAME)
.addFeature(TEST_ROUTE_TYPE_0)
.addFeature(TEST_ROUTE_TYPE_1)
.setDeviceType(TEST_DEVICE_TYPE)
.setIconUri(TEST_ICON_URI)
.setDescription(TEST_DESCRIPTION)
.setConnectionState(TEST_CONNECTION_STATE)
.setClientPackageName(TEST_CLIENT_PACKAGE_NAME)
.setVolumeHandling(TEST_VOLUME_HANDLING)
.setVolumeMax(TEST_VOLUME_MAX)
.setVolume(TEST_VOLUME)
.setExtras(extras)
.build();
Parcel parcel = Parcel.obtain();
routeInfo.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
MediaRoute2Info routeInfoFromParcel = MediaRoute2Info.CREATOR.createFromParcel(parcel);
assertEquals(routeInfo, routeInfoFromParcel);
assertEquals(routeInfo.hashCode(), routeInfoFromParcel.hashCode());
// Check extras
Bundle extrasOut = routeInfoFromParcel.getExtras();
assertNotNull(extrasOut);
assertTrue(extrasOut.containsKey(TEST_KEY));
assertEquals(TEST_VALUE, extrasOut.getString(TEST_KEY));
}
}

View File

@@ -16,17 +16,13 @@
package com.android.mediaroutertest;
import static android.media.MediaRoute2Info.CONNECTION_STATE_CONNECTED;
import static android.media.MediaRoute2Info.CONNECTION_STATE_CONNECTING;
import static android.media.MediaRoute2Info.DEVICE_TYPE_REMOTE_SPEAKER;
import static android.media.MediaRoute2Info.DEVICE_TYPE_REMOTE_TV;
import static android.media.MediaRoute2Info.PLAYBACK_VOLUME_FIXED;
import static android.media.MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE;
import static com.android.mediaroutertest.MediaRouterManagerTest.FEATURES_ALL;
import static com.android.mediaroutertest.MediaRouterManagerTest.FEATURES_SPECIAL;
import static com.android.mediaroutertest.MediaRouterManagerTest.FEATURE_SAMPLE;
import static com.android.mediaroutertest.MediaRouterManagerTest.FEATURE_SPECIAL;
import static com.android.mediaroutertest.MediaRouterManagerTest.ROUTE_ID1;
import static com.android.mediaroutertest.MediaRouterManagerTest.ROUTE_ID2;
import static com.android.mediaroutertest.MediaRouterManagerTest.ROUTE_ID3_SESSION_CREATION_FAILED;
@@ -131,57 +127,6 @@ public class MediaRouter2Test {
assertEquals(routeInfo, routeInfoFromParcel);
}
@Test
public void testRouteInfoInequality() {
MediaRoute2Info route = new MediaRoute2Info.Builder("id", "name")
.setDescription("description")
.setClientPackageName("com.android.mediaroutertest")
.setConnectionState(CONNECTION_STATE_CONNECTING)
.setIconUri(new Uri.Builder().path("icon").build())
.addFeature(FEATURE_SAMPLE)
.setVolume(5)
.setVolumeMax(20)
.setVolumeHandling(PLAYBACK_VOLUME_VARIABLE)
.setDeviceType(DEVICE_TYPE_REMOTE_SPEAKER)
.build();
MediaRoute2Info routeDescription = new MediaRoute2Info.Builder(route)
.setDescription("another description").build();
assertNotEquals(route, routeDescription);
MediaRoute2Info routeConnectionState = new MediaRoute2Info.Builder(route)
.setConnectionState(CONNECTION_STATE_CONNECTED).build();
assertNotEquals(route, routeConnectionState);
MediaRoute2Info routeIcon = new MediaRoute2Info.Builder(route)
.setIconUri(new Uri.Builder().path("new icon").build()).build();
assertNotEquals(route, routeIcon);
MediaRoute2Info routeClient = new MediaRoute2Info.Builder(route)
.setClientPackageName("another.client.package").build();
assertNotEquals(route, routeClient);
MediaRoute2Info routeType = new MediaRoute2Info.Builder(route)
.addFeature(FEATURE_SPECIAL).build();
assertNotEquals(route, routeType);
MediaRoute2Info routeVolume = new MediaRoute2Info.Builder(route)
.setVolume(10).build();
assertNotEquals(route, routeVolume);
MediaRoute2Info routeVolumeMax = new MediaRoute2Info.Builder(route)
.setVolumeMax(30).build();
assertNotEquals(route, routeVolumeMax);
MediaRoute2Info routeVolumeHandling = new MediaRoute2Info.Builder(route)
.setVolumeHandling(PLAYBACK_VOLUME_FIXED).build();
assertNotEquals(route, routeVolumeHandling);
MediaRoute2Info routeDeviceType = new MediaRoute2Info.Builder(route)
.setVolume(DEVICE_TYPE_REMOTE_TV).build();
assertNotEquals(route, routeDeviceType);
}
@Test
public void testControlVolumeWithRouter() throws Exception {
Map<String, MediaRoute2Info> routes = waitAndGetRoutes(FEATURES_ALL);
@@ -228,8 +173,10 @@ public class MediaRouter2Test {
@Test
public void testRequestCreateSessionWithInvalidArguments() {
MediaRoute2Info route = new MediaRoute2Info.Builder("id", "name").build();
String routeFeature = "routeFeature";
MediaRoute2Info route = new MediaRoute2Info.Builder("id", "name")
.addFeature(routeFeature)
.build();
// Tests null route
assertThrows(NullPointerException.class,

View File

@@ -20,7 +20,6 @@ import static android.media.MediaRoute2Info.PLAYBACK_VOLUME_FIXED;
import static android.media.MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -143,20 +142,6 @@ public class MediaRouterManagerTest {
clearCallbacks();
}
//TODO: Move to a separate file
@Test
public void testMediaRoute2Info() {
MediaRoute2Info routeInfo1 = new MediaRoute2Info.Builder("id", "name")
.build();
MediaRoute2Info routeInfo2 = new MediaRoute2Info.Builder(routeInfo1).build();
MediaRoute2Info routeInfo3 = new MediaRoute2Info.Builder(routeInfo1)
.setClientPackageName(mPackageName).build();
assertEquals(routeInfo1, routeInfo2);
assertNotEquals(routeInfo1, routeInfo3);
}
/**
* Tests if routes are added correctly when a new callback is registered.
*/