1# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
2#
3# Use of this source code is governed by a BSD-style license
4# that can be found in the LICENSE file in the root of the source
5# tree. An additional intellectual property rights grant can be found
6# in the file PATENTS.  All contributing project authors may
7# be found in the AUTHORS file in the root of the source tree.
8
9"""Unit tests for the export module.
10"""
11
12import logging
13import os
14import shutil
15import tempfile
16import unittest
17
18import pyquery as pq
19
20from . import audioproc_wrapper
21from . import collect_data
22from . import eval_scores_factory
23from . import evaluation
24from . import export
25from . import simulation
26from . import test_data_generation_factory
27
28
29class TestExport(unittest.TestCase):
30  """Unit tests for the export module.
31  """
32
33  _CLEAN_TMP_OUTPUT = True
34
35  def setUp(self):
36    """Creates temporary data to export."""
37    self._tmp_path = tempfile.mkdtemp()
38
39    # Run a fake experiment to produce data to export.
40    simulator = simulation.ApmModuleSimulator(
41        test_data_generator_factory=(
42            test_data_generation_factory.TestDataGeneratorFactory(
43                aechen_ir_database_path='',
44                noise_tracks_path='',
45                copy_with_identity=False)),
46        evaluation_score_factory=(
47          eval_scores_factory.EvaluationScoreWorkerFactory(
48              polqa_tool_bin_path=os.path.join(
49                  os.path.dirname(os.path.abspath(__file__)), 'fake_polqa'))),
50        ap_wrapper=audioproc_wrapper.AudioProcWrapper(
51            audioproc_wrapper.AudioProcWrapper.DEFAULT_APM_SIMULATOR_BIN_PATH),
52        evaluator=evaluation.ApmModuleEvaluator())
53    simulator.Run(
54        config_filepaths=['apm_configs/default.json'],
55        capture_input_filepaths=[
56            os.path.join(self._tmp_path, 'pure_tone-440_1000.wav'),
57            os.path.join(self._tmp_path, 'pure_tone-880_1000.wav'),
58        ],
59        test_data_generator_names=['identity', 'white_noise'],
60        eval_score_names=['audio_level_peak', 'audio_level_mean'],
61        output_dir=self._tmp_path)
62
63    # Export results.
64    p = collect_data.InstanceArgumentsParser()
65    args = p.parse_args(['--output_dir', self._tmp_path])
66    src_path = collect_data.ConstructSrcPath(args)
67    self._data_to_export = collect_data.FindScores(src_path, args)
68
69  def tearDown(self):
70    """Recursively deletes temporary folders."""
71    if self._CLEAN_TMP_OUTPUT:
72      shutil.rmtree(self._tmp_path)
73    else:
74      logging.warning(self.id() + ' did not clean the temporary path ' + (
75          self._tmp_path))
76
77  def testCreateHtmlReport(self):
78    fn_out = os.path.join(self._tmp_path, 'results.html')
79    exporter = export.HtmlExport(fn_out)
80    exporter.Export(self._data_to_export)
81
82    document = pq.PyQuery(filename=fn_out)
83    self.assertIsInstance(document, pq.PyQuery)
84    # TODO(alessiob): Use PyQuery API to check the HTML file.
85