1# Copyright 2018 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import os
7import time
8
9from autotest_lib.client.bin import test, utils
10from autotest_lib.client.common_lib.cros import chrome
11from autotest_lib.client.cros.chameleon import chameleon
12from autotest_lib.client.common_lib import ui_utils_helpers
13
14_CHECK_TIMEOUT = 20
15_PRINTER_NAME = "HP OfficeJet g55"
16_PRINTING_COMPLETE_NOTIF = "Printing complete"
17_PRINTING_NOTIF = "Printing"
18_STEPS_BETWEEN_CONTROLS = 4
19_USB_PRINTER_CONNECTED_NOTIF = "USB printer connected"
20
21_SHORT_WAIT = 2
22
23class platform_PrintJob(test.test):
24    """
25    E2E test - Chrome is brought up, local pdf file open, print dialog open,
26    chameleon gadget driver printer selected and print job executed. The test
27    verifies the print job finished successfully.
28    """
29    version = 1
30
31    def cleanup(self):
32        if hasattr(self, 'browser'):
33            self.browser.close()
34        if self.printer_capture_started:
35            self.usb_printer.StopCapturingPrinterData()
36        if self.printer_connected:
37            self.usb_printer.Unplug()
38
39    def check_notification(self, notification):
40        """Polls for successful print job notification"""
41
42        def find_notification(title=None):
43            notifications = self.cr.get_visible_notifications()
44            if notifications is None:
45                return False
46            for n in notifications:
47                if title in n['title']:
48                    return True
49            return False
50
51        utils.poll_for_condition(
52                condition=lambda: find_notification(notification),
53                desc='Notification %s NOT found' % notification,
54                timeout=_CHECK_TIMEOUT, sleep_interval=_SHORT_WAIT)
55
56    def navigate_to_pdf(self):
57        """Navigate to the pdf page to print"""
58        self.cr.browser.platform.SetHTTPServerDirectories(self.bindir)
59        tab = self.cr.browser.tabs.New()
60        pdf_path = os.path.join(self.bindir, 'to_print.pdf')
61        tab.Navigate(self.cr.browser.platform.http_server.UrlOf(pdf_path))
62        tab.WaitForDocumentReadyStateToBeInteractiveOrBetter(
63                timeout=_CHECK_TIMEOUT);
64        time.sleep(_SHORT_WAIT)
65
66    def run_once(self, host, args):
67        """Run the test."""
68        # Set these to know if the usb_printer needs to be handled post test.
69        self.printer_capture_started = False
70        self.printer_connected = False
71
72        # Make chameleon host known to the DUT host crbug.com/862646
73        chameleon_args = 'chameleon_host=' + host.hostname + '-chameleon'
74        args.append(chameleon_args)
75
76        chameleon_board = chameleon.create_chameleon_board(host.hostname, args)
77        chameleon_board.setup_and_reset(self.outputdir)
78        self.usb_printer = chameleon_board.get_usb_printer()
79        self.usb_printer.SetPrinterModel(1008, 17, _PRINTER_NAME)
80
81        with chrome.Chrome(autotest_ext=True,
82                           init_network_controller=True) as self.cr:
83            self.usb_printer.Plug()
84            self.printer_connected = True
85            self.check_notification(_USB_PRINTER_CONNECTED_NOTIF)
86            logging.info('Chameleon printer connected!')
87            self.navigate_to_pdf()
88            time.sleep(_SHORT_WAIT)
89            logging.info('PDF file opened in browser!')
90            self.ui_helper = ui_utils_helpers.UIPrinterHelper(chrome=self.cr)
91            self.ui_helper.print_to_custom_printer("Chameleon " + _PRINTER_NAME)
92            self.usb_printer.StartCapturingPrinterData()
93            self.printer_capture_started = True
94            self.check_notification(_PRINTING_NOTIF)
95            self.check_notification(_PRINTING_COMPLETE_NOTIF)
96