1#!/usr/bin/env python3 2# 3# Copyright 2019, 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"""Unit tests for the logcat_utils.py script.""" 18 19import asyncio 20import datetime 21import re 22 23import logcat_utils 24from mock import MagicMock, patch 25 26def test_parse_logcat_datatime(): 27 # Act 28 result = logcat_utils.parse_logcat_datetime('2019-07-01 16:13:55.221') 29 30 # Assert 31 assert result == datetime.datetime(2019, 7, 1, 16, 13, 55, 221000) 32 33class AsyncMock(MagicMock): 34 async def __call__(self, *args, **kwargs): 35 return super(AsyncMock, self).__call__(*args, **kwargs) 36 37def _async_return(): 38 f = asyncio.Future() 39 f.set_result( 40 b'2019-07-01 15:51:53.290 27365 27392 I ActivityTaskManager: ' 41 b'Displayed com.google.android.music/com.android.music.activitymanagement.' 42 b'TopLevelActivity: +1s7ms') 43 return f 44 45def test_parse_displayed_time_succeed(): 46 # Act 47 with patch('asyncio.create_subprocess_exec', 48 new_callable=AsyncMock) as asyncio_mock: 49 asyncio_mock.return_value.stdout.readline = _async_return 50 timestamp = datetime.datetime(datetime.datetime.now().year, 7, 1, 16, 13, 51 55, 221000) 52 timeout_dt = timestamp + datetime.timedelta(0, 10) 53 pattern = re.compile('.*ActivityTaskManager: Displayed ' 54 'com.google.android.music/com.android.music.*') 55 result = logcat_utils.blocking_wait_for_logcat_pattern(timestamp, 56 pattern, 57 timeout_dt) 58 59 # Assert 60 assert result == '2019-07-01 15:51:53.290 27365 27392 I ' \ 61 'ActivityTaskManager: ' \ 62 'Displayed com.google.android.music/com.android.music.' \ 63 'activitymanagement.TopLevelActivity: +1s7ms' 64 65def _async_timeout_return(): 66 f = asyncio.Future() 67 f.set_result( 68 b'2019-07-01 17:51:53.290 27365 27392 I ActivityTaskManager: ' 69 b'Displayed com.google.android.music/com.android.music.activitymanagement.' 70 b'TopLevelActivity: +1s7ms') 71 return f 72 73def test_parse_displayed_time_timeout(): 74 # Act 75 with patch('asyncio.create_subprocess_exec', 76 new_callable=AsyncMock) as asyncio_mock: 77 asyncio_mock.return_value.stdout.readline = _async_timeout_return 78 timestamp = datetime.datetime(datetime.datetime.now().year, 79 7, 1, 16, 13, 55, 221000) 80 timeout_dt = timestamp + datetime.timedelta(0, 10) 81 pattern = re.compile('.*ActivityTaskManager: Displayed ' 82 'com.google.android.music/com.android.music.*') 83 result = logcat_utils.blocking_wait_for_logcat_pattern(timestamp, 84 pattern, 85 timeout_dt) 86 87 # Assert 88 assert result == None 89