1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import logging
7import os
8import subprocess
9import sys
10
11import pyauto_functional  # Must be imported before pyauto
12import pyauto
13
14sys.path.append('/usr/local')  # To make autotest libs importable.
15from autotest.cros import cros_ui
16from autotest.cros import cryptohome
17
18
19class ChromeosVolume(pyauto.PyUITest):
20  """Test case for volume levels.
21
22  Test volume and mute changes with different state like, login,
23  lock, logout, etc...
24  """
25
26  def setUp(self):
27    # We want a clean session_manager instance for every run,
28    # so restart ui now.
29    cros_ui.stop(allow_fail=True)
30    cryptohome.remove_all_vaults()
31    cros_ui.start(wait_for_login_prompt=False)
32    pyauto.PyUITest.setUp(self)
33    self._initial_volume_info = self.GetVolumeInfo()
34
35  def tearDown(self):
36    self.SetVolume(self._initial_volume_info['volume'])
37    self.SetMute(self._initial_volume_info['is_mute'])
38    pyauto.PyUITest.tearDown(self)
39
40  def ShouldAutoLogin(self):
41    return False
42
43  def _Login(self):
44    """Perform login"""
45    credentials = self.GetPrivateInfo()['test_google_account']
46    self.Login(credentials['username'], credentials['password'])
47    logging.info('Logged in as %s' % credentials['username'])
48    login_info = self.GetLoginInfo()
49    self.assertTrue(login_info['is_logged_in'], msg='Login failed.')
50
51  def testDefaultVolume(self):
52    """Test the default volume settings"""
53    self._Login()
54    board_name = self.ChromeOSBoard()
55    default_volume = self.GetPrivateInfo()['default_volume']
56    assert default_volume.get(board_name), \
57           'No volume settings available for %s.' % board_name
58    expected = {u'volume': default_volume[board_name],
59                u'is_mute': default_volume['is_mute']}
60    volume = self.GetVolumeInfo()
61    self.assertEqual(volume.get('is_mute'), expected.get('is_mute'))
62    self.assertAlmostEqual(volume.get('volume'), expected.get('volume'),
63        msg='Volume settings are set to %s, not matching with default '
64            'volume settings %s.' % (volume, expected))
65
66  def testLoginLogoutVolume(self):
67    """Test that volume settings are preserved after login and logout"""
68    before_login = self.GetVolumeInfo()
69    self._Login()
70    after_login = self.GetVolumeInfo()
71    self.assertEqual(before_login, after_login,
72        msg='Before login : %s and after login : %s, volume states are not '
73            'matching' % (before_login, after_login))
74    self.Logout()
75    after_logout = self.GetVolumeInfo()
76    self.assertEqual(after_login, after_logout,
77        msg='Before logout : %s and after logout : %s, volume states are not '
78            'matching' % (after_login, after_logout))
79
80  def testLoginLockoutVolume(self):
81    """Test that volume changes on the lock screen, are preserved"""
82    lock_volume = {u'volume': 50.000000000000014, u'is_mute': True}
83    self._Login()
84    login_vol = self.GetVolumeInfo()
85    self.LockScreen()
86    self.SetVolume(lock_volume['volume'])
87    self.SetMute(lock_volume['is_mute'])
88    self.UnlockScreen(self.GetPrivateInfo()['test_google_account']['password'])
89    after_login = self.GetVolumeInfo()
90    self.assertEqual(lock_volume, after_login,
91        msg='Locking screen volume changes are not preserved')
92
93
94if __name__ == '__main__':
95  pyauto_functional.Main()
96