1 /* 2 * Copyright (C) 2017 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.server.cts; 18 19 import android.service.print.ActivePrintServiceProto; 20 import android.service.print.CachedPrintJobProto; 21 import android.service.print.PrinterDiscoverySessionProto; 22 import android.service.print.PrintDocumentInfoProto; 23 import android.service.print.PrinterIdProto; 24 import android.service.print.PrinterInfoProto; 25 import android.service.print.PrintJobInfoProto; 26 import android.service.print.PrintServiceDumpProto; 27 import android.service.print.PrintSpoolerInternalStateProto; 28 import android.service.print.PrintSpoolerStateProto; 29 import android.service.print.PrintUserStateProto; 30 31 import com.android.tradefed.device.DeviceNotAvailableException; 32 import com.android.tradefed.device.ITestDevice; 33 import com.android.tradefed.log.LogUtil; 34 35 /** 36 * Test proto dump of print 37 */ 38 public class PrintProtoTest extends ProtoDumpTestCase { 39 /** 40 * Test that print dump is reasonable 41 * 42 * @throws Exception 43 */ testDump()44 public void testDump() throws Exception { 45 // If the device doesn't support printing, then pass. 46 if (!supportsPrinting(getDevice())) { 47 LogUtil.CLog.d("Bypass as android.software.print is not supported."); 48 return; 49 } 50 51 PrintServiceDumpProto dump = getDump(PrintServiceDumpProto.parser(), 52 "dumpsys print --proto"); 53 54 verifyPrintServiceDumpProto(dump, PRIVACY_NONE); 55 } 56 verifyPrintServiceDumpProto(PrintServiceDumpProto dump, final int filterLevel)57 static void verifyPrintServiceDumpProto(PrintServiceDumpProto dump, final int filterLevel) throws Exception { 58 if (dump.getUserStatesCount() > 0) { 59 PrintUserStateProto userState = dump.getUserStatesList().get(0); 60 assertEquals(0, userState.getUserId()); 61 } 62 63 for (PrintUserStateProto pus : dump.getUserStatesList()) { 64 for (ActivePrintServiceProto aps : pus.getActiveServicesList()) { 65 verifyActivePrintServiceProto(aps, filterLevel); 66 } 67 for (CachedPrintJobProto cpj : pus.getCachedPrintJobsList()) { 68 verifyPrintJobInfoProto(cpj.getPrintJob(), filterLevel); 69 } 70 for (PrinterDiscoverySessionProto pds : pus.getDiscoverySessionsList()) { 71 verifyPrinterDiscoverySessionProto(pds, filterLevel); 72 } 73 verifyPrintSpoolerStateProto(pus.getPrintSpoolerState(), filterLevel); 74 } 75 } 76 verifyActivePrintServiceProto(ActivePrintServiceProto aps, final int filterLevel)77 private static void verifyActivePrintServiceProto(ActivePrintServiceProto aps, final int filterLevel) throws Exception { 78 for (PrinterIdProto pip : aps.getTrackedPrintersList()) { 79 verifyPrinterIdProto(pip, filterLevel); 80 } 81 } 82 verifyPrinterDiscoverySessionProto(PrinterDiscoverySessionProto pds, final int filterLevel)83 private static void verifyPrinterDiscoverySessionProto(PrinterDiscoverySessionProto pds, final int filterLevel) throws Exception { 84 for (PrinterIdProto pip : pds.getTrackedPrinterRequestsList()) { 85 verifyPrinterIdProto(pip, filterLevel); 86 } 87 for (PrinterInfoProto pip : pds.getPrinterList()) { 88 verifyPrinterInfoProto(pip, filterLevel); 89 } 90 } 91 verifyPrintDocumentInfoProto(PrintDocumentInfoProto pdi, final int filterLevel)92 private static void verifyPrintDocumentInfoProto(PrintDocumentInfoProto pdi, final int filterLevel) throws Exception { 93 if (filterLevel == PRIVACY_AUTO) { 94 assertTrue(pdi.getName().isEmpty()); 95 } 96 assertTrue(0 <= pdi.getPageCount()); 97 assertTrue(0 <= pdi.getDataSize()); 98 } 99 verifyPrinterIdProto(PrinterIdProto pip, final int filterLevel)100 private static void verifyPrinterIdProto(PrinterIdProto pip, final int filterLevel) throws Exception { 101 if (filterLevel == PRIVACY_AUTO) { 102 assertTrue(pip.getLocalId().isEmpty()); 103 } 104 } 105 verifyPrinterInfoProto(PrinterInfoProto pip, final int filterLevel)106 private static void verifyPrinterInfoProto(PrinterInfoProto pip, final int filterLevel) throws Exception { 107 verifyPrinterIdProto(pip.getId(), filterLevel); 108 if (filterLevel == PRIVACY_AUTO) { 109 assertTrue(pip.getName().isEmpty()); 110 assertTrue(pip.getDescription().isEmpty()); 111 } 112 assertTrue( 113 PrinterInfoProto.Status.getDescriptor().getValues() 114 .contains(pip.getStatus().getValueDescriptor())); 115 } 116 verifyPrintJobInfoProto(PrintJobInfoProto pji, final int filterLevel)117 private static void verifyPrintJobInfoProto(PrintJobInfoProto pji, final int filterLevel) throws Exception { 118 if (filterLevel == PRIVACY_AUTO) { 119 assertTrue(pji.getLabel().isEmpty()); 120 assertTrue(pji.getPrintJobId().isEmpty()); 121 assertTrue(pji.getTag().isEmpty()); 122 } 123 assertTrue( 124 PrintJobInfoProto.State.getDescriptor().getValues() 125 .contains(pji.getState().getValueDescriptor())); 126 verifyPrinterIdProto(pji.getPrinter(), filterLevel); 127 verifyPrintDocumentInfoProto(pji.getDocumentInfo(), filterLevel); 128 } 129 verifyPrintSpoolerStateProto(PrintSpoolerStateProto pss, final int filterLevel)130 private static void verifyPrintSpoolerStateProto(PrintSpoolerStateProto pss, final int filterLevel) throws Exception { 131 verifyPrintSpoolerInternalStateProto(pss.getInternalState(), filterLevel); 132 } 133 verifyPrintSpoolerInternalStateProto(PrintSpoolerInternalStateProto psis, final int filterLevel)134 private static void verifyPrintSpoolerInternalStateProto(PrintSpoolerInternalStateProto psis, final int filterLevel) throws Exception { 135 for (PrintJobInfoProto pji : psis.getPrintJobsList()) { 136 verifyPrintJobInfoProto(pji, filterLevel); 137 } 138 if (filterLevel == PRIVACY_AUTO) { 139 assertTrue(0 == psis.getPrintJobFilesCount()); 140 } 141 } 142 supportsPrinting(ITestDevice device)143 static boolean supportsPrinting(ITestDevice device) throws DeviceNotAvailableException { 144 return device.hasFeature("android.software.print"); 145 } 146 } 147