tor-browser

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

test_view_gecko_profiler.py (3111B)


      1 #!/usr/bin/env python
      2 
      3 import os
      4 import re
      5 import shutil
      6 import tempfile
      7 import threading
      8 import time
      9 import unittest
     10 from unittest import mock
     11 from urllib.parse import unquote
     12 
     13 import mozunit
     14 import requests
     15 from mozgeckoprofiler import view_gecko_profile
     16 
     17 
     18 def access_profiler_link(file_url, response):
     19    """Attempts to access the profile in a loop for 5 seconds.
     20 
     21    This is run from a separate thread.
     22    """
     23    timeout = 5  # seconds
     24    start = time.time()
     25 
     26    while time.time() - start < timeout:
     27        # Poll the server to try and get a response.
     28        result = requests.get(url=file_url)
     29        if result.ok:
     30            # Return the text back in a list.
     31            response[0] = result.text
     32            return
     33        time.sleep(0.1)
     34 
     35    response[0] = "Accessing the profiler link timed out after %s seconds" % timeout
     36 
     37 
     38 class TestViewGeckoProfile(unittest.TestCase):
     39    """Tests the opening local profiles in the Firefox Profiler."""
     40 
     41    def setUp(self):
     42        self.firefox_profiler_url = None
     43        self.thread = None
     44        self.response = [None]
     45 
     46    def test_view_gecko_profile(self):
     47        # Create a temporary fake performance profile.
     48        temp_dir = tempfile.mkdtemp()
     49        profile_path = os.path.join(temp_dir, "fakeprofile.json")
     50        with open(profile_path, "w") as f:
     51            f.write("FAKE_PROFILE")
     52 
     53        # Mock the open_new_tab function so that we know when the view_gecko_profile
     54        # function has done all of its work, and we can assert ressult of the
     55        # user behavior.
     56        def mocked_open_new_tab(firefox_profiler_url):
     57            self.firefox_profiler_url = firefox_profiler_url
     58            encoded_file_url = firefox_profiler_url.split("/")[-1]
     59            decoded_file_url = unquote(encoded_file_url)
     60            # Extract the actual file from the path.
     61            self.thread = threading.Thread(
     62                target=access_profiler_link, args=(decoded_file_url, self.response)
     63            )
     64            print("firefox_profiler_url %s" % firefox_profiler_url)
     65            print("encoded_file_url %s" % encoded_file_url)
     66            print("decoded_file_url %s" % decoded_file_url)
     67            self.thread.start()
     68 
     69        with mock.patch("webbrowser.open_new_tab", new=mocked_open_new_tab):
     70            # Run the test
     71            view_gecko_profile(profile_path)
     72 
     73        self.thread.join()
     74 
     75        # Compare the URLs, but replace the PORT value supplied, as that is dynamic.
     76        expected_url = (
     77            "https://profiler.firefox.com/from-url/"
     78            "http%3A%2F%2F127.0.0.1%3A{PORT}%2Ffakeprofile.json"
     79        )
     80        actual_url = re.sub(r"%3A\d+%2F", "%3A{PORT}%2F", self.firefox_profiler_url)
     81 
     82        self.assertEqual(
     83            actual_url,
     84            expected_url,
     85            "The URL generated was correct for the Firefox Profiler.",
     86        )
     87        self.assertEqual(
     88            self.response[0],
     89            "FAKE_PROFILE",
     90            "The response from the serve provided the profile contents.",
     91        )
     92 
     93        shutil.rmtree(temp_dir)
     94 
     95 
     96 if __name__ == "__main__":
     97    mozunit.main()