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              echo_metric_tool_bin_path=None
51          )),
52        ap_wrapper=audioproc_wrapper.AudioProcWrapper(
53            audioproc_wrapper.AudioProcWrapper.DEFAULT_APM_SIMULATOR_BIN_PATH),
54        evaluator=evaluation.ApmModuleEvaluator())
55    simulator.Run(
56        config_filepaths=['apm_configs/default.json'],
57        capture_input_filepaths=[
58            os.path.join(self._tmp_path, 'pure_tone-440_1000.wav'),
59            os.path.join(self._tmp_path, 'pure_tone-880_1000.wav'),
60        ],
61        test_data_generator_names=['identity', 'white_noise'],
62        eval_score_names=['audio_level_peak', 'audio_level_mean'],
63        output_dir=self._tmp_path)
64
65    # Export results.
66    p = collect_data.InstanceArgumentsParser()
67    args = p.parse_args(['--output_dir', self._tmp_path])
68    src_path = collect_data.ConstructSrcPath(args)
69    self._data_to_export = collect_data.FindScores(src_path, args)
70
71  def tearDown(self):
72    """Recursively deletes temporary folders."""
73    if self._CLEAN_TMP_OUTPUT:
74      shutil.rmtree(self._tmp_path)
75    else:
76      logging.warning(self.id() + ' did not clean the temporary path ' + (
77          self._tmp_path))
78
79  def testCreateHtmlReport(self):
80    fn_out = os.path.join(self._tmp_path, 'results.html')
81    exporter = export.HtmlExport(fn_out)
82    exporter.Export(self._data_to_export)
83
84    document = pq.PyQuery(filename=fn_out)
85    self.assertIsInstance(document, pq.PyQuery)
86    # TODO(alessiob): Use PyQuery API to check the HTML file.
87