~3v1n0/autopilot/badwindow-errors-protect

« back to all changes in this revision

Viewing changes to autopilot/tests/test_unity_logging.py

  • Committer: Thomi Richards
  • Date: 2012-05-06 22:45:27 UTC
  • Revision ID: thomi.richards@canonical.com-20120506224527-xh6wixqiw0rarkmh
Imported code from unity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
# Copyright 2012 Canonical
 
3
# Author: Thomi Richards
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
 
 
9
from os import remove
 
10
from os.path import exists
 
11
from tempfile import mktemp
 
12
from testtools.matchers import Contains, Not
 
13
from time import sleep
 
14
 
 
15
 
 
16
from autopilot.tests import AutopilotTestCase
 
17
from autopilot.emulators.unity import (
 
18
    start_log_to_file,
 
19
    reset_logging,
 
20
    set_log_severity,
 
21
    log_unity_message,
 
22
    )
 
23
 
 
24
 
 
25
class UnityLoggingTests(AutopilotTestCase):
 
26
    """Tests for Unity's debug logging framework."""
 
27
 
 
28
    def start_new_log_file(self):
 
29
        fpath = mktemp()
 
30
        start_log_to_file(fpath)
 
31
        return fpath
 
32
 
 
33
    def test_new_file_created(self):
 
34
        """Unity must create log file when we call start_log_to_file.
 
35
        """
 
36
        fpath = self.start_new_log_file()
 
37
        self.addCleanup(remove, fpath)
 
38
        self.addCleanup(reset_logging)
 
39
        sleep(1)
 
40
        self.assertTrue(exists(fpath))
 
41
 
 
42
    def test_messages_arrive_in_file(self):
 
43
        fpath = self.start_new_log_file()
 
44
        log_unity_message("WARNING", "This is a warning of things to come")
 
45
        sleep(1)
 
46
        reset_logging()
 
47
 
 
48
        with open(fpath, 'r') as f:
 
49
            self.assertThat(f.read(), Contains("This is a warning of things to come"))
 
50
 
 
51
    def test_default_log_level_unchanged(self):
 
52
        fpath = self.start_new_log_file()
 
53
        log_unity_message("DEBUG", "This is some INFORMATION")
 
54
        sleep(1)
 
55
        reset_logging()
 
56
        with open(fpath, 'r') as f:
 
57
            self.assertThat(f.read(), Not(Contains("This is some INFORMATION")))
 
58
 
 
59
    def test_can_change_log_level(self):
 
60
        fpath = self.start_new_log_file()
 
61
        set_log_severity("", "DEBUG")
 
62
        self.addCleanup(set_log_severity, "", "INFO")
 
63
        log_unity_message("DEBUG", "This is some more INFORMATION")
 
64
        sleep(1)
 
65
        reset_logging()
 
66
        with open(fpath, 'r') as f:
 
67
            self.assertThat(f.read(), Contains("This is some more INFORMATION"))