-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase test coverage from 70% to 93%
Also see discovered camera release bug while testing, refs #14
- Loading branch information
1 parent
e069870
commit 65be5a6
Showing
7 changed files
with
82 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from threading import Thread | ||
from unittest import mock | ||
|
||
from kivy.app import App | ||
from kivy.clock import Clock | ||
|
||
from kivy_garden.xcamera.main import CameraApp, main | ||
from tests.test_main import camera_release_workaround, patch_core_camera | ||
|
||
|
||
def patch_picture_taken(): | ||
return mock.patch('kivy_garden.xcamera.main.CameraApp.picture_taken') | ||
|
||
|
||
class TestMain: | ||
""" | ||
Tests the `main` module. | ||
""" | ||
|
||
def test_picture_taken(self): | ||
""" | ||
Checks the `picture_taken()` listener gets called on the running app. | ||
""" | ||
app_thread = Thread(target=main) | ||
app_thread.start() | ||
app = App.get_running_app() | ||
filename = mock.sentinel | ||
Clock.schedule_once( | ||
lambda dt: app.root.ids.xcamera.dispatch( | ||
'on_picture_taken', filename)) | ||
# makes sure app thread is gracefully stopped before asserting | ||
app.stop() | ||
with patch_picture_taken() as m_picture_taken, patch_core_camera(): | ||
app_thread.join() | ||
camera_release_workaround(app) | ||
assert type(app) == CameraApp | ||
assert m_picture_taken.mock_calls == [ | ||
mock.call(app.root.ids.xcamera, filename)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,52 @@ | ||
import os | ||
from threading import Thread | ||
from unittest import mock | ||
|
||
from kivy.app import App | ||
|
||
from kivy_garden.xcamera.main import CameraApp | ||
from main import main | ||
|
||
|
||
def camera_release_workaround(app): | ||
""" | ||
Upstream bug workaround, refs: | ||
https://github.com/kivy-garden/xcamera/issues/14 | ||
""" | ||
app.root.ids.xcamera._camera._device.release() | ||
|
||
|
||
def get_camera_class(): | ||
""" | ||
Continuous integration providers don't have a camera available. | ||
""" | ||
if os.environ.get('CI', False): | ||
Camera = None | ||
else: | ||
from kivy.core.camera import Camera | ||
return Camera | ||
|
||
|
||
def patch_core_camera(): | ||
Camera = get_camera_class() | ||
return mock.patch('kivy.uix.camera.CoreCamera', wraps=Camera) | ||
|
||
|
||
class TestMain: | ||
""" | ||
Tests the `main` module. | ||
""" | ||
|
||
def test_main(self): | ||
with mock.patch('kivy_garden.xcamera.main.CameraApp.run') as m_play: | ||
main() | ||
assert m_play.mock_calls == [mock.call()] | ||
""" | ||
Checks the main starts the app properly. | ||
""" | ||
app_thread = Thread(target=main) | ||
app_thread.start() | ||
app = App.get_running_app() | ||
# makes sure app thread is gracefully stopped before asserting | ||
app.stop() | ||
with patch_core_camera(): | ||
app_thread.join() | ||
camera_release_workaround(app) | ||
assert type(app) == CameraApp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters