Merge "Fix shell command bugs for supporting CTS test. Add service unit test. Fix javadoc." into tm-qpr-dev

This commit is contained in:
Lili Zou
2022-08-30 15:03:54 +00:00
committed by Android (Google) Code Review
4 changed files with 80 additions and 29 deletions

View File

@@ -153,7 +153,7 @@ public final class AmbientContextManager {
* eventTypes.add(AmbientContextEvent.EVENT_SNORE); * eventTypes.add(AmbientContextEvent.EVENT_SNORE);
* *
* // Create Consumer * // Create Consumer
* Consumer<Integer> statusConsumer = response -> { * Consumer<Integer> statusConsumer = status -> {
* int status = status.getStatusCode(); * int status = status.getStatusCode();
* if (status == AmbientContextManager.STATUS_SUCCESS) { * if (status == AmbientContextManager.STATUS_SUCCESS) {
* // Show user it's enabled * // Show user it's enabled

View File

@@ -21,12 +21,12 @@ import static java.lang.System.out;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.app.ambientcontext.AmbientContextEvent; import android.app.ambientcontext.AmbientContextEvent;
import android.app.ambientcontext.AmbientContextEventRequest; import android.app.ambientcontext.AmbientContextEventRequest;
import android.app.ambientcontext.AmbientContextManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.os.Binder; import android.os.Binder;
import android.os.RemoteCallback; import android.os.RemoteCallback;
import android.os.ShellCommand; import android.os.ShellCommand;
import android.service.ambientcontext.AmbientContextDetectionResult; import android.service.ambientcontext.AmbientContextDetectionResult;
import android.service.ambientcontext.AmbientContextDetectionServiceStatus;
import java.io.PrintWriter; import java.io.PrintWriter;
@@ -51,13 +51,13 @@ final class AmbientContextShellCommand extends ShellCommand {
/** Callbacks for AmbientContextEventService results used internally for testing. */ /** Callbacks for AmbientContextEventService results used internally for testing. */
static class TestableCallbackInternal { static class TestableCallbackInternal {
private AmbientContextDetectionResult mLastResult; private AmbientContextDetectionResult mLastResult;
private AmbientContextDetectionServiceStatus mLastStatus; private int mLastStatus;
public AmbientContextDetectionResult getLastResult() { public AmbientContextDetectionResult getLastResult() {
return mLastResult; return mLastResult;
} }
public AmbientContextDetectionServiceStatus getLastStatus() { public int getLastStatus() {
return mLastStatus; return mLastStatus;
} }
@@ -80,13 +80,10 @@ final class AmbientContextShellCommand extends ShellCommand {
@NonNull @NonNull
private RemoteCallback createRemoteStatusCallback() { private RemoteCallback createRemoteStatusCallback() {
return new RemoteCallback(result -> { return new RemoteCallback(result -> {
AmbientContextDetectionServiceStatus status = int status = result.getInt(AmbientContextManager.STATUS_RESPONSE_BUNDLE_KEY);
(AmbientContextDetectionServiceStatus) result.get(
AmbientContextDetectionServiceStatus.STATUS_RESPONSE_BUNDLE_KEY);
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
mLastStatus = status; mLastStatus = status;
out.println("Status available: " + status);
} finally { } finally {
Binder.restoreCallingIdentity(token); Binder.restoreCallingIdentity(token);
} }
@@ -110,8 +107,6 @@ final class AmbientContextShellCommand extends ShellCommand {
return runStopDetection(); return runStopDetection();
case "get-last-status-code": case "get-last-status-code":
return getLastStatusCode(); return getLastStatusCode();
case "get-last-package-name":
return getLastPackageName();
case "query-service-status": case "query-service-status":
return runQueryServiceStatus(); return runQueryServiceStatus();
case "get-bound-package": case "get-bound-package":
@@ -126,7 +121,8 @@ final class AmbientContextShellCommand extends ShellCommand {
private int runStartDetection() { private int runStartDetection() {
final int userId = Integer.parseInt(getNextArgRequired()); final int userId = Integer.parseInt(getNextArgRequired());
final String packageName = getNextArgRequired(); final String packageName = getNextArgRequired();
mService.startDetection(userId, REQUEST, packageName, mService.startDetection(
userId, REQUEST, packageName,
sTestableCallbackInternal.createRemoteDetectionResultCallback(), sTestableCallbackInternal.createRemoteDetectionResultCallback(),
sTestableCallbackInternal.createRemoteStatusCallback()); sTestableCallbackInternal.createRemoteStatusCallback());
return 0; return 0;
@@ -151,18 +147,9 @@ final class AmbientContextShellCommand extends ShellCommand {
} }
private int getLastStatusCode() { private int getLastStatusCode() {
AmbientContextDetectionServiceStatus lastResponse = final PrintWriter resultPrinter = getOutPrintWriter();
sTestableCallbackInternal.getLastStatus(); int lastStatus = sTestableCallbackInternal.getLastStatus();
if (lastResponse == null) { resultPrinter.println(lastStatus);
return -1;
}
return lastResponse.getStatusCode();
}
private int getLastPackageName() {
AmbientContextDetectionServiceStatus lastResponse =
sTestableCallbackInternal.getLastStatus();
out.println(lastResponse == null ? "" : lastResponse.getPackageName());
return 0; return 0;
} }
@@ -174,22 +161,21 @@ final class AmbientContextShellCommand extends ShellCommand {
pw.println(" Print this help text."); pw.println(" Print this help text.");
pw.println(); pw.println();
pw.println(" start-detection USER_ID PACKAGE_NAME: Starts AmbientContextEvent detection."); pw.println(" start-detection USER_ID PACKAGE_NAME: Starts AmbientContextEvent detection.");
pw.println(" stop-detection USER_ID: Stops AmbientContextEvent detection."); pw.println(" stop-detection USER_ID PACKAGE_NAME: Stops AmbientContextEvent detection.");
pw.println(" get-last-status-code: Prints the latest request status code."); pw.println(" get-last-status-code: Prints the latest request status code.");
pw.println(" get-last-package-name: Prints the latest request package name."); pw.println(" query-service-status USER_ID PACKAGE_NAME: Prints the service status code.");
pw.println(" query-event-status USER_ID PACKAGE_NAME: Prints the event status code.");
pw.println(" get-bound-package USER_ID:" pw.println(" get-bound-package USER_ID:"
+ " Print the bound package that implements the service."); + " Print the bound package that implements the service.");
pw.println(" set-temporary-service USER_ID [COMPONENT_NAME DURATION]"); pw.println(" set-temporary-service USER_ID [PACKAGE_NAME] [COMPONENT_NAME DURATION]");
pw.println(" Temporarily (for DURATION ms) changes the service implementation."); pw.println(" Temporarily (for DURATION ms) changes the service implementation.");
pw.println(" To reset, call with just the USER_ID argument."); pw.println(" To reset, call with just the USER_ID argument.");
} }
private int getBoundPackageName() { private int getBoundPackageName() {
final PrintWriter out = getOutPrintWriter(); final PrintWriter resultPrinter = getOutPrintWriter();
final int userId = Integer.parseInt(getNextArgRequired()); final int userId = Integer.parseInt(getNextArgRequired());
final ComponentName componentName = mService.getComponentName(userId); final ComponentName componentName = mService.getComponentName(userId);
out.println(componentName == null ? "" : componentName.getPackageName()); resultPrinter.println(componentName == null ? "" : componentName.getPackageName());
return 0; return 0;
} }

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2022 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.server.ambientcontext;
import static com.google.common.truth.Truth.assertThat;
import android.app.PendingIntent;
import android.app.ambientcontext.AmbientContextEvent;
import android.app.ambientcontext.AmbientContextEventRequest;
import android.content.Intent;
import android.os.RemoteCallback;
import android.os.UserHandle;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import org.junit.Test;
/**
* Unit test for {@link AmbientContextManagerService}.
* atest FrameworksServicesTests:AmbientContextManagerServiceTest
*/
public class AmbientContextManagerServiceTest {
public static final String SYSTEM_PACKAGE_NAME = "com.android.frameworks.servicestests";
private static final int USER_ID = UserHandle.USER_SYSTEM;
@SmallTest
@Test
public void testClientRequest() {
AmbientContextEventRequest request = new AmbientContextEventRequest.Builder()
.addEventType(AmbientContextEvent.EVENT_COUGH)
.build();
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getBroadcast(
InstrumentationRegistry.getTargetContext(), 0,
intent, PendingIntent.FLAG_IMMUTABLE);
AmbientContextManagerService.ClientRequest clientRequest =
new AmbientContextManagerService.ClientRequest(USER_ID, request,
pendingIntent, new RemoteCallback(result -> {}));
assertThat(clientRequest.getRequest()).isEqualTo(request);
assertThat(clientRequest.getPackageName()).isEqualTo(SYSTEM_PACKAGE_NAME);
assertThat(clientRequest.hasUserId(USER_ID)).isTrue();
assertThat(clientRequest.hasUserId(-1)).isFalse();
assertThat(clientRequest.hasUserIdAndPackageName(USER_ID, SYSTEM_PACKAGE_NAME)).isTrue();
assertThat(clientRequest.hasUserIdAndPackageName(-1, SYSTEM_PACKAGE_NAME)).isFalse();
assertThat(clientRequest.hasUserIdAndPackageName(USER_ID, "random.package.name"))
.isFalse();
}
}

View File

@@ -0,0 +1 @@
include /services/core/java/com/android/server/ambientcontext/OWNERS