tor-browser

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

ffx_emulator_unittests.py (4093B)


      1 #!/usr/bin/env vpython3
      2 # Copyright 2023 The Chromium Authors
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 """File for testing ffx_emulator.py."""
      6 
      7 import argparse
      8 import unittest
      9 import unittest.mock as mock
     10 
     11 from ffx_emulator import FfxEmulator
     12 
     13 
     14 class FfxEmulatorTest(unittest.TestCase):
     15    """Unittests for ffx_emulator.py"""
     16    def test_use_fixed_node_name(self) -> None:
     17        """FfxEmulator should use a fixed node name."""
     18        # Allowing the test case to access FfxEmulator._node_name directly.
     19        # pylint: disable=protected-access
     20        self.assertEqual(
     21            FfxEmulator(
     22                argparse.Namespace(
     23                    **{
     24                        'product': None,
     25                        'enable_graphics': False,
     26                        'hardware_gpu': False,
     27                        'logs_dir': '.',
     28                        'with_network': False,
     29                        'everlasting': True,
     30                        'device_spec': ''
     31                    }))._node_name, 'fuchsia-everlasting-emulator')
     32 
     33    def test_use_random_node_name(self) -> None:
     34        """FfxEmulator should not use a fixed node name."""
     35        # Allowing the test case to access FfxEmulator._node_name directly.
     36        # pylint: disable=protected-access
     37        self.assertNotEqual(
     38            FfxEmulator(
     39                argparse.Namespace(
     40                    **{
     41                        'product': None,
     42                        'enable_graphics': False,
     43                        'hardware_gpu': False,
     44                        'logs_dir': '.',
     45                        'with_network': False,
     46                        'everlasting': False,
     47                        'device_spec': ''
     48                    }))._node_name, 'fuchsia-everlasting-emulator')
     49 
     50    @mock.patch('ffx_emulator.run_ffx_command')
     51    def test_use_none_device_spec(self, mock_ffx) -> None:
     52        """FfxEmulator should use the default device spec if spec is None."""
     53        FfxEmulator(
     54            argparse.Namespace(
     55                **{
     56                    'product': None,
     57                    'enable_graphics': False,
     58                    'hardware_gpu': False,
     59                    'logs_dir': '.',
     60                    'with_network': False,
     61                    'everlasting': False,
     62                    'device_spec': None
     63                })).__enter__()
     64        self.assertIn(' '.join(['--net', 'user']),
     65                      ' '.join(mock_ffx.call_args.kwargs['cmd']))
     66        self.assertNotIn('--device', mock_ffx.call_args.kwargs['cmd'])
     67 
     68    @mock.patch('ffx_emulator.run_ffx_command')
     69    def test_use_empty_device_spec(self, mock_ffx) -> None:
     70        """FfxEmulator should use the default device spec if spec is empty."""
     71        FfxEmulator(
     72            argparse.Namespace(
     73                **{
     74                    'product': None,
     75                    'enable_graphics': False,
     76                    'hardware_gpu': False,
     77                    'logs_dir': '.',
     78                    'with_network': False,
     79                    'everlasting': False,
     80                    'device_spec': ''
     81                })).__enter__()
     82        self.assertIn(' '.join(['--net', 'user']),
     83                      ' '.join(mock_ffx.call_args.kwargs['cmd']))
     84        self.assertNotIn('--device', mock_ffx.call_args.kwargs['cmd'])
     85 
     86    @mock.patch('ffx_emulator.run_ffx_command')
     87    def test_use_large_device_spec(self, mock_ffx) -> None:
     88        """FfxEmulator should use large device spec."""
     89        FfxEmulator(
     90            argparse.Namespace(
     91                **{
     92                    'product': None,
     93                    'enable_graphics': False,
     94                    'hardware_gpu': False,
     95                    'logs_dir': '.',
     96                    'with_network': False,
     97                    'everlasting': False,
     98                    'device_spec': 'large'
     99                })).__enter__()
    100        self.assertIn(' '.join(['--device', 'large']),
    101                      ' '.join(mock_ffx.call_args.kwargs['cmd']))
    102 
    103 
    104 if __name__ == '__main__':
    105    unittest.main()