1 /* 2 * Copyright (C) 2019 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.managedprovisioning.analytics; 18 19 import static com.android.managedprovisioning.analytics.AnalyticsRoboTestUtils.assertDevicePolicyEventLoggersEqual; 20 21 import android.app.admin.DevicePolicyEventLogger; 22 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.robolectric.Robolectric; 26 import org.robolectric.RobolectricTestRunner; 27 28 import java.io.File; 29 import java.util.ArrayList; 30 import java.util.Arrays; 31 import java.util.List; 32 33 /** 34 * Robolectric integration tests for {@link DeferredMetricsWriter} and 35 * {@link DeferredMetricsReader}. 36 */ 37 @RunWith(RobolectricTestRunner.class) 38 public class DeferredMetricsWriteReadRoboTest { 39 40 private static final DevicePolicyEventLogger[] EVENTS_TO_WRITE 41 = new DevicePolicyEventLogger[] { 42 DevicePolicyEventLogger 43 .createEvent(124) 44 .setAdmin("test2") 45 .setBoolean(true) 46 .setInt(37) 47 .setStrings("one", "two") 48 .setTimePeriod(50231L), 49 DevicePolicyEventLogger.createEvent(125), 50 DevicePolicyEventLogger.createEvent(126) 51 .setAdmin("test3") 52 .setStrings("one", null, null, "three"), 53 DevicePolicyEventLogger.createEvent(127) 54 .setStrings(new String[]{null, null, null}) 55 }; 56 57 @Test writeRead_deferredMetricsWriter_eventsAreCorrect()58 public void writeRead_deferredMetricsWriter_eventsAreCorrect() { 59 final File file = new File("test-file"); 60 61 writeMetricsToFile(EVENTS_TO_WRITE, file); 62 final DevicePolicyEventLogger[] eventsRead = readMetricsFromFile(file); 63 64 assertDevicePolicyEventLoggersEqual(EVENTS_TO_WRITE, eventsRead); 65 } 66 readMetricsFromFile(File file)67 private DevicePolicyEventLogger[] readMetricsFromFile(File file) { 68 final List<DevicePolicyEventLogger> eventsList = new ArrayList<>(); 69 final ProcessMetricsJobService processMetricsJobService = new ProcessMetricsJobService( 70 loggers -> eventsList.addAll(Arrays.asList(loggers))); 71 processMetricsJobService.executeReadDeferredMetrics(/* params */ null, file); 72 Robolectric.flushBackgroundThreadScheduler(); 73 return eventsList.toArray(new DevicePolicyEventLogger[0]); 74 } 75 writeMetricsToFile( DevicePolicyEventLogger[] devicePolicyEvent, File file)76 private void writeMetricsToFile( 77 DevicePolicyEventLogger[] devicePolicyEvent, File file) { 78 final DeferredMetricsWriter writer = new DeferredMetricsWriter(file); 79 writer.write(devicePolicyEvent); 80 Robolectric.flushBackgroundThreadScheduler(); 81 } 82 } 83