def test_init(self): inst = daemon.Brightness('gazp9', 'intel_backlight') self.assertEqual(inst.model, 'gazp9') self.assertEqual(inst.name, 'intel_backlight') self.assertEqual(inst.key, 'gazp9.intel_backlight') self.assertIsNone(inst.current) self.assertEqual(inst.backlight_dir, '/sys/class/backlight/intel_backlight' ) self.assertEqual(inst.max_brightness_file, '/sys/class/backlight/intel_backlight/max_brightness' ) self.assertEqual(inst.saved_file, '/var/lib/system76-driver/brightness.json' ) tmp = TempDir() inst = daemon.Brightness('sabc1', 'acpi_video0', rootdir=tmp.dir) self.assertEqual(inst.model, 'sabc1') self.assertEqual(inst.name, 'acpi_video0') self.assertEqual(inst.key, 'sabc1.acpi_video0') self.assertIsNone(inst.current) self.assertEqual(inst.backlight_dir, tmp.join('sys', 'class', 'backlight', 'acpi_video0') ) self.assertEqual(inst.max_brightness_file, tmp.join('sys', 'class', 'backlight', 'acpi_video0', 'max_brightness') ) self.assertEqual(inst.brightness_file, tmp.join('sys', 'class', 'backlight', 'acpi_video0', 'brightness') ) self.assertEqual(inst.saved_file, tmp.join('var', 'lib', 'system76-driver', 'brightness.json') )
def test_save(self): tmp = TempDir() inst = daemon.Brightness('gazp9', 'intel_backlight', rootdir=tmp.dir) # Missing dir with self.assertRaises(FileNotFoundError) as cm: inst.save(303) self.assertTrue(cm.exception.filename.startswith(inst.saved_file + '.')) # No file: tmp.makedirs('var', 'lib', 'system76-driver') self.assertIsNone(inst.save(303)) self.assertEqual(json.load(open(inst.saved_file, 'r')), {'gazp9.intel_backlight': 303} ) self.assertEqual(inst.load(), 303) # Existing file: json.dump({'gazp9': 70}, open(inst.saved_file, 'w')) self.assertIsNone(inst.save(76)) self.assertEqual( json.load(open(inst.saved_file, 'r')), {'gazp9.intel_backlight': 76, 'gazp9': 70} ) self.assertEqual(inst.load(), 76) # Existing file with existing 'gazp9' entry: self.assertIsNone(inst.save(69)) self.assertEqual( json.load(open(inst.saved_file, 'r')), {'gazp9.intel_backlight': 69, 'gazp9': 70} ) self.assertEqual(inst.load(), 69)
def test_read_brightness(self): tmp = TempDir() inst = daemon.Brightness('gazp9', 'intel_backlight', rootdir=tmp.dir) # Missing dir with self.assertRaises(FileNotFoundError) as cm: inst.read_brightness() self.assertEqual(cm.exception.filename, inst.brightness_file) # Mising file: tmp.makedirs('sys', 'class', 'backlight', 'intel_backlight') with self.assertRaises(FileNotFoundError) as cm: inst.read_brightness() self.assertEqual(cm.exception.filename, inst.brightness_file) # Bad file content open(inst.brightness_file, 'x').write('foobar\n') with self.assertRaises(ValueError) as cm: inst.read_brightness() self.assertEqual(str(cm.exception), "invalid literal for int() with base 10: b'foobar\\n'" ) # Good values open(inst.brightness_file, 'w').write('0\n') self.assertEqual(inst.read_brightness(), 0) open(inst.brightness_file, 'w').write('17\n') self.assertEqual(inst.read_brightness(), 17) open(inst.brightness_file, 'w').write('1776\n') self.assertEqual(inst.read_brightness(), 1776)
def test_restore(self): # Test when all needed files/dirs are missing: tmp = TempDir() inst = daemon.Brightness('gazp9', 'intel_backlight', rootdir=tmp.dir) self.assertIsNone(inst.restore()) # When /var/lib/system76-driver/brightness.json file exists, but the # /sys/class/backlight/intel_backlight/ dir doesn't exist, should get a # FileNotFoundError: tmp = TempDir() tmp.makedirs('var', 'lib', 'system76-driver') tmp.write(b'{"gazp9.intel_backlight":790}', 'var', 'lib', 'system76-driver', 'brightness.json') inst = daemon.Brightness('gazp9', 'intel_backlight', rootdir=tmp.dir) with self.assertRaises(FileNotFoundError) as cm: inst.restore() self.assertEqual(cm.exception.filename, tmp.join('sys', 'class', 'backlight', 'intel_backlight', 'brightness') ) # When /var/lib/system76-driver/brightness.json file does *not* exist, # should default to 75% of of max_brightness: tmp = TempDir() tmp.makedirs('sys', 'class', 'backlight', 'intel_backlight') tmp.write(b'1054\n', 'sys', 'class', 'backlight', 'intel_backlight', 'max_brightness') inst = daemon.Brightness('gazp9', 'intel_backlight', rootdir=tmp.dir) self.assertIsNone(inst.restore()) self.assertEqual(inst.current, 790) self.assertEqual(open(inst.brightness_file, 'rb').read(), b'790') # Should overright contents of # /sys/class/backlight/intel_backlight/brightness file: tmp = TempDir() tmp.makedirs('sys', 'class', 'backlight', 'intel_backlight') tmp.write(b'1054\n', 'sys', 'class', 'backlight', 'intel_backlight', 'max_brightness') tmp.write(b'42\n', 'sys', 'class', 'backlight', 'intel_backlight', 'brightness') inst = daemon.Brightness('gazp9', 'intel_backlight', rootdir=tmp.dir) self.assertIsNone(inst.restore()) self.assertEqual(inst.current, 790) self.assertEqual(open(inst.brightness_file, 'rb').read(), b'790')
def test_write_brightness(self): tmp = TempDir() inst = daemon.Brightness('gazp9', 'intel_backlight', rootdir=tmp.dir) # Missing dir with self.assertRaises(FileNotFoundError) as cm: inst.write_brightness(303) self.assertEqual(cm.exception.filename, inst.brightness_file) # No file: tmp.makedirs('sys', 'class', 'backlight', 'intel_backlight') self.assertIsNone(inst.write_brightness(303)) self.assertEqual(open(inst.brightness_file, 'r').read(), '303') self.assertEqual(inst.read_brightness(), 303) # Existing file: self.assertIsNone(inst.write_brightness(76)) self.assertEqual(open(inst.brightness_file, 'r').read(), '76') self.assertEqual(inst.read_brightness(), 76)
def test_load(self): tmp = TempDir() tmp.makedirs('var', 'lib', 'system76-driver') inst = daemon.Brightness('gazp9', 'intel_backlight', rootdir=tmp.dir) # No file: self.assertIsNone(inst.load()) # Bad value in file open(inst.saved_file, 'x').write('no json here') self.assertIsNone(inst.load()) open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': 17.69}) ) self.assertIsNone(inst.load()) # Less than or equal to zero open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': 0}) ) self.assertIsNone(inst.load()) open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': -1}) ) self.assertIsNone(inst.load()) # One and other good values open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': 1}) ) self.assertEqual(inst.load(), 1) open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': 1054}) ) self.assertEqual(inst.load(), 1054) ############################################################ # All the same, except this time with a max_brightness file: tmp = TempDir() tmp.makedirs('var', 'lib', 'system76-driver') inst = daemon.Brightness('gazp9', 'intel_backlight', rootdir=tmp.dir) tmp.makedirs('sys', 'class', 'backlight', 'intel_backlight') open(inst.max_brightness_file, 'xb').write(b'5273') # No file self.assertEqual(inst.load(), 3954) # Bad value in file open(inst.saved_file, 'x').write('no json here') self.assertEqual(inst.load(), 3954) open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': 17.69}) ) self.assertEqual(inst.load(), 3954) # Less than or equal to zero open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': 0}) ) self.assertEqual(inst.load(), 3954) open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': -1}) ) self.assertEqual(inst.load(), 3954) # One and other good values open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': 1}) ) self.assertEqual(inst.load(), 1) open(inst.saved_file, 'w').write( json.dumps({'gazp9.intel_backlight': 1054}) ) self.assertEqual(inst.load(), 1054)