1#!/usr/bin/env python3
2#
3# Copyright 2018, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Unittests for robolectric_test_runner."""
18
19# pylint: disable=line-too-long
20
21import json
22import unittest
23import subprocess
24import tempfile
25
26from unittest import mock
27
28from test_finders import test_info
29from test_runners import event_handler
30from test_runners import robolectric_test_runner
31
32# pylint: disable=protected-access
33class RobolectricTestRunnerUnittests(unittest.TestCase):
34    """Unit tests for robolectric_test_runner.py"""
35
36    def setUp(self):
37        self.polling_time = robolectric_test_runner.POLL_FREQ_SECS
38        self.suite_tr = robolectric_test_runner.RobolectricTestRunner(results_dir='')
39
40    def tearDown(self):
41        mock.patch.stopall()
42
43    @mock.patch.object(robolectric_test_runner.RobolectricTestRunner, 'run')
44    def test_run_tests_raw(self, mock_run):
45        """Test run_tests_raw method."""
46        test_infos = [test_info.TestInfo("Robo1",
47                                         "RobolectricTestRunner",
48                                         ["RoboTest"])]
49        extra_args = []
50        mock_subproc = mock.Mock()
51        mock_run.return_value = mock_subproc
52        mock_subproc.returncode = 0
53        mock_reporter = mock.Mock()
54        # Test Build Pass
55        self.assertEqual(
56            0,
57            self.suite_tr.run_tests_raw(test_infos, extra_args, mock_reporter))
58        # Test Build Fail
59        mock_subproc.returncode = 1
60        self.assertNotEqual(
61            0,
62            self.suite_tr.run_tests_raw(test_infos, extra_args, mock_reporter))
63
64    @mock.patch.object(event_handler.EventHandler, 'process_event')
65    def test_exec_with_robo_polling_complete_information(self, mock_pe):
66        """Test _exec_with_robo_polling method."""
67        event_name = 'TEST_STARTED'
68        event_data = {'className':'SomeClass', 'testName':'SomeTestName'}
69
70        json_event_data = json.dumps(event_data)
71        data = '%s %s\n\n' %(event_name, json_event_data)
72        event_file = tempfile.NamedTemporaryFile(delete=True)
73        subprocess.call("echo '%s' -n >> %s" %(data, event_file.name), shell=True)
74        robo_proc = subprocess.Popen("sleep %s" %str(self.polling_time * 2), shell=True)
75        self.suite_tr. _exec_with_robo_polling(event_file, robo_proc, mock_pe)
76        calls = [mock.call.process_event(event_name, event_data)]
77        mock_pe.assert_has_calls(calls)
78
79    @mock.patch.object(event_handler.EventHandler, 'process_event')
80    def test_exec_with_robo_polling_with_partial_info(self, mock_pe):
81        """Test _exec_with_robo_polling method."""
82        event_name = 'TEST_STARTED'
83        event1 = '{"className":"SomeClass","test'
84        event2 = 'Name":"SomeTestName"}\n\n'
85        data1 = '%s %s'%(event_name, event1)
86        data2 = event2
87        event_file = tempfile.NamedTemporaryFile(delete=True)
88        subprocess.Popen("echo -n '%s' >> %s" %(data1, event_file.name), shell=True)
89        robo_proc = subprocess.Popen("echo '%s' >> %s && sleep %s"
90                                     %(data2,
91                                       event_file.name,
92                                       str(self.polling_time*5)),
93                                     shell=True)
94        self.suite_tr. _exec_with_robo_polling(event_file, robo_proc, mock_pe)
95        calls = [mock.call.process_event(event_name,
96                                         json.loads(event1 + event2))]
97        mock_pe.assert_has_calls(calls)
98
99    @mock.patch.object(event_handler.EventHandler, 'process_event')
100    def test_exec_with_robo_polling_with_fail_stacktrace(self, mock_pe):
101        """Test _exec_with_robo_polling method."""
102        event_name = 'TEST_FAILED'
103        event_data = {'className':'SomeClass', 'testName':'SomeTestName',
104                      'trace':'{"trace":"AssertionError: <true> is equal to <false>\n'
105                              'at FailureStrategy.fail(FailureStrategy.java:24)\n'
106                              'at FailureStrategy.fail(FailureStrategy.java:20)\n'}
107        data = '%s %s\n\n'%(event_name, json.dumps(event_data))
108        event_file = tempfile.NamedTemporaryFile(delete=True)
109        subprocess.call("echo '%s' -n >> %s" %(data, event_file.name), shell=True)
110        robo_proc = subprocess.Popen("sleep %s" %str(self.polling_time * 2), shell=True)
111        self.suite_tr. _exec_with_robo_polling(event_file, robo_proc, mock_pe)
112        calls = [mock.call.process_event(event_name, event_data)]
113        mock_pe.assert_has_calls(calls)
114
115    @mock.patch.object(event_handler.EventHandler, 'process_event')
116    def test_exec_with_robo_polling_with_multi_event(self, mock_pe):
117        """Test _exec_with_robo_polling method."""
118        event_file = tempfile.NamedTemporaryFile(delete=True)
119        events = [
120            ('TEST_MODULE_STARTED', {
121                'moduleContextFileName':'serial-util1146216{974}2772610436.ser',
122                'moduleName':'someTestModule'}),
123            ('TEST_RUN_STARTED', {'testCount': 2}),
124            ('TEST_STARTED', {'start_time':52, 'className':'someClassName',
125                              'testName':'someTestName'}),
126            ('TEST_ENDED', {'end_time':1048, 'className':'someClassName',
127                            'testName':'someTestName'}),
128            ('TEST_STARTED', {'start_time':48, 'className':'someClassName2',
129                              'testName':'someTestName2'}),
130            ('TEST_FAILED', {'className':'someClassName2', 'testName':'someTestName2',
131                             'trace': 'someTrace'}),
132            ('TEST_ENDED', {'end_time':9876450, 'className':'someClassName2',
133                            'testName':'someTestName2'}),
134            ('TEST_RUN_ENDED', {}),
135            ('TEST_MODULE_ENDED', {'foo': 'bar'}),]
136        data = ''
137        for event in events:
138            data += '%s %s\n\n'%(event[0], json.dumps(event[1]))
139
140        subprocess.call("echo '%s' -n >> %s" %(data, event_file.name), shell=True)
141        robo_proc = subprocess.Popen("sleep %s" %str(self.polling_time * 2), shell=True)
142        self.suite_tr. _exec_with_robo_polling(event_file, robo_proc, mock_pe)
143        calls = [mock.call.process_event(name, data) for name, data in events]
144        mock_pe.assert_has_calls(calls)
145
146if __name__ == '__main__':
147    unittest.main()
148