1#!/usr/bin/env python3 2# 3# Copyright 2022, 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 metrics_utils.""" 18 19# pylint: disable=invalid-name 20 21from io import StringIO 22import sys 23import unittest 24from unittest import mock 25 26from atest.metrics import metrics_utils 27 28 29class MetricsUtilsUnittests(unittest.TestCase): 30 """Unit tests for metrics_utils.py""" 31 32 def setUp(self) -> None: 33 self.maxDiff = None 34 35 @mock.patch('atest.metrics.metrics_base.get_user_type') 36 def test_print_data_collection_notice(self, mock_get_user_type): 37 """Test method print_data_collection_notice.""" 38 39 # get_user_type return 1(external). 40 mock_get_user_type.return_value = 1 41 capture_output = StringIO() 42 sys.stdout = capture_output 43 metrics_utils.print_data_collection_notice(colorful=False) 44 sys.stdout = sys.__stdout__ 45 self.assertEqual(capture_output.getvalue(), "") 46 47 # get_user_type return 0(internal). 48 red = '31m' 49 green = '32m' 50 start = '\033[1;' 51 end = '\033[0m' 52 mock_get_user_type.return_value = 0 53 notice_str = ( 54 f'\n==================\n{start}{red}Notice:{end}\n' 55 f'{start}{green} We collect usage statistics (including usernames) ' 56 'in accordance with our ' 57 'Content Licenses (https://source.android.com/setup/start/licenses), ' 58 'Contributor License Agreement (https://cla.developers.google.com/), ' 59 'Privacy Policy (https://policies.google.com/privacy) and ' 60 f'Terms of Service (https://policies.google.com/terms).{end}' 61 '\n==================\n\n' 62 ) 63 capture_output = StringIO() 64 sys.stdout = capture_output 65 metrics_utils.print_data_collection_notice() 66 sys.stdout = sys.__stdout__ 67 self.assertEqual(capture_output.getvalue(), notice_str) 68