def testGetRequiredLibrariesForVTuneProfile(self):
        vtune_db_output = os.path.join(util.GetUnittestDataDir(),
                                       'sample_vtune_db_output')
        with open(vtune_db_output, 'rb') as f:
            vtune_db_output = pickle.load(f)

        mock_cursor = simple_mock.MockObject()
        mock_cursor.ExpectCall('execute').WithArgs(
            simple_mock.DONT_CARE).WillReturn(vtune_db_output)

        mock_conn = simple_mock.MockObject()
        mock_conn.ExpectCall('cursor').WillReturn(mock_cursor)
        mock_conn.ExpectCall('close')

        mock_sqlite3 = simple_mock.MockObject()
        mock_sqlite3.ExpectCall('connect').WithArgs(
            simple_mock.DONT_CARE).WillReturn(mock_conn)

        real_sqlite3 = android_profiling_helper.sqlite3
        android_profiling_helper.sqlite3 = mock_sqlite3
        try:
            libs = android_profiling_helper.GetRequiredLibrariesForVTuneProfile(
                'foo')
            self.assertEqual(
                libs,
                set([
                    '/data/app-lib/com.google.android.apps.chrome-1/libchrome.2019.0.so',
                    '/system/lib/libdvm.so', '/system/lib/libc.so',
                    '/system/lib/libm.so'
                ]))
        finally:
            android_profiling_helper.sqlite3 = real_sqlite3
    def testGetRequiredLibrariesForPerfProfile(self):
        perf_output = os.path.join(util.GetUnittestDataDir(),
                                   'sample_perf_report_output.txt')
        with open(perf_output) as f:
            perf_output = f.read()

        mock_popen = simple_mock.MockObject()
        mock_popen.ExpectCall('communicate').WillReturn([None, perf_output])

        mock_subprocess = simple_mock.MockObject()
        mock_subprocess.ExpectCall('Popen').WithArgs(
            simple_mock.DONT_CARE).WillReturn(mock_popen)
        mock_subprocess.SetAttribute('PIPE', simple_mock.MockObject())

        real_subprocess = android_profiling_helper.subprocess
        android_profiling_helper.subprocess = mock_subprocess
        try:
            libs = android_profiling_helper.GetRequiredLibrariesForPerfProfile(
                'foo')
            self.assertEqual(
                libs,
                set([
                    '/data/app-lib/com.google.android.apps.chrome-2/libchrome.2016.0.so',
                    '/system/lib/libart.so', '/system/lib/libc.so',
                    '/system/lib/libm.so'
                ]))
        finally:
            android_profiling_helper.subprocess = real_subprocess
示例#3
0
    def testSubObjectMismatch(self):
        mock = simple_mock.MockObject()
        mock.bar = simple_mock.MockObject(mock)

        mock.ExpectCall('foo').WithArgs(_, 4)
        mock.bar.ExpectCall('baz')

        self.assertRaises(Exception, lambda: mock.bar.baz())  # pylint: disable=unnecessary-lambda
示例#4
0
    def testSubObject(self):
        mock = simple_mock.MockObject()
        mock.bar = simple_mock.MockObject(mock)

        mock.ExpectCall('foo').WithArgs(_, 4)
        mock.bar.ExpectCall('baz')

        mock.foo(0, 4)
        mock.bar.baz()
 def _ExpectProcesses(self, args):
     mock_subprocess = simple_mock.MockObject()
     mock_subprocess.SetAttribute('PIPE', simple_mock.MockObject())
     for arg in args:
         mock_popen = simple_mock.MockObject()
         mock_popen.ExpectCall('communicate').WillReturn(arg[1][1:])
         mock_popen.ExpectCall('poll').WillReturn(arg[1][0])
         mock_subprocess.ExpectCall('Popen').WithArgs(
             arg[0]).WillReturn(mock_popen)
     trybot_browser_finder.subprocess = mock_subprocess
    def testSysfsReadFailed(self):
        mock_power_monitor = simple_mock.MockObject()
        mock_power_monitor.ExpectCall('CanMonitorPower').WillReturn(False)
        mock_device_utils = simple_mock.MockObject()
        mock_device_utils.ExpectCall('ReadFile', _).WillReturn('')

        monitor = android_temperature_monitor.AndroidTemperatureMonitor(
            mock_power_monitor, mock_device_utils)
        self.assertTrue(monitor.CanMonitorPower())
        monitor.StartMonitoringPower(None)
        measurements = monitor.StopMonitoringPower()
        self.assertTrue('identifier' in measurements)
        self.assertTrue('component_utilization' not in measurements)
示例#7
0
    def _RunMetricOnJson(self, json_to_return, stats_metric):
        tab = simple_mock.MockObject()
        page = simple_mock.MockObject()

        stats_metric.Start(page, tab)

        tab.ExpectCall('EvaluateJavaScript2',
                       simple_mock.DONT_CARE).WillReturn(json_to_return)
        stats_metric.Stop(page, tab)

        page.url = simple_mock.MockObject()
        results = FakeResults(page)
        stats_metric.AddResults(tab, results)
        return results
    def testSysfsReadFailedCanMonitor(self):
        mock_device_utils = simple_mock.MockObject()
        mock_device_utils.ExpectCall('ReadFile', _).WillReturn(None)

        monitor = android_temperature_monitor.AndroidTemperatureMonitor(
            mock_device_utils)
        self.assertFalse(monitor.CanMonitorPower())
 def test_find_all_browser_types_non_trybot_browser(self):
     finder_options = browser_options.BrowserFinderOptions(
         browser_type='release')
     trybot_browser_finder.urllib2 = simple_mock.MockObject()
     self.assertEquals(
         [],
         # pylint: disable=W0212
         sorted(trybot_browser_finder.FindAllBrowserTypes(finder_options)))
    def _LoginUsingMock(self, backend, login_page_url, email_element_id,
                        password_element_id, form_element_id,
                        already_logged_in_js):  # pylint: disable=no-self-use
        del form_element_id  # Unused.
        del email_element_id  # Unused.
        del password_element_id  # Unused.
        tab = simple_mock.MockObject()
        ar = simple_mock.MockObject()

        config = {'username': '******', 'password': '******'}

        tab.ExpectCall('Navigate', login_page_url)
        tab.ExpectCall('EvaluateJavaScript',
                       already_logged_in_js).WillReturn(False)
        tab.ExpectCall('WaitForDocumentReadyStateToBeInteractiveOrBetter')

        ar.ExpectCall(
            'WaitForJavaScriptCondition',
            '(document.querySelector({{ form_id }}) !== null) || ({{ @code }})'
        )
        ar.ExpectCall('WaitForNavigate')

        def VerifyEmail(js):
            assert '{{ selector }}' in js
            assert '{{ username }}' in js

        tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyEmail)

        def VerifyPw(js):
            assert '{{ selector }}' in js
            assert '{{ password }}' in js

        tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifyPw)

        def VerifySubmit(js):
            assert '.submit' in js or '.click' in js

        tab.ExpectCall('ExecuteJavaScript', _).WhenCalled(VerifySubmit)

        # Checking for form still up.
        tab.ExpectCall('EvaluateJavaScript', _).WillReturn(False)

        backend.LoginNeeded(tab, ar, config)
    def testNoAttmptToMonitorIfIncapable(self):
        mock_power_monitor = simple_mock.MockObject()
        mock_power_monitor.ExpectCall('CanMonitorPower').WillReturn(False)

        temperature_monitor = TemperatureMonitorForTesting(
            mock_power_monitor, 42.0)
        self.assertTrue(temperature_monitor.CanMonitorPower())
        temperature_monitor.StartMonitoringPower(None)
        power_results = temperature_monitor.StopMonitoringPower()
        self.assertTrue(
            temperature_monitor.PowerMeasurementsConsistent(power_results))
    def testSysfsReadFailed(self):
        mock_device_utils = simple_mock.MockObject()
        mock_device_utils.ExpectCall('ReadFile', _).WillReturn('24')
        mock_device_utils.ExpectCall('ReadFile', _).WillReturn(None)

        monitor = android_temperature_monitor.AndroidTemperatureMonitor(
            mock_device_utils)
        self.assertTrue(monitor.CanMonitorPower())
        monitor.StartMonitoringPower(None)
        measurements = monitor.StopMonitoringPower()
        self.assertTrue('identifier' in measurements)
        self.assertTrue('platform_info' not in measurements)
示例#13
0
    def testPerfProfiler(self):
        options = options_for_unittests.GetCopy()
        if not perf_profiler.PerfProfiler.is_supported(options.browser_type):
            logging.warning('PerfProfiler is not supported. Skipping test')
            return

        profile_file = os.path.join(util.GetUnittestDataDir(),
                                    'perf_report_output.txt')
        with open(profile_file) as f:
            perf_report_output = f.read()

        mock_popen = simple_mock.MockObject()
        mock_popen.ExpectCall('communicate').WillReturn([perf_report_output])

        mock_subprocess = simple_mock.MockObject()
        mock_subprocess.ExpectCall('Popen').WithArgs(
            simple_mock.DONT_CARE).WillReturn(mock_popen)
        mock_subprocess.SetAttribute('PIPE', simple_mock.MockObject())

        real_subprocess = perf_profiler.subprocess
        perf_profiler.subprocess = mock_subprocess
        try:
            self.assertEqual(
                perf_profiler.PerfProfiler.GetTopSamples(profile_file, 10), {
                    'v8::internal::StaticMarkingVisitor::MarkMapContents':
                    63615201,
                    'v8::internal::RelocIterator::next': 38271931,
                    'v8::internal::LAllocator::MeetConstraintsBetween':
                    42913933,
                    'v8::internal::FlexibleBodyVisitor::Visit': 31909537,
                    'v8::internal::LiveRange::CreateAssignedOperand': 42913933,
                    'void v8::internal::RelocInfo::Visit': 96878864,
                    'WebCore::HTMLTokenizer::nextToken': 48240439,
                    'v8::internal::Scanner::ScanIdentifierOrKeyword': 46054550,
                    'sk_memset32_SSE2': 45121317,
                    'v8::internal::HeapObject::Size': 39786862
                })
        finally:
            perf_profiler.subprocess = real_subprocess
示例#14
0
    def testOnCall(self):
        mock = simple_mock.MockObject()

        handler_called = []

        def Handler(arg0):
            assert arg0 == 7
            handler_called.append(True)

        mock.ExpectCall('baz', 7).WhenCalled(Handler)

        mock.baz(7)
        self.assertTrue(len(handler_called) > 0)
示例#15
0
    def testVTuneProfilerIsSupported(self):
        options = options_for_unittests.GetCopy()

        mock_subprocess = simple_mock.MockObject()
        mock_subprocess.ExpectCall('Popen').WithArgs(
            simple_mock.DONT_CARE).WillReturn(MockPopen(0))
        mock_subprocess.SetAttribute('PIPE', simple_mock.MockObject())
        mock_subprocess.SetAttribute('STDOUT', simple_mock.MockObject())

        real_subprocess = vtune_profiler.subprocess
        vtune_profiler.subprocess = mock_subprocess

        if options.browser_type.startswith('android'):
            # On Android we're querying if 'su' is available.
            mock_subprocess.ExpectCall('Popen').WithArgs(
                simple_mock.DONT_CARE).WillReturn(MockPopen(0, 'su', None))

        try:
            self.assertTrue(
                vtune_profiler.VTuneProfiler.is_supported(options.browser_type)
                or sys.platform != 'linux2'
                or options.browser_type.startswith('cros'))
        finally:
            vtune_profiler.subprocess = real_subprocess
    def testPowerMonitoringResultsWereUpdated(self):
        mock_power_monitor = simple_mock.MockObject()
        mock_power_monitor.ExpectCall('CanMonitorPower').WillReturn(True)
        fake_measurement = {'identifier': '123'}
        mock_power_monitor.ExpectCall('StartMonitoringPower', _)
        mock_power_monitor.ExpectCall('StopMonitoringPower').WillReturn(
            fake_measurement)

        temperature_monitor = TemperatureMonitorForTesting(
            mock_power_monitor, 24.0)
        self.assertTrue(temperature_monitor.CanMonitorPower())
        temperature_monitor.StartMonitoringPower(None)
        measurements = temperature_monitor.StopMonitoringPower()
        self.assertTrue(
            temperature_monitor.PowerMeasurementsConsistent(measurements))
        self.assertEqual('123', measurements['identifier'])
    def testPowerMonitoringResultsWereUpdated(self):
        mock_device_utils = simple_mock.MockObject()
        mock_device_utils.ExpectCall('ReadFile', _).WillReturn('0')
        mock_device_utils.ExpectCall('ReadFile', _).WillReturn('24')

        monitor = android_temperature_monitor.AndroidTemperatureMonitor(
            mock_device_utils)
        self.assertTrue(monitor.CanMonitorPower())
        monitor.StartMonitoringPower(None)
        measurements = monitor.StopMonitoringPower()
        expected_return = {
            'identifier': 'android_temperature_monitor',
            'platform_info': {
                'average_temperature_c': 24.0
            }
        }
        self.assertDictEqual(expected_return, measurements)
示例#18
0
    def SetUpCycler(self,
                    page_repeat=1,
                    pageset_repeat=10,
                    cold_load_percent=50,
                    report_speed_index=False,
                    setup_memory_module=False):
        cycler = page_cycler.PageCycler(page_repeat=page_repeat,
                                        pageset_repeat=pageset_repeat,
                                        cold_load_percent=cold_load_percent,
                                        report_speed_index=report_speed_index)
        options = browser_options.BrowserFinderOptions()
        options.browser_options.platform = FakePlatform()
        parser = options.CreateParser()
        story_runner.AddCommandLineArgs(parser)
        args = [
            '--page-repeat=%i' % page_repeat,
            '--pageset-repeat=%i' % pageset_repeat
        ]
        parser.parse_args(args)
        story_runner.ProcessCommandLineArgs(parser, options)
        cycler.CustomizeBrowserOptions(options.browser_options)

        if setup_memory_module:
            # Mock out memory metrics; the real ones require a real browser.
            mock_memory_metric = MockMemoryMetric()

            mock_memory_module = simple_mock.MockObject()
            mock_memory_module.ExpectCall('MemoryMetric').WithArgs(
                simple_mock.DONT_CARE).WillReturn(mock_memory_metric)

            real_memory_module = page_cycler.memory
            try:
                page_cycler.memory = mock_memory_module
                browser = FakeBrowser()
                cycler.WillStartBrowser(options.browser_options.platform)
                cycler.DidStartBrowser(browser)
            finally:
                page_cycler.memory = real_memory_module

        return cycler
示例#19
0
    def testBasic(self):
        mock = simple_mock.MockObject()
        mock.ExpectCall('foo')

        mock.foo()
示例#20
0
    def testReturn(self):
        mock = simple_mock.MockObject()
        mock.ExpectCall('foo').WillReturn(7)

        ret = mock.foo()
        self.assertEquals(ret, 7)
示例#21
0
    def testArgs(self):
        mock = simple_mock.MockObject()
        mock.ExpectCall('foo').WithArgs(3, 4)

        mock.foo(3, 4)
示例#22
0
 def __init__(self):
     self.PIPE = simple_mock.MockObject()
     self.STDOUT = simple_mock.MockObject()
     self._num_collect_calls = 0
     self._num_stop_calls = 0
示例#23
0
    def testArgs2(self):
        mock = simple_mock.MockObject()
        mock.ExpectCall('foo', 3, 4)

        mock.foo(3, 4)
 def _MockTryserverJson(self, bots_dict):
     trybot_browser_finder.urllib2 = simple_mock.MockObject()
     trybot_browser_finder.urllib2.ExpectCall('urlopen').WithArgs(
         'http://build.chromium.org/p/tryserver.chromium.perf/json'
     ).WillReturn(StringIO.StringIO(json.dumps({'builders': bots_dict})))
示例#25
0
    def testArgsMismatch(self):
        mock = simple_mock.MockObject()
        mock.ExpectCall('foo').WithArgs(3, 4)

        self.assertRaises(Exception, lambda: mock.foo(4, 4))
示例#26
0
    def testArgsDontCare(self):
        mock = simple_mock.MockObject()
        mock.ExpectCall('foo').WithArgs(_, 4)

        mock.foo(4, 4)