<commit_before># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import cStringIO
import json
import os
import unittest

from telemetry import tab_test_case
from telemetry import tracing_backend
from telemetry import util


class TracingBackendTest(tab_test_case.TabTestCase):
  def _StartServer(self):
    base_dir = os.path.dirname(__file__)
    self._browser.SetHTTPServerDirectory(os.path.join(base_dir, '..',
        'unittest_data'))

  def _WaitForAnimationFrame(self):
    def _IsDone():
      js_is_done = """done"""
      return bool(self._tab.EvaluateJavaScript(js_is_done))
    util.WaitFor(_IsDone, 5)

  def testGotTrace(self):
    self._StartServer()
    self._browser.StartTracing()
    self.assertTrue(self._browser.supports_tracing)
    self._browser.StopTracing()
    model = self._browser.GetTraceResultAndReset().AsTimelineModel()
    events = model.GetAllEvents()
    assert len(events) > 0

class TracingResultImplTest(unittest.TestCase):
  def testWrite1(self):
    ri = tracing_backend.TraceResultImpl([])
    f = cStringIO.StringIO()
    ri.Serialize(f)
    v = f.getvalue()

    j = json.loads(v)
    assert 'traceEvents' in j
    self.assertEquals(j['traceEvents'], [])

  def testWrite2(self):
    ri = tracing_backend.TraceResultImpl([
        '"foo"',
        '"bar"'])
    f = cStringIO.StringIO()
    ri.Serialize(f)
    v = f.getvalue()

    j = json.loads(v)
    assert 'traceEvents' in j
    self.assertEquals(j['traceEvents'], ['foo', 'bar'])

  def testWrite3(self):
    ri = tracing_backend.TraceResultImpl([
        '"foo"',
        '"bar"',
        '"baz"'])
    f = cStringIO.StringIO()
    ri.Serialize(f)
    v = f.getvalue()

    j = json.loads(v)
    assert 'traceEvents' in j
    self.assertEquals(j['traceEvents'],
                      ['foo', 'bar', 'baz'])
<commit_msg>Telemetry: fixes testGotTrace on old browsers.<commit_after># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import cStringIO
import json
import logging
import os
import unittest

from telemetry import tab_test_case
from telemetry import tracing_backend
from telemetry import util


class TracingBackendTest(tab_test_case.TabTestCase):
  def _StartServer(self):
    base_dir = os.path.dirname(__file__)
    self._browser.SetHTTPServerDirectory(os.path.join(base_dir, '..',
        'unittest_data'))

  def _WaitForAnimationFrame(self):
    def _IsDone():
      js_is_done = """done"""
      return bool(self._tab.EvaluateJavaScript(js_is_done))
    util.WaitFor(_IsDone, 5)

  def testGotTrace(self):
    if not self._browser.supports_tracing:
      logging.warning('Browser does not support tracing, skipping test.')
      return
    self._StartServer()
    self._browser.StartTracing()
    self._browser.StopTracing()
    model = self._browser.GetTraceResultAndReset().AsTimelineModel()
    events = model.GetAllEvents()
    assert len(events) > 0

class TracingResultImplTest(unittest.TestCase):
  def testWrite1(self):
    ri = tracing_backend.TraceResultImpl([])
    f = cStringIO.StringIO()
    ri.Serialize(f)
    v = f.getvalue()

    j = json.loads(v)
    assert 'traceEvents' in j
    self.assertEquals(j['traceEvents'], [])

  def testWrite2(self):
    ri = tracing_backend.TraceResultImpl([
        '"foo"',
        '"bar"'])
    f = cStringIO.StringIO()
    ri.Serialize(f)
    v = f.getvalue()

    j = json.loads(v)
    assert 'traceEvents' in j
    self.assertEquals(j['traceEvents'], ['foo', 'bar'])

  def testWrite3(self):
    ri = tracing_backend.TraceResultImpl([
        '"foo"',
        '"bar"',
        '"baz"'])
    f = cStringIO.StringIO()
    ri.Serialize(f)
    v = f.getvalue()

    j = json.loads(v)
    assert 'traceEvents' in j
    self.assertEquals(j['traceEvents'],
                      ['foo', 'bar', 'baz'])

{'fraction_non_alphanumeric': 0.12425, 'fraction_numerical': 0.0045, 'mean_word_length': 3.3679039301310043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5541367', 'n_tokens_mistral': 1302, 'n_tokens_neox': 1272, 'n_words': 297}