Skip to content

Commit 61b1da9

Browse files
committed
Add property setter/getter utilities
1 parent 737b63a commit 61b1da9

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

lglpy/android/test.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,65 @@ def test_util_package_data_dir(self):
344344
self.assertTrue(data_dir)
345345

346346

347+
class AndroidTestDeviceProps(unittest.TestCase):
348+
'''
349+
This set of tests validates modifications to device-level settings
350+
This require adb to have a valid implicit default device connected.
351+
'''
352+
353+
def test_util_properties_modifiers(self):
354+
'''
355+
Test helper to set, get, or clear a property.
356+
'''
357+
conn = ADBConnect()
358+
prop = 'debug.vulkan.layers'
359+
360+
# Ensure test device starts from a clear state
361+
success = AndroidUtils.clear_property(conn, prop)
362+
self.assertTrue(success)
363+
364+
value = AndroidUtils.get_property(conn, prop)
365+
self.assertEqual(value, '')
366+
367+
success = AndroidUtils.set_property(conn, prop, 'test_')
368+
self.assertTrue(success)
369+
370+
value = AndroidUtils.get_property(conn, prop)
371+
self.assertEqual(value, 'test_')
372+
373+
success = AndroidUtils.clear_property(conn, prop)
374+
self.assertTrue(success)
375+
376+
value = AndroidUtils.get_property(conn, prop)
377+
self.assertEqual(value, '')
378+
379+
def test_util_settings_modifiers(self):
380+
'''
381+
Test helper to set, get, or clear a setting.
382+
'''
383+
conn = ADBConnect()
384+
prop = 'enable_gpu_debug_layers'
385+
386+
# Ensure test device starts from a clear state
387+
success = AndroidUtils.clear_setting(conn, prop)
388+
self.assertTrue(success)
389+
390+
value = AndroidUtils.get_setting(conn, prop)
391+
self.assertEqual(value, None)
392+
393+
success = AndroidUtils.set_setting(conn, prop, '1')
394+
self.assertTrue(success)
395+
396+
value = AndroidUtils.get_setting(conn, prop)
397+
self.assertEqual(value, '1')
398+
399+
success = AndroidUtils.clear_setting(conn, prop)
400+
self.assertTrue(success)
401+
402+
value = AndroidUtils.get_setting(conn, prop)
403+
self.assertEqual(value, None)
404+
405+
347406
class AndroidTestDeviceFilesystem(unittest.TestCase):
348407
'''
349408
This set of tests validates execution of device-level filesystem operations

lglpy/android/utils.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,125 @@ def get_package_data_dir(conn: ADBConnect):
255255

256256
except sp.CalledProcessError:
257257
return None
258+
259+
@staticmethod
260+
def set_property(conn: ADBConnect, property: str, value: str) -> bool:
261+
'''
262+
Set an Android system property to a value.
263+
264+
Args:
265+
conn: The adb connection.
266+
property: The name of the property to set.
267+
value: The desired value of the property.
268+
269+
Returns:
270+
True on success, False otherwise.
271+
'''
272+
try:
273+
conn.adb('shell', 'setprop', property, value)
274+
return True
275+
276+
except sp.CalledProcessError:
277+
return False
278+
279+
@staticmethod
280+
def get_property(conn: ADBConnect, property: str) -> Optional[str]:
281+
'''
282+
Get an Android system property value.
283+
284+
Args:
285+
conn: The adb connection.
286+
property: The name of the property to get.
287+
288+
Returns:
289+
The value of the property on success, None otherwise. Note that
290+
deleted settings that do not exist will also return None.
291+
'''
292+
try:
293+
value = conn.adb('shell', 'getprop', property)
294+
return value.strip()
295+
296+
except sp.CalledProcessError:
297+
return None
298+
299+
@staticmethod
300+
def clear_property(conn: ADBConnect, property: str) -> bool:
301+
'''
302+
Set an Android system property to an empty value.
303+
304+
Args:
305+
conn: The adb connection.
306+
property: The name of the property to set.
307+
308+
Returns:
309+
True on success, False otherwise.
310+
'''
311+
try:
312+
conn.adb('shell', 'setprop', property, '""')
313+
return True
314+
315+
except sp.CalledProcessError:
316+
return False
317+
318+
@staticmethod
319+
def set_setting(conn: ADBConnect, setting: str, value: str) -> bool:
320+
'''
321+
Set an Android system setting to a value.
322+
323+
Args:
324+
conn: The adb connection.
325+
setting: The name of the setting to set.
326+
value: The desired value of the setting.
327+
328+
Returns:
329+
True on success, False otherwise.
330+
'''
331+
try:
332+
conn.adb('shell', 'settings', 'put', 'global', setting, value)
333+
return True
334+
335+
except sp.CalledProcessError:
336+
return False
337+
338+
@staticmethod
339+
def get_setting(conn: ADBConnect, setting: str) -> Optional[str]:
340+
'''
341+
Get an Android system property setting.
342+
343+
Args:
344+
conn: The adb connection.
345+
setting: The name of the setting to get.
346+
347+
Returns:
348+
The value of the setting on success, None otherwise.
349+
'''
350+
try:
351+
value = conn.adb('shell', 'settings', 'get', 'global', setting)
352+
value = value.strip()
353+
354+
if value == 'null':
355+
return None
356+
357+
return value
358+
359+
except sp.CalledProcessError:
360+
return None
361+
362+
@staticmethod
363+
def clear_setting(conn: ADBConnect, setting: str) -> bool:
364+
'''
365+
Clear an Android system setting.
366+
367+
Args:
368+
conn: The adb connection.
369+
setting: The name of the setting to set.
370+
371+
Returns:
372+
True on success, False otherwise.
373+
'''
374+
try:
375+
conn.adb('shell', 'settings', 'delete', 'global', setting)
376+
return True
377+
378+
except sp.CalledProcessError:
379+
return False

0 commit comments

Comments
 (0)