1#!/usr/bin/env python
2
3# Copyright (c) 2016 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import contextlib
8import os
9import unittest
10
11from systrace import decorators
12from systrace import run_systrace
13from systrace import update_systrace_trace_viewer
14from systrace import util
15
16TEST_DIR = os.path.join(os.path.dirname(__file__), '..', 'test_data')
17
18COMPRESSED_ATRACE_DATA = os.path.join(TEST_DIR, 'compressed_atrace_data.txt')
19DECOMPRESSED_ATRACE_DATA = os.path.join(TEST_DIR,
20                                        'decompressed_atrace_data.txt')
21NON_EXISTENT_DATA = os.path.join(TEST_DIR, 'THIS_FILE_DOES_NOT_EXIST.txt')
22
23class AtraceFromFileAgentTest(unittest.TestCase):
24  @decorators.HostOnlyTest
25  def test_from_file(self):
26    update_systrace_trace_viewer.update(force_update=True)
27    self.assertTrue(os.path.exists(
28        update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
29    output_file_name = util.generate_random_filename_for_test()
30    try:
31      # use from-file to create a specific expected output
32      run_systrace.main_impl(['./run_systrace.py',
33                              '--from-file',
34                              COMPRESSED_ATRACE_DATA,
35                              '-o',
36                              output_file_name])
37      # and verify file contents
38      with contextlib.nested(open(output_file_name, 'r'),
39                             open(DECOMPRESSED_ATRACE_DATA, 'r')) as (f1, f2):
40        full_trace = f1.read()
41        expected_contents = f2.read()
42        self.assertTrue(expected_contents in full_trace)
43    except:
44      raise
45    finally:
46      os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
47      if os.path.exists(output_file_name):
48        os.remove(output_file_name)
49
50
51  @decorators.HostOnlyTest
52  def test_default_output_filename(self):
53    update_systrace_trace_viewer.update(force_update=True)
54    self.assertTrue(os.path.exists(
55        update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
56    output_file_name = os.path.join(TEST_DIR, 'compressed_atrace_data.html')
57    try:
58      # use from-file to create a specific expected output
59      run_systrace.main_impl(['./run_systrace.py',
60                              '--from-file',
61                              COMPRESSED_ATRACE_DATA])
62      # and verify file contents
63      with contextlib.nested(open(output_file_name, 'r'),
64                             open(DECOMPRESSED_ATRACE_DATA, 'r')) as (f1, f2):
65        full_trace = f1.read()
66        expected_contents = f2.read()
67        self.assertTrue(expected_contents in full_trace)
68    except:
69      raise
70    finally:
71      os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
72      if os.path.exists(output_file_name):
73        os.remove(output_file_name)
74
75
76  @decorators.HostOnlyTest
77  def test_missing_file(self):
78    try:
79      run_systrace.main_impl(['./run_systrace.py',
80                              '--from-file',
81                              NON_EXISTENT_DATA])
82      self.fail('should not get here')
83    except IOError:
84      pass
85