blob: ec8cd71accbeae60df66c095883847ece4959854 [file] [log] [blame]
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -08001/*
2 * Copyright (C) 2016 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
17package android.print;
18
19import android.annotation.NonNull;
20import android.content.ComponentName;
21import android.content.Context;
22import android.os.Bundle;
23import android.os.CancellationSignal;
24import android.os.Handler;
25import android.os.Looper;
26import android.os.ParcelFileDescriptor;
27import android.os.Process;
28import android.os.ServiceManager;
29import android.os.UserHandle;
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -080030import android.print.PrintAttributes.Margins;
31import android.print.PrintAttributes.MediaSize;
32import android.print.PrintAttributes.Resolution;
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -080033import android.printservice.PrintServiceInfo;
34
35import android.print.mockservice.MockPrintService;
36import android.print.mockservice.PrintServiceCallbacks;
37import android.print.mockservice.PrinterDiscoverySessionCallbacks;
38import android.print.mockservice.StubbablePrinterDiscoverySession;
39
Philip P. Moltmann01527ee2016-03-16 16:52:24 -070040import android.test.suitebuilder.annotation.LargeTest;
41import android.test.suitebuilder.annotation.MediumTest;
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -080042import org.mockito.invocation.InvocationOnMock;
43import org.mockito.stubbing.Answer;
44
45import java.util.ArrayList;
46import java.util.List;
47
48/**
49 * tests feeding all possible parameters to the IPrintManager Binder.
50 */
51public class IPrintManagerParametersTest extends BasePrintTest {
52
53 private final int BAD_APP_ID = 0xffffffff;
54
55 private final int mAppId;
56 private final int mUserId;
57 private final PrintJobId mBadPrintJobId;
58
59 private PrintJob mGoodPrintJob;
60 private PrinterId mBadPrinterId;
61 private PrinterId mGoodPrinterId;
62 private ComponentName mGoodComponentName;
63 private ComponentName mBadComponentName;
64
65 private IPrintManager mIPrintManager;
66
67 /**
68 * Create a new IPrintManagerParametersTest and setup basic fields.
69 */
70 public IPrintManagerParametersTest() {
71 super();
72
73 mAppId = UserHandle.getAppId(Process.myUid());
74 mUserId = UserHandle.myUserId();
75 mBadPrintJobId = new PrintJobId();
76 mBadComponentName = new ComponentName("bad", "bad");
77 }
78
79 /**
80 * Create a mock PrintDocumentAdapter.
81 *
82 * @return The adapter
83 */
84 private @NonNull PrintDocumentAdapter createMockAdapter() {
85 return new PrintDocumentAdapter() {
86 @Override
87 public void onStart() {
88 onStartCalled();
89 }
90
91 @Override
92 public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
93 CancellationSignal cancellationSignal, LayoutResultCallback callback,
94 Bundle extras) {
95 }
96
97 @Override
98 public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
99 CancellationSignal cancellationSignal, WriteResultCallback callback) {
100 }
101 };
102 }
103
104 /**
105 * Create mock print service callbacks.
106 *
107 * @return the callbacks
108 */
109 private PrintServiceCallbacks createMockCallbacks() {
110 return createMockPrintServiceCallbacks(
111 new Answer<PrinterDiscoverySessionCallbacks>() {
112 @Override
113 public PrinterDiscoverySessionCallbacks answer(InvocationOnMock invocation) {
114 return createMockPrinterDiscoverySessionCallbacks(new Answer<Void>() {
115 @Override
116 public Void answer(InvocationOnMock invocation) {
117 // Get the session.
118 StubbablePrinterDiscoverySession session =
119 ((PrinterDiscoverySessionCallbacks) invocation
120 .getMock()).getSession();
121
122 if (session.getPrinters().isEmpty()) {
123 final String PRINTER_NAME = "good printer";
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800124 List<PrinterInfo> printers = new ArrayList<>();
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800125
126 // Add the printer.
127 mGoodPrinterId = session.getService()
128 .generatePrinterId(PRINTER_NAME);
129
130 PrinterCapabilitiesInfo capabilities =
131 new PrinterCapabilitiesInfo.Builder(mGoodPrinterId)
132 .setMinMargins(
133 new Margins(200, 200, 200, 200))
134 .addMediaSize(MediaSize.ISO_A4, true)
135 .addResolution(new Resolution("300x300",
136 "300x300", 300, 300),
137 true)
138 .setColorModes(
139 PrintAttributes.COLOR_MODE_COLOR,
140 PrintAttributes.COLOR_MODE_COLOR)
141 .build();
142
143 PrinterInfo printer = new PrinterInfo.Builder(
144 mGoodPrinterId,
145 PRINTER_NAME,
146 PrinterInfo.STATUS_IDLE)
147 .setCapabilities(capabilities)
148 .build();
149 printers.add(printer);
150
151 session.addPrinters(printers);
152 }
153 onPrinterDiscoverySessionStartCalled();
154 return null;
155 }
156 }, null, null, null, null, null, null);
157 }
158 },
159 null, null);
160 }
161
162 /**
163 * Create a IPrintJobStateChangeListener object.
164 *
165 * @return the object
166 * @throws Exception if the object could not be created.
167 */
168 private IPrintJobStateChangeListener createMockIPrintJobStateChangeListener() throws Exception {
169 return new PrintManager.PrintJobStateChangeListenerWrapper(null,
170 new Handler(Looper.getMainLooper()));
171 }
172
173 /**
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800174 * Create a IPrintServicesChangeListener object.
175 *
176 * @return the object
177 * @throws Exception if the object could not be created.
178 */
179 private IPrintServicesChangeListener createMockIPrintServicesChangeListener() throws Exception {
180 return new PrintManager.PrintServicesChangeListenerWrapper(null,
181 new Handler(Looper.getMainLooper()));
182 }
183
184
185 /**
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800186 * Create a IPrinterDiscoveryObserver object.
187 *
188 * @return the object
189 * @throws Exception if the object could not be created.
190 */
191 private IPrinterDiscoveryObserver createMockIPrinterDiscoveryObserver() throws Exception {
192 return new PrinterDiscoverySession.PrinterDiscoveryObserver(null);
193 }
194
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800195 private void startPrinting() {
196 mGoodPrintJob = print(createMockAdapter(), null);
197
198 // Wait for PrintActivity to be ready
199 waitForStartAdapterCallbackCalled();
200
201 // Wait for printer discovery session to be ready
202 waitForPrinterDiscoverySessionStartCallbackCalled();
203 }
204
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800205 @Override
206 public void setUp() throws Exception {
207 super.setUp();
208
209 MockPrintService.setCallbacks(createMockCallbacks());
210
211 mGoodComponentName = getActivity().getComponentName();
212
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800213 mIPrintManager = IPrintManager.Stub
214 .asInterface(ServiceManager.getService(Context.PRINT_SERVICE));
215
216 // Generate dummy printerId which is a valid PrinterId object, but does not correspond to a
217 // printer
218 mBadPrinterId = new PrinterId(mGoodComponentName, "dummy printer");
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800219 }
220
221 /**
222 * {@link Runnable} that can throw and {@link Exception}
223 */
224 private interface Invokable {
225 /**
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800226 * Execute the invokable
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800227 *
228 * @throws Exception
229 */
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800230 void run() throws Exception;
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800231 }
232
233 /**
234 * Assert that the invokable throws an expectedException
235 *
236 * @param invokable The {@link Invokable} to run
237 * @param expectedClass The {@link Exception} that is supposed to be thrown
238 */
239 public void assertException(Invokable invokable, Class<? extends Exception> expectedClass)
240 throws Exception {
241 try {
242 invokable.run();
243 } catch (Exception e) {
244 if (e.getClass().isAssignableFrom(expectedClass)) {
245 return;
246 } else {
247 throw new AssertionError("Expected: " + expectedClass.getName() + ", got: "
248 + e.getClass().getName());
249 }
250 }
251
252 throw new AssertionError("No exception thrown");
253 }
254
255 /**
256 * test IPrintManager.getPrintJobInfo
257 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700258 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800259 public void testGetPrintJobInfo() throws Exception {
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800260 startPrinting();
261
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800262 assertEquals(mGoodPrintJob.getId(), mIPrintManager.getPrintJobInfo(mGoodPrintJob.getId(),
263 mAppId, mUserId).getId());
264 assertEquals(null, mIPrintManager.getPrintJobInfo(mBadPrintJobId, mAppId, mUserId));
265 assertEquals(null, mIPrintManager.getPrintJobInfo(null, mAppId, mUserId));
266
267 assertException(new Invokable() {
268 @Override
269 public void run() throws Exception {
270 mIPrintManager.getPrintJobInfo(mGoodPrintJob.getId(), BAD_APP_ID, mUserId);
271 }
272 }, SecurityException.class);
273
274 // Cannot test bad user Id as these tests are allowed to call across users
275 }
276
277 /**
278 * test IPrintManager.getPrintJobInfos
279 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700280 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800281 public void testGetPrintJobInfos() throws Exception {
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800282 startPrinting();
283
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800284 List<PrintJobInfo> infos = mIPrintManager.getPrintJobInfos(mAppId, mUserId);
285
286 boolean foundPrintJob = false;
287 for (PrintJobInfo info : infos) {
288 if (info.getId().equals(mGoodPrintJob.getId())) {
289 assertEquals(PrintJobInfo.STATE_CREATED, info.getState());
290 foundPrintJob = true;
291 }
292 }
293 assertTrue(foundPrintJob);
294
295 assertException(new Invokable() {
296 @Override
297 public void run() throws Exception {
298 mIPrintManager.getPrintJobInfos(BAD_APP_ID, mUserId);
299 }
300 }, SecurityException.class);
301
302 // Cannot test bad user Id as these tests are allowed to call across users
303 }
304
305 /**
306 * test IPrintManager.print
307 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700308 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800309 public void testPrint() throws Exception {
310 final String name = "dummy print job";
311
312 final IPrintDocumentAdapter adapter = new PrintManager
313 .PrintDocumentAdapterDelegate(getActivity(), createMockAdapter());
314
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800315 startPrinting();
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800316
317 assertException(new Invokable() {
318 @Override
319 public void run() throws Exception {
320 mIPrintManager.print(null, adapter, null, mGoodComponentName.getPackageName(),
321 mAppId, mUserId);
322 }
323 }, IllegalArgumentException.class);
324
325 assertException(new Invokable() {
326 @Override
327 public void run() throws Exception {
328 mIPrintManager.print(name, null, null, mGoodComponentName.getPackageName(),
329 mAppId, mUserId);
330 }
331 }, NullPointerException.class);
332
333 assertException(new Invokable() {
334 @Override
335 public void run() throws Exception {
336 mIPrintManager.print(name, adapter, null, null, mAppId, mUserId);
337 }
338 }, IllegalArgumentException.class);
339
340 assertException(new Invokable() {
341 @Override
342 public void run() throws Exception {
343 mIPrintManager.print(name, adapter, null, mBadComponentName.getPackageName(),
344 mAppId, mUserId);
345 }
346 }, IllegalArgumentException.class);
347
348 assertException(new Invokable() {
349 @Override
350 public void run() throws Exception {
351 mIPrintManager.print(name, adapter, null, mGoodComponentName.getPackageName(),
352 BAD_APP_ID, mUserId);
353 }
354 }, SecurityException.class);
355
356 // Cannot test bad user Id as these tests are allowed to call across users
357 }
358
359 /**
360 * test IPrintManager.cancelPrintJob
361 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700362 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800363 public void testCancelPrintJob() throws Exception {
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800364 startPrinting();
365
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800366 // Invalid print jobs IDs do not produce an exception
367 mIPrintManager.cancelPrintJob(mBadPrintJobId, mAppId, mUserId);
368 mIPrintManager.cancelPrintJob(null, mAppId, mUserId);
369
370 assertException(new Invokable() {
371 @Override
372 public void run() throws Exception {
373 mIPrintManager.cancelPrintJob(mGoodPrintJob.getId(), BAD_APP_ID, mUserId);
374 }
375 }, SecurityException.class);
376
377 // Cannot test bad user Id as these tests are allowed to call across users
378
379 // Must be last as otherwise mGoodPrintJob will not be good anymore
380 mIPrintManager.cancelPrintJob(mGoodPrintJob.getId(), mAppId, mUserId);
381 }
382
383 /**
384 * test IPrintManager.restartPrintJob
385 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700386 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800387 public void testRestartPrintJob() throws Exception {
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800388 startPrinting();
389
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800390 mIPrintManager.restartPrintJob(mGoodPrintJob.getId(), mAppId, mUserId);
391
392 // Invalid print jobs IDs do not produce an exception
393 mIPrintManager.restartPrintJob(mBadPrintJobId, mAppId, mUserId);
394 mIPrintManager.restartPrintJob(null, mAppId, mUserId);
395
396 assertException(new Invokable() {
397 @Override
398 public void run() throws Exception {
399 mIPrintManager.restartPrintJob(mGoodPrintJob.getId(), BAD_APP_ID, mUserId);
400 }
401 }, SecurityException.class);
402
403 // Cannot test bad user Id as these tests are allowed to call across users
404 }
405
406 /**
407 * test IPrintManager.addPrintJobStateChangeListener
408 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700409 @MediumTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800410 public void testAddPrintJobStateChangeListener() throws Exception {
411 final IPrintJobStateChangeListener listener = createMockIPrintJobStateChangeListener();
412
413 mIPrintManager.addPrintJobStateChangeListener(listener, mAppId, mUserId);
414
415 assertException(new Invokable() {
416 @Override
417 public void run() throws Exception {
418 mIPrintManager.addPrintJobStateChangeListener(null, mAppId, mUserId);
419 }
420 }, NullPointerException.class);
421
422 assertException(new Invokable() {
423 @Override
424 public void run() throws Exception {
425 mIPrintManager.addPrintJobStateChangeListener(listener, BAD_APP_ID, mUserId);
426 }
427 }, SecurityException.class);
428
429 // Cannot test bad user Id as these tests are allowed to call across users
430 }
431
432 /**
433 * test IPrintManager.removePrintJobStateChangeListener
434 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700435 @MediumTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800436 public void testRemovePrintJobStateChangeListener() throws Exception {
437 final IPrintJobStateChangeListener listener = createMockIPrintJobStateChangeListener();
438
439 mIPrintManager.addPrintJobStateChangeListener(listener, mAppId, mUserId);
440 mIPrintManager.removePrintJobStateChangeListener(listener, mUserId);
441
442 // Removing unknown listeners is a no-op
443 mIPrintManager.removePrintJobStateChangeListener(listener, mUserId);
444
445 mIPrintManager.addPrintJobStateChangeListener(listener, mAppId, mUserId);
446 assertException(new Invokable() {
447 @Override
448 public void run() throws Exception {
449 mIPrintManager.removePrintJobStateChangeListener(null, mUserId);
450 }
451 }, NullPointerException.class);
452
453 // Cannot test bad user Id as these tests are allowed to call across users
454 }
455
456 /**
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800457 * test IPrintManager.addPrintServicesChangeListener
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800458 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700459 @MediumTest
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800460 public void testAddPrintServicesChangeListener() throws Exception {
461 final IPrintServicesChangeListener listener = createMockIPrintServicesChangeListener();
462
463 mIPrintManager.addPrintServicesChangeListener(listener, mUserId);
464
465 assertException(new Invokable() {
466 @Override
467 public void run() throws Exception {
468 mIPrintManager.addPrintServicesChangeListener(null, mUserId);
469 }
470 }, NullPointerException.class);
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800471
472 // Cannot test bad user Id as these tests are allowed to call across users
473 }
474
475 /**
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800476 * test IPrintManager.removePrintServicesChangeListener
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800477 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700478 @MediumTest
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800479 public void testRemovePrintServicesChangeListener() throws Exception {
480 final IPrintServicesChangeListener listener = createMockIPrintServicesChangeListener();
481
482 mIPrintManager.addPrintServicesChangeListener(listener, mUserId);
483 mIPrintManager.removePrintServicesChangeListener(listener, mUserId);
484
485 // Removing unknown listeners is a no-op
486 mIPrintManager.removePrintServicesChangeListener(listener, mUserId);
487
488 mIPrintManager.addPrintServicesChangeListener(listener, mUserId);
489 assertException(new Invokable() {
490 @Override
491 public void run() throws Exception {
492 mIPrintManager.removePrintServicesChangeListener(null, mUserId);
493 }
494 }, NullPointerException.class);
495
496 // Cannot test bad user Id as these tests are allowed to call across users
497 }
498
499 /**
500 * test IPrintManager.getPrintServices
501 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700502 @MediumTest
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800503 public void testGetPrintServices() throws Exception {
504 List<PrintServiceInfo> printServices = mIPrintManager.getPrintServices(
505 PrintManager.ALL_SERVICES, mUserId);
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800506 assertTrue(printServices.size() >= 2);
507
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800508 printServices = mIPrintManager.getPrintServices(0, mUserId);
509 assertEquals(printServices, null);
510
511 assertException(new Invokable() {
512 @Override
513 public void run() throws Exception {
514 mIPrintManager.getPrintServices(~PrintManager.ALL_SERVICES, mUserId);
515 }
516 }, IllegalArgumentException.class);
517
518 // Cannot test bad user Id as these tests are allowed to call across users
519 }
520
521 /**
522 * test IPrintManager.setPrintServiceEnabled
523 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700524 @MediumTest
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800525 public void testSetPrintServiceEnabled() throws Exception {
526 final ComponentName printService = mIPrintManager.getPrintServices(
527 PrintManager.ALL_SERVICES, mUserId).get(0).getComponentName();
528
529 assertException(new Invokable() {
530 @Override
531 public void run() throws Exception {
532 mIPrintManager.setPrintServiceEnabled(printService, false, mUserId);
533 }
534 }, SecurityException.class);
535
536 assertException(new Invokable() {
537 @Override
538 public void run() throws Exception {
539 mIPrintManager.setPrintServiceEnabled(printService, true, mUserId);
540 }
541 }, SecurityException.class);
542
543 assertException(new Invokable() {
544 @Override
545 public void run() throws Exception {
546 mIPrintManager.setPrintServiceEnabled(new ComponentName("bad", "name"), true,
547 mUserId);
548 }
549 }, SecurityException.class);
550
551 assertException(new Invokable() {
552 @Override
553 public void run() throws Exception {
554 mIPrintManager.setPrintServiceEnabled(null, true, mUserId);
555 }
556 }, SecurityException.class);
557
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800558 // Cannot test bad user Id as these tests are allowed to call across users
559 }
560
561 /**
562 * test IPrintManager.createPrinterDiscoverySession
563 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700564 @MediumTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800565 public void testCreatePrinterDiscoverySession() throws Exception {
566 final IPrinterDiscoveryObserver listener = createMockIPrinterDiscoveryObserver();
567
568 mIPrintManager.createPrinterDiscoverySession(listener, mUserId);
569
570 try {
571 assertException(new Invokable() {
572 @Override
573 public void run() throws Exception {
574 mIPrintManager.createPrinterDiscoverySession(null, mUserId);
575 }
576 }, NullPointerException.class);
577
578 // Cannot test bad user Id as these tests are allowed to call across users
579 } finally {
580 // Remove discovery session so that the next test create a new one. Usually a leaked
581 // session is removed on the next call from the print service. But in this case we want
582 // to force a new call to onPrinterDiscoverySessionStart in the next test.
583 mIPrintManager.destroyPrinterDiscoverySession(listener, mUserId);
584 }
585 }
586
587 /**
588 * test IPrintManager.startPrinterDiscovery
589 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700590 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800591 public void testStartPrinterDiscovery() throws Exception {
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800592 startPrinting();
593
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800594 final IPrinterDiscoveryObserver listener = createMockIPrinterDiscoveryObserver();
595 final List<PrinterId> goodPrinters = new ArrayList<>();
596 goodPrinters.add(mGoodPrinterId);
597
598 final List<PrinterId> badPrinters = new ArrayList<>();
599 badPrinters.add(mBadPrinterId);
600
601 final List<PrinterId> emptyPrinters = new ArrayList<>();
602
603 final List<PrinterId> nullPrinters = new ArrayList<>();
604 nullPrinters.add(null);
605
606 mIPrintManager.startPrinterDiscovery(listener, goodPrinters, mUserId);
607
608 // Bad or no printers do no cause exceptions
609 mIPrintManager.startPrinterDiscovery(listener, badPrinters, mUserId);
610 mIPrintManager.startPrinterDiscovery(listener, emptyPrinters, mUserId);
611 mIPrintManager.startPrinterDiscovery(listener, null, mUserId);
612
613 assertException(new Invokable() {
614 @Override
615 public void run() throws Exception {
616 mIPrintManager.startPrinterDiscovery(listener, nullPrinters, mUserId);
617 }
618 }, NullPointerException.class);
619
620 assertException(new Invokable() {
621 @Override
622 public void run() throws Exception {
623 mIPrintManager.startPrinterDiscovery(null, goodPrinters, mUserId);
624 }
625 }, NullPointerException.class);
626
627 // Cannot test bad user Id as these tests are allowed to call across users
628 }
629
630 /**
631 * test IPrintManager.stopPrinterDiscovery
632 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700633 @MediumTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800634 public void testStopPrinterDiscovery() throws Exception {
635 final IPrinterDiscoveryObserver listener = createMockIPrinterDiscoveryObserver();
636
637 mIPrintManager.startPrinterDiscovery(listener, null, mUserId);
638 mIPrintManager.stopPrinterDiscovery(listener, mUserId);
639
640 // Stopping an already stopped session is a no-op
641 mIPrintManager.stopPrinterDiscovery(listener, mUserId);
642
643 mIPrintManager.startPrinterDiscovery(listener, null, mUserId);
644 assertException(new Invokable() {
645 @Override
646 public void run() throws Exception {
647 mIPrintManager.stopPrinterDiscovery(null, mUserId);
648 }
649 }, NullPointerException.class);
650
651 // Cannot test bad user Id as these tests are allowed to call across users
652 }
653
654 /**
655 * test IPrintManager.validatePrinters
656 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700657 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800658 public void testValidatePrinters() throws Exception {
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800659 startPrinting();
660
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800661 final List<PrinterId> goodPrinters = new ArrayList<>();
662 goodPrinters.add(mGoodPrinterId);
663
664 final List<PrinterId> badPrinters = new ArrayList<>();
665 badPrinters.add(mBadPrinterId);
666
667 final List<PrinterId> emptyPrinters = new ArrayList<>();
668
669 final List<PrinterId> nullPrinters = new ArrayList<>();
670 nullPrinters.add(null);
671
672 mIPrintManager.validatePrinters(goodPrinters, mUserId);
673
674 // Bad or empty list of printers do no cause exceptions
675 mIPrintManager.validatePrinters(badPrinters, mUserId);
676 mIPrintManager.validatePrinters(emptyPrinters, mUserId);
677
678 assertException(new Invokable() {
679 @Override
680 public void run() throws Exception {
681 mIPrintManager.validatePrinters(null, mUserId);
682 }
683 }, NullPointerException.class);
684
685 assertException(new Invokable() {
686 @Override
687 public void run() throws Exception {
688 mIPrintManager.validatePrinters(nullPrinters, mUserId);
689 }
690 }, NullPointerException.class);
691
692 // Cannot test bad user Id as these tests are allowed to call across users
693 }
694
695 /**
696 * test IPrintManager.startPrinterStateTracking
697 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700698 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800699 public void testStartPrinterStateTracking() throws Exception {
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800700 startPrinting();
701
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800702 mIPrintManager.startPrinterStateTracking(mGoodPrinterId, mUserId);
703
704 // Bad printers do no cause exceptions
705 mIPrintManager.startPrinterStateTracking(mBadPrinterId, mUserId);
706
707 assertException(new Invokable() {
708 @Override
709 public void run() throws Exception {
710 mIPrintManager.startPrinterStateTracking(null, mUserId);
711 }
712 }, NullPointerException.class);
713
714 // Cannot test bad user Id as these tests are allowed to call across users
715 }
716
717 /**
718 * test IPrintManager.getCustomPrinterIcon
719 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700720 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800721 public void testGetCustomPrinterIcon() throws Exception {
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800722 startPrinting();
723
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800724 mIPrintManager.getCustomPrinterIcon(mGoodPrinterId, mUserId);
725
726 // Bad printers do no cause exceptions
727 mIPrintManager.getCustomPrinterIcon(mBadPrinterId, mUserId);
728
729 assertException(new Invokable() {
730 @Override
731 public void run() throws Exception {
732 mIPrintManager.getCustomPrinterIcon(null, mUserId);
733 }
734 }, NullPointerException.class);
735
736 // Cannot test bad user Id as these tests are allowed to call across users
737 }
738
739 /**
740 * test IPrintManager.stopPrinterStateTracking
741 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700742 @LargeTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800743 public void testStopPrinterStateTracking() throws Exception {
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800744 startPrinting();
745
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800746 mIPrintManager.startPrinterStateTracking(mGoodPrinterId, mUserId);
747 mIPrintManager.stopPrinterStateTracking(mGoodPrinterId, mUserId);
748
749 // Stop to track a non-tracked printer is a no-op
750 mIPrintManager.stopPrinterStateTracking(mGoodPrinterId, mUserId);
751
752 // Bad printers do no cause exceptions
753 mIPrintManager.startPrinterStateTracking(mBadPrinterId, mUserId);
754 mIPrintManager.stopPrinterStateTracking(mBadPrinterId, mUserId);
755
756 assertException(new Invokable() {
757 @Override
758 public void run() throws Exception {
759 mIPrintManager.stopPrinterStateTracking(null, mUserId);
760 }
761 }, NullPointerException.class);
762
763 // Cannot test bad user Id as these tests are allowed to call across users
764 }
765
766 /**
767 * test IPrintManager.destroyPrinterDiscoverySession
768 */
Philip P. Moltmann01527ee2016-03-16 16:52:24 -0700769 @MediumTest
Philip P. Moltmann76d7e3e2016-01-15 13:22:13 -0800770 public void testDestroyPrinterDiscoverySession() throws Exception {
771 final IPrinterDiscoveryObserver listener = createMockIPrinterDiscoveryObserver();
772
773 mIPrintManager.createPrinterDiscoverySession(listener, mUserId);
774 mIPrintManager.destroyPrinterDiscoverySession(listener, mUserId);
775
776 // Destroying already destroyed session is a no-op
777 mIPrintManager.destroyPrinterDiscoverySession(listener, mUserId);
778
779 assertException(new Invokable() {
780 @Override
781 public void run() throws Exception {
782 mIPrintManager.destroyPrinterDiscoverySession(null, mUserId);
783 }
784 }, NullPointerException.class);
785
786 // Cannot test bad user Id as these tests are allowed to call across users
787 }
788}