2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
11 from pylib
import constants
12 from pylib
.device
import adb_wrapper
13 from pylib
.device
import decorators
14 from pylib
.device
import logcat_monitor
16 sys
.path
.append(os
.path
.join(
17 constants
.DIR_SOURCE_ROOT
, 'third_party', 'pymock'))
18 import mock
# pylint: disable=F0401
21 class LogcatMonitorTest(unittest
.TestCase
):
23 _TEST_THREADTIME_LOGCAT_DATA
= [
24 '01-01 01:02:03.456 7890 0987 V LogcatMonitorTest: '
25 'verbose logcat monitor test message 1',
26 '01-01 01:02:03.457 8901 1098 D LogcatMonitorTest: '
27 'debug logcat monitor test message 2',
28 '01-01 01:02:03.458 9012 2109 I LogcatMonitorTest: '
29 'info logcat monitor test message 3',
30 '01-01 01:02:03.459 0123 3210 W LogcatMonitorTest: '
31 'warning logcat monitor test message 4',
32 '01-01 01:02:03.460 1234 4321 E LogcatMonitorTest: '
33 'error logcat monitor test message 5',
34 '01-01 01:02:03.461 2345 5432 F LogcatMonitorTest: '
35 'fatal logcat monitor test message 6',
36 '01-01 01:02:03.462 3456 6543 D LogcatMonitorTest: '
39 def _createTestLog(self
, raw_logcat
=None):
40 test_adb
= adb_wrapper
.AdbWrapper('0123456789abcdef')
41 test_adb
.Logcat
= mock
.Mock(return_value
=(l
for l
in raw_logcat
))
42 test_log
= logcat_monitor
.LogcatMonitor(test_adb
, clear
=False)
45 def assertIterEqual(self
, expected_iter
, actual_iter
):
46 for expected
, actual
in itertools
.izip_longest(expected_iter
, actual_iter
):
49 msg
='actual has unexpected elements starting with %s' % str(actual
))
52 msg
='actual is missing elements starting with %s' % str(expected
))
53 self
.assertEqual(actual
.group('proc_id'), expected
[0])
54 self
.assertEqual(actual
.group('thread_id'), expected
[1])
55 self
.assertEqual(actual
.group('log_level'), expected
[2])
56 self
.assertEqual(actual
.group('component'), expected
[3])
57 self
.assertEqual(actual
.group('message'), expected
[4])
59 with self
.assertRaises(StopIteration):
61 with self
.assertRaises(StopIteration):
64 def testWaitFor_success(self
):
65 test_log
= self
._createTestLog
(
66 raw_logcat
=type(self
)._TEST
_THREADTIME
_LOGCAT
_DATA
)
67 actual_match
= test_log
.WaitFor(r
'.*(fatal|error) logcat monitor.*', None)
68 self
.assertTrue(actual_match
)
70 '01-01 01:02:03.460 1234 4321 E LogcatMonitorTest: '
71 'error logcat monitor test message 5',
72 actual_match
.group(0))
73 self
.assertEqual('error', actual_match
.group(1))
75 def testWaitFor_failure(self
):
76 test_log
= self
._createTestLog
(
77 raw_logcat
=type(self
)._TEST
_THREADTIME
_LOGCAT
_DATA
)
78 actual_match
= test_log
.WaitFor(
79 r
'.*My Success Regex.*', r
'.*(fatal|error) logcat monitor.*')
80 self
.assertIsNone(actual_match
)
82 def testFindAll_defaults(self
):
83 test_log
= self
._createTestLog
(
84 raw_logcat
=type(self
)._TEST
_THREADTIME
_LOGCAT
_DATA
)
86 ('7890', '0987', 'V', 'LogcatMonitorTest',
87 'verbose logcat monitor test message 1'),
88 ('8901', '1098', 'D', 'LogcatMonitorTest',
89 'debug logcat monitor test message 2'),
90 ('9012', '2109', 'I', 'LogcatMonitorTest',
91 'info logcat monitor test message 3'),
92 ('0123', '3210', 'W', 'LogcatMonitorTest',
93 'warning logcat monitor test message 4'),
94 ('1234', '4321', 'E', 'LogcatMonitorTest',
95 'error logcat monitor test message 5'),
96 ('2345', '5432', 'F', 'LogcatMonitorTest',
97 'fatal logcat monitor test message 6')]
98 actual_results
= test_log
.FindAll(r
'\S* logcat monitor test message \d')
99 self
.assertIterEqual(iter(expected_results
), actual_results
)
101 def testFindAll_defaults_miss(self
):
102 test_log
= self
._createTestLog
(
103 raw_logcat
=type(self
)._TEST
_THREADTIME
_LOGCAT
_DATA
)
104 expected_results
= []
105 actual_results
= test_log
.FindAll(r
'\S* nothing should match this \d')
106 self
.assertIterEqual(iter(expected_results
), actual_results
)
108 def testFindAll_filterProcId(self
):
109 test_log
= self
._createTestLog
(
110 raw_logcat
=type(self
)._TEST
_THREADTIME
_LOGCAT
_DATA
)
111 actual_results
= test_log
.FindAll(
112 r
'\S* logcat monitor test message \d', proc_id
=1234)
114 ('1234', '4321', 'E', 'LogcatMonitorTest',
115 'error logcat monitor test message 5')]
116 self
.assertIterEqual(iter(expected_results
), actual_results
)
118 def testFindAll_filterThreadId(self
):
119 test_log
= self
._createTestLog
(
120 raw_logcat
=type(self
)._TEST
_THREADTIME
_LOGCAT
_DATA
)
121 actual_results
= test_log
.FindAll(
122 r
'\S* logcat monitor test message \d', thread_id
=2109)
124 ('9012', '2109', 'I', 'LogcatMonitorTest',
125 'info logcat monitor test message 3')]
126 self
.assertIterEqual(iter(expected_results
), actual_results
)
128 def testFindAll_filterLogLevel(self
):
129 test_log
= self
._createTestLog
(
130 raw_logcat
=type(self
)._TEST
_THREADTIME
_LOGCAT
_DATA
)
131 actual_results
= test_log
.FindAll(
132 r
'\S* logcat monitor test message \d', log_level
=r
'[DW]')
134 ('8901', '1098', 'D', 'LogcatMonitorTest',
135 'debug logcat monitor test message 2'),
136 ('0123', '3210', 'W', 'LogcatMonitorTest',
137 'warning logcat monitor test message 4'),]
138 self
.assertIterEqual(iter(expected_results
), actual_results
)
140 def testFindAll_filterComponent(self
):
141 test_log
= self
._createTestLog
(
142 raw_logcat
=type(self
)._TEST
_THREADTIME
_LOGCAT
_DATA
)
143 actual_results
= test_log
.FindAll(r
'.*', component
='LogcatMonitorTest')
145 ('7890', '0987', 'V', 'LogcatMonitorTest',
146 'verbose logcat monitor test message 1'),
147 ('8901', '1098', 'D', 'LogcatMonitorTest',
148 'debug logcat monitor test message 2'),
149 ('9012', '2109', 'I', 'LogcatMonitorTest',
150 'info logcat monitor test message 3'),
151 ('0123', '3210', 'W', 'LogcatMonitorTest',
152 'warning logcat monitor test message 4'),
153 ('1234', '4321', 'E', 'LogcatMonitorTest',
154 'error logcat monitor test message 5'),
155 ('2345', '5432', 'F', 'LogcatMonitorTest',
156 'fatal logcat monitor test message 6'),
157 ('3456', '6543', 'D', 'LogcatMonitorTest',
159 self
.assertIterEqual(iter(expected_results
), actual_results
)
162 if __name__
== '__main__':
163 unittest
.main(verbosity
=2)