Add Event.java

Test: unit test
Bug: 200231384
Change-Id: I3c99f8ddfeb26858dcf8522d474df4b89c2cc588
diff --git a/nearby/tests/Android.bp b/nearby/tests/Android.bp
index ec66b32..8a07882 100644
--- a/nearby/tests/Android.bp
+++ b/nearby/tests/Android.bp
@@ -34,10 +34,11 @@
 
     static_libs: [
         "androidx.test.rules",
-        "mockito-target",
+        "fast-pair-lite-protos",
         "framework-nearby-pre-jarjar",
         "guava",
         "libprotobuf-java-lite",
+        "mockito-target",
         "platform-test-annotations",
         "service-nearby",
         "truth-prebuilt",
diff --git a/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/EventTest.java b/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/EventTest.java
new file mode 100644
index 0000000..e2f42ee
--- /dev/null
+++ b/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/EventTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2021 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.nearby.common.bluetooth.fastpair;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.os.Parcel;
+import android.platform.test.annotations.Presubmit;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.server.nearby.proto.NearbyEventCodes.NearbyEvent.EventCode;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Unit tests for {@link EventTest}.
+ */
+@Presubmit
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class EventTest {
+
+    private static final String EXCEPTION_MESSAGE = "Test exception";
+    private static final long TIMESTAMP = 1234L;
+    private static final EventCode EVENT_CODE = EventCode.CREATE_BOND;
+    private static final BluetoothDevice BLUETOOTH_DEVICE = BluetoothAdapter.getDefaultAdapter()
+            .getRemoteDevice("11:22:33:44:55:66");
+    private static final Short PROFILE = (short) 1;
+
+    @Test
+    public void createAndReadFromParcel() {
+        Event event =
+                Event.builder()
+                        .setException(new Exception(EXCEPTION_MESSAGE))
+                        .setTimestamp(TIMESTAMP)
+                        .setEventCode(EVENT_CODE)
+                        .setBluetoothDevice(BLUETOOTH_DEVICE)
+                        .setProfile(PROFILE)
+                        .build();
+
+        Parcel parcel = Parcel.obtain();
+        event.writeToParcel(parcel, event.describeContents());
+        parcel.setDataPosition(0);
+        Event result = Event.CREATOR.createFromParcel(parcel);
+
+        assertThat(result.getException()).hasMessageThat()
+                .isEqualTo(event.getException().getMessage());
+        assertThat(result.getTimestamp()).isEqualTo(event.getTimestamp());
+        assertThat(result.getEventCode()).isEqualTo(event.getEventCode());
+        assertThat(result.getBluetoothDevice()).isEqualTo(event.getBluetoothDevice());
+        assertThat(result.getProfile()).isEqualTo(event.getProfile());
+    }
+}