tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_chrome_screenshot.py (4018B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import os
      6 import sys
      7 
      8 from marionette_driver import By
      9 from marionette_driver.errors import NoSuchWindowException
     10 from marionette_harness import WindowManagerMixin
     11 
     12 # add this directory to the path
     13 sys.path.append(os.path.dirname(__file__))
     14 
     15 from chrome_handler_mixin import ChromeHandlerMixin
     16 from test_screenshot import ScreenCaptureTestCase
     17 
     18 
     19 class TestScreenCaptureChrome(
     20    ChromeHandlerMixin, WindowManagerMixin, ScreenCaptureTestCase
     21 ):
     22    def setUp(self):
     23        super(TestScreenCaptureChrome, self).setUp()
     24        self.marionette.set_context("chrome")
     25 
     26    def tearDown(self):
     27        self.close_all_windows()
     28        super(TestScreenCaptureChrome, self).tearDown()
     29 
     30    @property
     31    def window_dimensions(self):
     32        return tuple(
     33            self.marionette.execute_script(
     34                """
     35            let el = document.documentElement;
     36            let rect = el.getBoundingClientRect();
     37            return [rect.width, rect.height];
     38            """
     39            )
     40        )
     41 
     42    def open_dialog(self):
     43        return self.open_chrome_window(self.chrome_base_url + "test_dialog.xhtml")
     44 
     45    def test_capture_different_context(self):
     46        """Check that screenshots in content and chrome are different."""
     47        with self.marionette.using_context("content"):
     48            screenshot_content = self.marionette.screenshot()
     49        screenshot_chrome = self.marionette.screenshot()
     50        self.assertNotEqual(screenshot_content, screenshot_chrome)
     51 
     52    def test_capture_element(self):
     53        dialog = self.open_dialog()
     54        self.marionette.switch_to_window(dialog)
     55 
     56        # Ensure we only capture the element
     57        el = self.marionette.find_element(By.ID, "test-list")
     58        screenshot_element = self.marionette.screenshot(element=el)
     59        self.assertEqual(
     60            self.scale(self.get_element_dimensions(el)),
     61            self.get_image_dimensions(screenshot_element),
     62        )
     63 
     64        # Ensure we do not capture the full window
     65        screenshot_dialog = self.marionette.screenshot()
     66        self.assertNotEqual(screenshot_dialog, screenshot_element)
     67 
     68        self.marionette.close_chrome_window()
     69        self.marionette.switch_to_window(self.start_window)
     70 
     71    def test_capture_full_area(self):
     72        dialog = self.open_dialog()
     73        self.marionette.switch_to_window(dialog)
     74 
     75        root_dimensions = self.scale(self.get_element_dimensions(self.document_element))
     76 
     77        # self.marionette.set_window_rect(width=100, height=100)
     78        # A full capture is not the outer dimensions of the window,
     79        # but instead the bounding box of the window's root node (documentElement).
     80        screenshot_full = self.marionette.screenshot()
     81        screenshot_root = self.marionette.screenshot(element=self.document_element)
     82 
     83        self.marionette.close_chrome_window()
     84        self.marionette.switch_to_window(self.start_window)
     85 
     86        self.assert_png(screenshot_full)
     87        self.assert_png(screenshot_root)
     88        self.assertEqual(root_dimensions, self.get_image_dimensions(screenshot_full))
     89        self.assertEqual(screenshot_root, screenshot_full)
     90 
     91    def test_capture_window_already_closed(self):
     92        dialog = self.open_dialog()
     93        self.marionette.switch_to_window(dialog)
     94        self.marionette.close_chrome_window()
     95 
     96        self.assertRaises(NoSuchWindowException, self.marionette.screenshot)
     97        self.marionette.switch_to_window(self.start_window)
     98 
     99    def test_formats(self):
    100        dialog = self.open_dialog()
    101        self.marionette.switch_to_window(dialog)
    102 
    103        self.assert_formats()
    104 
    105        self.marionette.close_chrome_window()
    106        self.marionette.switch_to_window(self.start_window)
    107 
    108    def test_format_unknown(self):
    109        with self.assertRaises(ValueError):
    110            self.marionette.screenshot(format="cheese")