Fix unit test for TVRemoteProviderWatcherTest.java.

Previous submit used the wrong unit test name as a check and apparently
presubmits did not catch the bug. TVRemoteProvider package loading
is done in the constructor and the test needs to re-create the teste
object whenever changing package configuration.

Bug: 150766750
Test: atest services/tests/servicestests/src/com/android/server/tv/TvRemoteProviderWatcherTest.java
Change-Id: Ide38af5490b7d88edbb893f7a1a93c7b47beca85
This commit is contained in:
Andrei Litvin
2020-04-16 15:57:36 -04:00
parent 2b80990bbc
commit 9f71403e1d
2 changed files with 21 additions and 11 deletions

View File

@@ -59,9 +59,9 @@ final class TvRemoteProviderWatcher {
private boolean mRunning;
TvRemoteProviderWatcher(Context context, Object lock) {
TvRemoteProviderWatcher(Context context, Object lock, Handler handler) {
mContext = context;
mHandler = new Handler(true);
mHandler = handler;
mUserId = UserHandle.myUserId();
mPackageManager = context.getPackageManager();
mLock = lock;
@@ -79,6 +79,10 @@ final class TvRemoteProviderWatcher {
});
}
TvRemoteProviderWatcher(Context context, Object lock) {
this(context, lock, new Handler(true));
}
public void start() {
if (DEBUG) Slog.d(TAG, "start()");
if (!mRunning) {

View File

@@ -29,6 +29,7 @@ import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;
import androidx.test.filters.SmallTest;
@@ -88,7 +89,12 @@ public class TvRemoteProviderWatcherTest {
// Test intentionally includes empty spacing for a more complex test
when(mMockResources.getString(com.android.internal.R.string.config_tvRemoteServicePackage))
.thenReturn(",,foo, " + TV_REMOTE_SERVICE_PACKAGE_NAME + ",bar, baz,,");
assertTrue(mTvRemoteProviderWatcher.verifyServiceTrusted(createTvServiceInfo()));
// Re-create the object since package name is loaded in the constructor
TvRemoteProviderWatcher watcher =
new TvRemoteProviderWatcher(
mMockContext, new Object(), new Handler(Looper.getMainLooper()));
assertTrue(watcher.verifyServiceTrusted(createTvServiceInfo()));
}
@Test
@@ -96,7 +102,12 @@ public class TvRemoteProviderWatcherTest {
// Checks include empty strings to validate that processing as well
when(mMockResources.getString(com.android.internal.R.string.config_tvRemoteServicePackage))
.thenReturn(",,foo,, ,bar, baz,,");
assertFalse(mTvRemoteProviderWatcher.verifyServiceTrusted(createTvServiceInfo()));
// Re-create the object since package name is loaded in the constructor
TvRemoteProviderWatcher watcher =
new TvRemoteProviderWatcher(
mMockContext, new Object(), new Handler(Looper.getMainLooper()));
assertFalse(watcher.verifyServiceTrusted(createTvServiceInfo()));
}
@Test
@@ -139,14 +150,9 @@ public class TvRemoteProviderWatcherTest {
public void whitelistingPackageNameIsRequired() {
reset(mMockResources);
when(mMockResources.getString(anyInt())).thenReturn("");
// Create a new watcher, as the resources are read in the constructor of the class
if (Looper.myLooper() == null) {
Looper.prepare();
}
TvRemoteProviderWatcher watcher =
new TvRemoteProviderWatcher(mMockContext, new Object());
new TvRemoteProviderWatcher(
mMockContext, new Object(), new Handler(Looper.getMainLooper()));
assertFalse(watcher.verifyServiceTrusted(createTvServiceInfo()));
}