|
1 | 1 | import sys |
2 | 2 | import unittest |
| 3 | +from unittest.mock import patch |
| 4 | +from tempfile import TemporaryDirectory |
3 | 5 | from pathlib import Path |
4 | 6 | import os |
5 | 7 | import warnings |
|
9 | 11 | import pygame.image |
10 | 12 | import pygame.surfarray |
11 | 13 |
|
12 | | -from pgzero.screen import Screen |
| 14 | +import pgzero.screen as screen |
13 | 15 | from pgzero.loaders import set_root, images |
14 | 16 | from pgzero.rect import Rect, ZRect |
15 | 17 |
|
@@ -96,7 +98,7 @@ def tearDownClass(cls): |
96 | 98 | pygame.display.quit() |
97 | 99 |
|
98 | 100 | def setUp(self): |
99 | | - self.screen = Screen() |
| 101 | + self.screen = screen.Screen() |
100 | 102 | self.screen._set_surface(self.surf) |
101 | 103 | self.screen.clear() |
102 | 104 |
|
@@ -238,6 +240,40 @@ def test_bounds(self): |
238 | 240 | ZRect(0, 0, 200, 200) |
239 | 241 | ) |
240 | 242 |
|
| 243 | + @patch("sys.platform", "win32") |
| 244 | + @patch.dict("os.environ", {"USERPROFILE": r"c:\Users\user"}) |
| 245 | + def test_get_screenshot_path_windows(self): |
| 246 | + r"""Screenshot path on Windows is %USERPROFILE%\Pictures\pgzero.""" |
| 247 | + result_path = screen._get_platform_screenshot_path() |
| 248 | + self.assertEqual(result_path, |
| 249 | + os.path.join(r"c:\Users\user", "Pictures", "pgzero")) |
| 250 | + |
| 251 | + @patch("sys.platform", "linux") |
| 252 | + @patch.dict("os.environ", {"HOME": "/home/user"}) |
| 253 | + def test_get_screenshot_path_linux(self): |
| 254 | + """Screenshot path on Linux or MacOS is ~/Pictures/pgzero.""" |
| 255 | + result_path = screen._get_platform_screenshot_path() |
| 256 | + self.assertEqual(result_path, |
| 257 | + os.path.join("/home/user", "Pictures", "pgzero")) |
| 258 | + |
| 259 | + @patch("sys.platform", "NOTHING") |
| 260 | + def test_get_screenshot_path_other(self): |
| 261 | + """If OS is not supported, CWD is used for screenshots.""" |
| 262 | + result_path = screen._get_platform_screenshot_path() |
| 263 | + self.assertEqual(result_path, |
| 264 | + os.path.join(os.getcwd(), "pgzero_screenshots")) |
| 265 | + |
| 266 | + @patch("sys.platform", "NOTHING") |
| 267 | + def test_take_screenshot(self): |
| 268 | + """Screenshot files are created and have the proper extension.""" |
| 269 | + with TemporaryDirectory("screenshot_testdir") as td: |
| 270 | + os.chdir(td) |
| 271 | + screen._initialize_screenshots(__file__) |
| 272 | + self.screen.screenshot() |
| 273 | + self.assertEqual(len(os.listdir("pgzero_screenshots")), 1) |
| 274 | + ext = os.listdir("pgzero_screenshots")[0].split(".")[-1] |
| 275 | + self.assertEqual(ext, "png") |
| 276 | + |
241 | 277 |
|
242 | 278 | if __name__ == '__main__': |
243 | 279 | unittest.main() |
0 commit comments