~canonical-platform-qa/ubuntu-system-tests/stability-longer-preview-timeout

« back to all changes in this revision

Viewing changes to ubuntu_system_tests/helpers/power/fixture_setup.py

test brightness from indicator.

Approved by Brendan Donegan, PS Jenkins bot, Richard Huddie.

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
#
 
3
# Ubuntu System Tests
 
4
# Copyright (C) 2015 Canonical
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation, either version 3 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
 
 
20
import subprocess
 
21
import shlex
 
22
 
 
23
import fixtures
 
24
 
 
25
 
 
26
class BrightnessEnvironmentFixture(fixtures.Fixture):
 
27
 
 
28
    def __init__(self, auto_brightness='disable', brightness_value=255):
 
29
        self.auto_brightness = auto_brightness
 
30
        self.brightness_value = str(brightness_value)
 
31
 
 
32
    def setUp(self):
 
33
        super().setUp()
 
34
        self.useFixture(AutoBrightness(state=self.auto_brightness))
 
35
        self.useFixture(Brightness(value=self.brightness_value))
 
36
 
 
37
 
 
38
def _get_auto_brightness_current_setting():
 
39
    """Return current auto brightness state from gsettings."""
 
40
    command = 'gsettings get com.ubuntu.touch.system auto-brightness'
 
41
    return subprocess.check_output(shlex.split(command),
 
42
                                   universal_newlines=True).strip()
 
43
 
 
44
 
 
45
def _set_auto_brightness_setting(value):
 
46
    """Change auto brightness from gsettings."""
 
47
    command = 'gsettings set com.ubuntu.touch.system ' \
 
48
              'auto-brightness {}'.format(value)
 
49
    return subprocess.check_call(shlex.split(command))
 
50
 
 
51
 
 
52
class AutoBrightness(fixtures.Fixture):
 
53
 
 
54
    def __init__(self, state):
 
55
        self.request = state
 
56
 
 
57
    def setUp(self):
 
58
        super().setUp()
 
59
        old_value = _get_auto_brightness_current_setting()
 
60
        self.addCleanup(_set_auto_brightness_setting, old_value)
 
61
 
 
62
        if self.request == 'enable':
 
63
            _set_auto_brightness_setting('true')
 
64
        elif self.request == 'disable':
 
65
            _set_auto_brightness_setting('false')
 
66
 
 
67
 
 
68
def _set_brightness_value(value):
 
69
    """Set brightness through powerd, should be between 10 and 255."""
 
70
    command = 'powerd-cli brightness {}'.format(value)
 
71
    subprocess.check_call(shlex.split(command),
 
72
                          stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
 
73
 
 
74
 
 
75
def _get_current_brightness():
 
76
    """Return the current brightness value."""
 
77
    command = 'gsettings get com.ubuntu.touch.system brightness'
 
78
    return subprocess.check_output(shlex.split(command),
 
79
                                   universal_newlines=True).strip()
 
80
 
 
81
 
 
82
class Brightness(fixtures.Fixture):
 
83
    """Fixture to set custom screen brightness."""
 
84
 
 
85
    def __init__(self, value):
 
86
        self.value = value
 
87
 
 
88
    def setUp(self):
 
89
        super().setUp()
 
90
        old_value = _get_current_brightness()
 
91
        _set_brightness_value(self.value)
 
92
        self.addCleanup(_set_brightness_value, old_value)