1 /* 2 * Copyright 2022 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 android.ondevicepersonalization; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.os.Parcel; 22 import android.os.PersistableBundle; 23 24 import androidx.test.ext.junit.runners.AndroidJUnit4; 25 import androidx.test.filters.SmallTest; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 /** 31 * Unit Tests of Framework API Classes. 32 */ 33 @SmallTest 34 @RunWith(AndroidJUnit4.class) 35 public class OnDevicePersonalizationFrameworkClassesTest { 36 /** 37 * Tests that the ExecuteOutput object serializes correctly. 38 */ 39 @Test testExecuteOutput()40 public void testExecuteOutput() { 41 ExecuteOutput result = 42 new ExecuteOutput.Builder() 43 .addSlotResults( 44 new SlotResult.Builder().setSlotKey("abc") 45 .addRenderedBidKeys("bid1") 46 .addLoggedBids( 47 new Bid.Builder() 48 .setKey("bid1") 49 .setMetrics(new Metrics.Builder() 50 .setLongValues(11).build()) 51 .build()) 52 .addLoggedBids( 53 new Bid.Builder() 54 .setKey("bid2") 55 .build()) 56 .build()) 57 .build(); 58 59 Parcel parcel = Parcel.obtain(); 60 result.writeToParcel(parcel, 0); 61 parcel.setDataPosition(0); 62 ExecuteOutput result2 = ExecuteOutput.CREATOR.createFromParcel(parcel); 63 64 SlotResult slotResult = result2.getSlotResults().get(0); 65 assertEquals("abc", slotResult.getSlotKey()); 66 assertEquals("bid1", slotResult.getLoggedBids().get(0).getKey()); 67 assertEquals("bid1", slotResult.getRenderedBidKeys().get(0)); 68 assertEquals(11, slotResult.getLoggedBids().get(0).getMetrics().getLongValues()[0]); 69 assertEquals("bid2", slotResult.getLoggedBids().get(1).getKey()); 70 } 71 72 /** 73 * Tests that the SlotInfo object serializes correctly. 74 */ 75 @Test testSlotInfo()76 public void testSlotInfo() { 77 SlotInfo slotInfo = new SlotInfo.Builder().setWidth(100).setHeight(50).build(); 78 79 Parcel parcel = Parcel.obtain(); 80 slotInfo.writeToParcel(parcel, 0); 81 parcel.setDataPosition(0); 82 SlotInfo slotInfo2 = SlotInfo.CREATOR.createFromParcel(parcel); 83 84 assertEquals(slotInfo, slotInfo2); 85 assertEquals(100, slotInfo2.getWidth()); 86 assertEquals(50, slotInfo2.getHeight()); 87 } 88 89 /** 90 * Tests that the RenderOutput object serializes correctly. 91 */ 92 @Test testRenderOutput()93 public void testRenderOutput() { 94 RenderOutput result = new RenderOutput.Builder().setContent("abc").build(); 95 96 Parcel parcel = Parcel.obtain(); 97 result.writeToParcel(parcel, 0); 98 parcel.setDataPosition(0); 99 RenderOutput result2 = RenderOutput.CREATOR.createFromParcel(parcel); 100 101 assertEquals(result, result2); 102 assertEquals("abc", result2.getContent()); 103 } 104 105 /** 106 * Tests that the DownloadOutput object serializes correctly. 107 */ 108 @Test teetDownloadOutput()109 public void teetDownloadOutput() { 110 DownloadOutput result = new DownloadOutput.Builder() 111 .addKeysToRetain("abc").addKeysToRetain("def").build(); 112 113 Parcel parcel = Parcel.obtain(); 114 result.writeToParcel(parcel, 0); 115 parcel.setDataPosition(0); 116 DownloadOutput result2 = DownloadOutput.CREATOR.createFromParcel(parcel); 117 118 assertEquals(result, result2); 119 assertEquals("abc", result2.getKeysToRetain().get(0)); 120 assertEquals("def", result2.getKeysToRetain().get(1)); 121 } 122 123 /** 124 * Tests that the Metrics object serializes correctly. 125 */ 126 @Test testMetrics()127 public void testMetrics() { 128 long[] intMetrics = {10, 20}; 129 double[] floatMetrics = {5.0}; 130 Metrics result = new Metrics.Builder() 131 .setLongValues(intMetrics) 132 .setDoubleValues(floatMetrics) 133 .setBooleanValues(true).build(); 134 135 Parcel parcel = Parcel.obtain(); 136 result.writeToParcel(parcel, 0); 137 parcel.setDataPosition(0); 138 Metrics result2 = Metrics.CREATOR.createFromParcel(parcel); 139 140 assertEquals(result, result2); 141 assertEquals(10, result2.getLongValues()[0]); 142 assertEquals(20, result2.getLongValues()[1]); 143 assertEquals(5.0, result2.getDoubleValues()[0], 0.0); 144 assertEquals(true, result2.getBooleanValues()[0]); 145 } 146 147 /** 148 * Tests that the EventInput object serializes correctly. 149 */ 150 @Test testEventInput()151 public void testEventInput() { 152 PersistableBundle params = new PersistableBundle(); 153 params.putInt("x", 3); 154 EventInput result = new EventInput.Builder() 155 .setEventType(6) 156 .setBid(new Bid.Builder().setKey("a").build()) 157 .build(); 158 159 Parcel parcel = Parcel.obtain(); 160 result.writeToParcel(parcel, 0); 161 parcel.setDataPosition(0); 162 EventInput result2 = EventInput.CREATOR.createFromParcel(parcel); 163 164 assertEquals(6, result2.getEventType()); 165 assertEquals("a", result2.getBid().getKey()); 166 } 167 168 /** 169 * Tests that the Metrics object serializes correctly. 170 */ 171 @Test testEventOutput()172 public void testEventOutput() { 173 long[] intMetrics = {10, 20}; 174 double[] floatMetrics = {5.0}; 175 EventOutput result = new EventOutput.Builder() 176 .setMetrics(new Metrics.Builder().setLongValues(11).build()).build(); 177 178 Parcel parcel = Parcel.obtain(); 179 result.writeToParcel(parcel, 0); 180 parcel.setDataPosition(0); 181 EventOutput result2 = EventOutput.CREATOR.createFromParcel(parcel); 182 183 assertEquals(result, result2); 184 assertEquals(11, result2.getMetrics().getLongValues()[0]); 185 } 186 } 187