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

Commit 5de8f058 authored by Joe Antonetti's avatar Joe Antonetti Committed by Android (Google) Code Review
Browse files

Merge "Add RemoteTaskRemovedMessage" into main

parents df165e3e 7fa9c4c8
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ message TaskContinuityMessage {
    oneof data {
        ContinuityDeviceConnected device_connected = 1;
        RemoteTaskAddedMessage remote_task_added_message = 2;
        RemoteTaskRemovedMessage remote_task_removed = 3;
    }
}

@@ -43,3 +44,7 @@ message RemoteTaskInfo {
    int64 lastUsedTimeMillis = 3;
    bytes taskIcon = 4;
}

message RemoteTaskRemovedMessage {
    int32 taskId = 1;
}
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.companion.datatransfer.continuity.messages;

import android.util.proto.ProtoInputStream;
import android.util.proto.ProtoOutputStream;

import java.io.IOException;

public record RemoteTaskRemovedMessage(int taskId) implements TaskContinuityMessageData {

    public static RemoteTaskRemovedMessage readFromProto(ProtoInputStream pis) throws IOException {
        int taskId = 0;
        while (pis.nextField() != ProtoInputStream.NO_MORE_FIELDS) {
            switch (pis.getFieldNumber()) {
                case (int) android.companion.RemoteTaskRemovedMessage.TASK_ID:
                    taskId = pis.readInt(
                        android.companion.RemoteTaskRemovedMessage.TASK_ID
                    );
                    break;
            }
        }

        return new RemoteTaskRemovedMessage(taskId);
    }

    @Override
    public void writeToProto(ProtoOutputStream pos) {
        pos.write(
            android.companion.RemoteTaskRemovedMessage.TASK_ID,
            taskId());
    }
}
 No newline at end of file
+14 −0
Original line number Diff line number Diff line
@@ -54,6 +54,13 @@ public final class TaskContinuityMessage {
                    mData = new RemoteTaskAddedMessage(pis);
                    pis.end(remoteTaskAddedMessageToken);
                    break;
                case (int) android.companion.TaskContinuityMessage.REMOTE_TASK_REMOVED:
                    final long remoteTaskRemovedToken = pis.start(
                        android.companion.TaskContinuityMessage.REMOTE_TASK_REMOVED
                    );
                    mData = RemoteTaskRemovedMessage.readFromProto(pis);
                    pis.end(remoteTaskRemovedToken);
                    break;
            }
        }
    }
@@ -87,6 +94,13 @@ public final class TaskContinuityMessage {
                remoteTaskAddedMessage.writeToProto(pos);
                pos.end(remoteTaskAddedMessageToken);
                break;
            case RemoteTaskRemovedMessage remoteTaskRemovedMessage:
                long remoteTaskRemovedToken = pos.start(
                    android.companion.TaskContinuityMessage.REMOTE_TASK_REMOVED
                );
                remoteTaskRemovedMessage.writeToProto(pos);
                pos.end(remoteTaskRemovedToken);
                break;
            default:
                break;
        }
+125 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.companion.datatransfer.continuity.messages;


import static com.google.common.truth.Truth.assertThat;
import static org.testng.Assert.expectThrows;

import android.platform.test.annotations.Presubmit;
import android.testing.AndroidTestingRunner;

import android.util.proto.ProtoInputStream;
import android.util.proto.ProtoOutputStream;
import android.util.proto.ProtoParseException;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

@Presubmit
@RunWith(AndroidTestingRunner.class)
public class RemoteTaskRemovedMessageTest {

    @Test
    public void testConstructor_fromTaskId() {
        final int taskId = 1234;
        final RemoteTaskRemovedMessage remoteTaskRemovedMessage
            = new RemoteTaskRemovedMessage(taskId);
        assertThat(remoteTaskRemovedMessage.taskId())
            .isEqualTo(taskId);
    }

    @Test
    public void readFromProto_readsValidProto()
        throws IOException, ProtoParseException {

        final int taskId = 1234;
        final ProtoOutputStream pos = new ProtoOutputStream();
        pos.writeInt32(
            android.companion.RemoteTaskRemovedMessage.TASK_ID,
            taskId);
        pos.flush();

        final ProtoInputStream pis = new ProtoInputStream(pos.getBytes());
        final RemoteTaskRemovedMessage remoteTaskRemovedMessage
            = RemoteTaskRemovedMessage.readFromProto(pis);

        assertThat(remoteTaskRemovedMessage.taskId())
            .isEqualTo(taskId);
    }

    @Test
    public void testConstructor_fromProto_setsToDefaultTaskId()
        throws IOException, ProtoParseException {

        RemoteTaskRemovedMessage remoteTaskRemovedMessage
            = RemoteTaskRemovedMessage.readFromProto(new ProtoInputStream(new byte[0]));

        assertThat(remoteTaskRemovedMessage.taskId())
            .isEqualTo(0);
    }

    @Test
    public void testWriteToProto_writesValidProto() throws IOException {
        int taskId = 1234;
        RemoteTaskRemovedMessage remoteTaskRemovedMessage
            = new RemoteTaskRemovedMessage(taskId);
        final ProtoOutputStream pos = new ProtoOutputStream();
        remoteTaskRemovedMessage.writeToProto(pos);
        pos.flush();

        final ProtoInputStream pis = new ProtoInputStream(pos.getBytes());
        pis.nextField();
        long taskIdFieldNumber =
            android.companion.RemoteTaskRemovedMessage.TASK_ID;
        assertThat(pis.getFieldNumber())
            .isEqualTo((int) taskIdFieldNumber);
        assertThat(pis.readInt(taskIdFieldNumber))
            .isEqualTo(taskId);
        pis.nextField();
        assertThat(pis.nextField()).isEqualTo(ProtoInputStream.NO_MORE_FIELDS);
    }

    @Test
    public void testWriteAndRead_roundTrip_works() throws IOException {
        int taskId = 1234;
        RemoteTaskRemovedMessage expected
            = new RemoteTaskRemovedMessage(taskId);
        final ProtoOutputStream pos = new ProtoOutputStream();
        expected.writeToProto(pos);
        pos.flush();

        final ProtoInputStream pis = new ProtoInputStream(pos.getBytes());
        final RemoteTaskRemovedMessage actual
            = RemoteTaskRemovedMessage.readFromProto(pis);

        assertThat(actual.taskId())
            .isEqualTo(expected.taskId());
    }

    @Test
    public void testEquals_works() {
        int taskId = 1234;
        RemoteTaskRemovedMessage expected
            = new RemoteTaskRemovedMessage(taskId);
        RemoteTaskRemovedMessage actual
            = new RemoteTaskRemovedMessage(taskId);
        assertThat(actual).isEqualTo(expected);
    }
}
 No newline at end of file