1  /*
2   * Copyright (C) 2024 The Android Open Source Project
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *      http://www.apache.org/licenses/LICENSE-2.0
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.android.extensions.appfunctions.tests
18  
19  import android.app.appfunctions.AppFunctionException
20  import android.app.appfunctions.ExecuteAppFunctionRequest
21  import android.app.appfunctions.ExecuteAppFunctionResponse
22  import android.app.appsearch.GenericDocument
23  import android.os.Bundle
24  import androidx.test.ext.junit.runners.AndroidJUnit4
25  import com.android.extensions.appfunctions.SidecarConverter
26  import com.google.common.truth.Truth.assertThat
27  import org.junit.Test
28  import org.junit.runner.RunWith
29  
30  @RunWith(AndroidJUnit4::class)
31  class SidecarConverterTest {
32      @Test
getSidecarExecuteAppFunctionRequest_sameContentsnull33      fun getSidecarExecuteAppFunctionRequest_sameContents() {
34          val extras = Bundle()
35          extras.putString("extra", "value")
36          val parameters: GenericDocument =
37              GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "")
38                  .setPropertyLong("testLong", 23)
39                  .build()
40          val platformRequest: ExecuteAppFunctionRequest =
41              ExecuteAppFunctionRequest.Builder("targetPkg", "targetFunctionId")
42                  .setExtras(extras)
43                  .setParameters(parameters)
44                  .build()
45  
46          val sidecarRequest = SidecarConverter.getSidecarExecuteAppFunctionRequest(platformRequest)
47  
48          assertThat(sidecarRequest.targetPackageName).isEqualTo("targetPkg")
49          assertThat(sidecarRequest.functionIdentifier).isEqualTo("targetFunctionId")
50          assertThat(sidecarRequest.parameters).isEqualTo(parameters)
51          assertThat(sidecarRequest.extras.size()).isEqualTo(1)
52          assertThat(sidecarRequest.extras.getString("extra")).isEqualTo("value")
53      }
54  
55      @Test
getPlatformExecuteAppFunctionRequest_sameContentsnull56      fun getPlatformExecuteAppFunctionRequest_sameContents() {
57          val extras = Bundle()
58          extras.putString("extra", "value")
59          val parameters: GenericDocument =
60              GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "")
61                  .setPropertyLong("testLong", 23)
62                  .build()
63          val sidecarRequest =
64              com.android.extensions.appfunctions.ExecuteAppFunctionRequest.Builder(
65                  "targetPkg",
66                  "targetFunctionId"
67              )
68                  .setExtras(extras)
69                  .setParameters(parameters)
70                  .build()
71  
72          val platformRequest = SidecarConverter.getPlatformExecuteAppFunctionRequest(sidecarRequest)
73  
74          assertThat(platformRequest.targetPackageName).isEqualTo("targetPkg")
75          assertThat(platformRequest.functionIdentifier).isEqualTo("targetFunctionId")
76          assertThat(platformRequest.parameters).isEqualTo(parameters)
77          assertThat(platformRequest.extras.size()).isEqualTo(1)
78          assertThat(platformRequest.extras.getString("extra")).isEqualTo("value")
79      }
80  
81      @Test
getSidecarExecuteAppFunctionResponse_successResponse_sameContentsnull82      fun getSidecarExecuteAppFunctionResponse_successResponse_sameContents() {
83          val resultGd: GenericDocument =
84              GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "")
85                  .setPropertyBoolean(ExecuteAppFunctionResponse.PROPERTY_RETURN_VALUE, true)
86                  .build()
87          val platformResponse = ExecuteAppFunctionResponse(resultGd)
88  
89          val sidecarResponse = SidecarConverter.getSidecarExecuteAppFunctionResponse(
90              platformResponse
91          )
92  
93          assertThat(
94              sidecarResponse.resultDocument.getProperty(
95                  ExecuteAppFunctionResponse.PROPERTY_RETURN_VALUE
96              )
97          )
98              .isEqualTo(booleanArrayOf(true))
99      }
100  
101      @Test
getSidecarAppFunctionException_sameContentsnull102      fun getSidecarAppFunctionException_sameContents() {
103          val bundle = Bundle()
104          bundle.putString("key", "value")
105          val platformException =
106              AppFunctionException(
107                  AppFunctionException.ERROR_SYSTEM_ERROR,
108                  "error",
109                  bundle
110              )
111  
112          val sidecarException = SidecarConverter.getSidecarAppFunctionException(
113              platformException
114          )
115  
116          assertThat(sidecarException.errorCode).isEqualTo(AppFunctionException.ERROR_SYSTEM_ERROR)
117          assertThat(sidecarException.errorMessage).isEqualTo("error")
118          assertThat(sidecarException.extras.getString("key")).isEqualTo("value")
119      }
120  
121      @Test
getPlatformExecuteAppFunctionResponse_successResponse_sameContentsnull122      fun getPlatformExecuteAppFunctionResponse_successResponse_sameContents() {
123          val resultGd: GenericDocument =
124              GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "")
125                  .setPropertyBoolean(ExecuteAppFunctionResponse.PROPERTY_RETURN_VALUE, true)
126                  .build()
127          val sidecarResponse =
128              com.android.extensions.appfunctions.ExecuteAppFunctionResponse(resultGd)
129  
130          val platformResponse = SidecarConverter.getPlatformExecuteAppFunctionResponse(
131              sidecarResponse
132          )
133  
134          assertThat(
135              platformResponse.resultDocument.getProperty(
136                  ExecuteAppFunctionResponse.PROPERTY_RETURN_VALUE
137              )
138          )
139              .isEqualTo(booleanArrayOf(true))
140      }
141  
142      @Test
getPlatformAppFunctionException_sameContentsnull143      fun getPlatformAppFunctionException_sameContents() {
144          val bundle = Bundle()
145          bundle.putString("key", "value")
146          val sidecarException =
147              com.android.extensions.appfunctions.AppFunctionException(
148                  AppFunctionException.ERROR_SYSTEM_ERROR,
149                  "error",
150                  bundle
151              )
152  
153          val platformException = SidecarConverter.getPlatformAppFunctionException(
154              sidecarException
155          )
156  
157          assertThat(platformException.errorCode)
158              .isEqualTo(AppFunctionException.ERROR_SYSTEM_ERROR)
159          assertThat(platformException.errorMessage).isEqualTo("error")
160          assertThat(platformException.extras.getString("key")).isEqualTo("value")
161      }
162  }
163