Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 099faf74 authored by Joanne Chung's avatar Joanne Chung
Browse files

Fix broken tests in TextClassifierServiceTest.

We introduce SystemTCMetadata in ag/10605740 which centrializes the
needed information for the TCMS to process the requests. Because we
change the getPackageName() logic to return the calling package name
of SystemTCMetadata and the SystemTCMetadata is only created if the
event is from the SystemTC not other TCs. The tests fail because the
SelectionEvent.packageName is not populated.

This cl contains the following changes:
- Return the package name that the SelectionEvent or
TextClassificationContext originated from
- Fix incorrect javadoc of TextClassificationContext.getPackageName
- Always verify the calling package name for onSelectionEvent and
onTextClassifierEvent

Bug: 151614815
Test: atest TextClassifierServiceTest
Test: atest FrameworksCoreTests:android.view.textclassifier
Test: atest FrameworksCoreTests:android.widget.TextViewActivityTest
Test: atest CtsTextClassifierTestCases
Change-Id: Iabe1691d63e3ec6ac834984cf79b60faf9c0e765
parent 1660402a
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -158,7 +158,6 @@ public final class SelectionEvent implements Parcelable {
        mEventType = in.readInt();
        mEntityType = in.readString();
        mWidgetVersion = in.readInt() > 0 ? in.readString() : null;
        // TODO: remove mPackageName once aiai does not need it
        mPackageName = in.readString();
        mWidgetType = in.readString();
        mInvocationMethod = in.readInt();
@@ -186,7 +185,6 @@ public final class SelectionEvent implements Parcelable {
        if (mWidgetVersion != null) {
            dest.writeString(mWidgetVersion);
        }
        // TODO: remove mPackageName once aiai does not need it
        dest.writeString(mPackageName);
        dest.writeString(mWidgetType);
        dest.writeInt(mInvocationMethod);
@@ -406,7 +404,7 @@ public final class SelectionEvent implements Parcelable {
     */
    @NonNull
    public String getPackageName() {
        return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : "";
        return mPackageName;
    }

    /**
+1 −6
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import java.util.Objects;
 */
public final class TextClassificationContext implements Parcelable {

    // NOTE: Modify packageName only in the constructor or in setSystemTextClassifierMetadata()
    private String mPackageName;
    private final String mWidgetType;
    @Nullable private final String mWidgetVersion;
@@ -47,7 +46,7 @@ public final class TextClassificationContext implements Parcelable {
    }

    /**
     * Returns the package name for the calling package.
     * Returns the package name of the app that this context originated in.
     */
    @NonNull
    public String getPackageName() {
@@ -57,14 +56,10 @@ public final class TextClassificationContext implements Parcelable {
    /**
     * Sets the information about the {@link SystemTextClassifier} that sent this request.
     *
     * <p><b>NOTE: </b>This will override the value returned in {@link getPackageName()}.
     * @hide
     */
    void setSystemTextClassifierMetadata(@Nullable SystemTextClassifierMetadata systemTcMetadata) {
        mSystemTcMetadata = systemTcMetadata;
        if (mSystemTcMetadata != null) {
            mPackageName = mSystemTcMetadata.getCallingPackageName();
        }
    }

    /**
+2 −2
Original line number Diff line number Diff line
@@ -234,7 +234,7 @@ public final class TextClassificationManagerService extends ITextClassifierServi

        handleRequest(
                event.getSystemTextClassifierMetadata(),
                /* verifyCallingPackage= */ false,
                /* verifyCallingPackage= */ true,
                /* attemptToBind= */ false,
                service -> service.onSelectionEvent(sessionId, event),
                "onSelectionEvent",
@@ -253,7 +253,7 @@ public final class TextClassificationManagerService extends ITextClassifierServi

        handleRequest(
                systemTcMetadata,
                /* verifyCallingPackage= */ false,
                /* verifyCallingPackage= */ true,
                /* attemptToBind= */ false,
                service -> service.onTextClassifierEvent(sessionId, event),
                "onTextClassifierEvent",