| 25 | maxDiff = None |
| 26 | |
| 27 | def setUp(self): |
| 28 | self.logcat_process = subprocess.Popen( |
| 29 | ["logcat", "-v", "tag"], stdout=subprocess.PIPE, |
| 30 | errors="backslashreplace" |
| 31 | ) |
| 32 | self.logcat_queue = queue.Queue() |
| 33 | |
| 34 | def logcat_thread(): |
| 35 | for line in self.logcat_process.stdout: |
| 36 | self.logcat_queue.put(line.rstrip("\n")) |
| 37 | self.logcat_process.stdout.close() |
| 38 | |
| 39 | self.logcat_thread = Thread(target=logcat_thread) |
| 40 | self.logcat_thread.start() |
| 41 | |
| 42 | try: |
| 43 | from ctypes import CDLL, c_char_p, c_int |
| 44 | android_log_write = getattr(CDLL("liblog.so"), "__android_log_write") |
| 45 | android_log_write.argtypes = (c_int, c_char_p, c_char_p) |
| 46 | ANDROID_LOG_INFO = 4 |
| 47 | |
| 48 | # Separate tests using a marker line with a different tag. |
| 49 | tag, message = "python.test", f"{self.id()} {time()}" |
| 50 | android_log_write( |
| 51 | ANDROID_LOG_INFO, tag.encode("UTF-8"), message.encode("UTF-8")) |
| 52 | self.assert_log("I", tag, message, skip=True) |
| 53 | except: |
| 54 | # If setUp throws an exception, tearDown is not automatically |
| 55 | # called. Avoid leaving a dangling thread which would keep the |
| 56 | # Python process alive indefinitely. |
| 57 | self.tearDown() |
| 58 | raise |
| 59 | |
| 60 | def assert_logs(self, level, tag, expected, **kwargs): |
| 61 | for line in expected: |