Fix Android storage directory detection in StorageManager

- Updated logic to determine storage directory based on ANDROID_DATA and EXTERNAL_STORAGE environment variables.
- Added unit tests to cover new storage directory detection scenarios for Android, including fallback options.
This commit is contained in:
2025-11-15 23:38:39 -06:00
parent 0aaa7938e6
commit e36bfec4a0
2 changed files with 40 additions and 7 deletions

View File

@@ -37,15 +37,18 @@ class StorageManager:
pass pass
if os.name == "posix" and "ANDROID_ROOT" in os.environ: if os.name == "posix" and "ANDROID_ROOT" in os.environ:
# Android - use user-accessible external storage if "ANDROID_DATA" in os.environ:
storage_dir = pathlib.Path("/storage/emulated/0/Documents/ren_browser") storage_dir = pathlib.Path(os.environ["ANDROID_DATA"]) / "ren_browser"
elif "EXTERNAL_STORAGE" in os.environ:
ext_storage = pathlib.Path(os.environ["EXTERNAL_STORAGE"])
storage_dir = ext_storage / "ren_browser"
else:
storage_dir = pathlib.Path("/data/local/tmp/ren_browser")
elif hasattr(os, "uname") and "iOS" in str( elif hasattr(os, "uname") and "iOS" in str(
getattr(os, "uname", lambda: "")() getattr(os, "uname", lambda: "")()
).replace("iPhone", "iOS"): ).replace("iPhone", "iOS"):
# iOS - use app's documents directory
storage_dir = pathlib.Path.home() / "Documents" / "ren_browser" storage_dir = pathlib.Path.home() / "Documents" / "ren_browser"
else: else:
# Desktop (Linux, Windows, macOS) - use home directory
if "APPDATA" in os.environ: # Windows if "APPDATA" in os.environ: # Windows
storage_dir = pathlib.Path(os.environ["APPDATA"]) / "ren_browser" storage_dir = pathlib.Path(os.environ["APPDATA"]) / "ren_browser"
elif "XDG_CONFIG_HOME" in os.environ: # Linux XDG standard elif "XDG_CONFIG_HOME" in os.environ: # Linux XDG standard

View File

@@ -68,8 +68,38 @@ class TestStorageManager:
# Skip this test on non-Windows systems to avoid path issues # Skip this test on non-Windows systems to avoid path issues
pytest.skip("Windows path test skipped on non-Windows system") pytest.skip("Windows path test skipped on non-Windows system")
def test_get_storage_directory_android(self): def test_get_storage_directory_android_with_android_data(self):
"""Test storage directory detection for Android.""" """Test storage directory detection for Android with ANDROID_DATA."""
with (
patch("os.name", "posix"),
patch.dict("os.environ", {"ANDROID_ROOT": "/system", "ANDROID_DATA": "/data"}, clear=True),
patch("pathlib.Path.mkdir"),
):
with patch(
"ren_browser.storage.storage.StorageManager._ensure_storage_directory"
):
storage = StorageManager()
storage._storage_dir = storage._get_storage_directory()
expected_dir = Path("/data/ren_browser")
assert storage._storage_dir == expected_dir
def test_get_storage_directory_android_with_external_storage(self):
"""Test storage directory detection for Android with EXTERNAL_STORAGE."""
with (
patch("os.name", "posix"),
patch.dict("os.environ", {"ANDROID_ROOT": "/system", "EXTERNAL_STORAGE": "/storage/emulated/0"}, clear=True),
patch("pathlib.Path.mkdir"),
):
with patch(
"ren_browser.storage.storage.StorageManager._ensure_storage_directory"
):
storage = StorageManager()
storage._storage_dir = storage._get_storage_directory()
expected_dir = Path("/storage/emulated/0/ren_browser")
assert storage._storage_dir == expected_dir
def test_get_storage_directory_android_fallback(self):
"""Test storage directory detection for Android with fallback."""
with ( with (
patch("os.name", "posix"), patch("os.name", "posix"),
patch.dict("os.environ", {"ANDROID_ROOT": "/system"}, clear=True), patch.dict("os.environ", {"ANDROID_ROOT": "/system"}, clear=True),
@@ -80,7 +110,7 @@ class TestStorageManager:
): ):
storage = StorageManager() storage = StorageManager()
storage._storage_dir = storage._get_storage_directory() storage._storage_dir = storage._get_storage_directory()
expected_dir = Path("/storage/emulated/0/Documents/ren_browser") expected_dir = Path("/data/local/tmp/ren_browser")
assert storage._storage_dir == expected_dir assert storage._storage_dir == expected_dir
def test_get_config_path(self): def test_get_config_path(self):