# Path: tests/testapp/models.py
# class BlogStreamPage(Page):
#     author = models.CharField(max_length=255)
#     date = models.DateField("Post date")
#     body = StreamField(
#         [
#             ("heading", blocks.CharBlock(classname="full title", icon="title")),
#             ("paragraph", blocks.RichTextBlock(icon="pilcrow")),
#             ("media", TestMediaBlock(icon="media")),
#             ("video", VideoChooserBlock(icon="media")),
#             ("audio", AudioChooserBlock(icon="media")),
#         ]
#     )
# 
#     featured_media = models.ForeignKey(
#         "wagtailmedia.Media", on_delete=models.PROTECT, related_name="+"
#     )
# 
#     content_panels = Page.content_panels + [
#         FieldPanel("author"),
#         FieldPanel("date"),
#         StreamFieldPanel("body"),
#         MediaChooserPanel("featured_media"),
#         # the following are left here for local testing convenience
#         # MediaChooserPanel("featured_media", media_type="audio"),
#         # MediaChooserPanel("featured_media", media_type="video"),
#     ]


from django.contrib.auth.models import AnonymousUser
from django.core.files.base import ContentFile
from django.test import RequestFactory, TestCase
from django.urls import reverse
from wagtail import VERSION as WAGTAIL_VERSION
from wagtail.admin.edit_handlers import ObjectList
from wagtail.core.models import Page
from tests.testapp.models import BlogStreamPage
from wagtailmedia.edit_handlers import MediaChooserPanel
from wagtailmedia.models import Media
from wagtailmedia.widgets import AdminAudioChooser, AdminMediaChooser, AdminVideoChooser




class MediaChooserPanelTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.request = RequestFactory().get("/")
        cls.request.user = (
            AnonymousUser()
        )  # technically, Anonymous users cannot access the admin

        fake_file = ContentFile("Test")
        fake_file.name = "test.mp3"

        cls.audio = Media.objects.create(
            title="Test audio", duration=1000, file=fake_file, type="audio"
        )

        fake_file = ContentFile("Test")
        fake_file.name = "test.mp4"

        cls.video = Media.objects.create(
            title="Test video", duration=1024, file=fake_file, type="video"
        )

        # a MediaChooserPanel class that works on BlogStreamPage's 'video' field
        cls.edit_handler = ObjectList([MediaChooserPanel("featured_media")]).bind_to(

# Path: tests/testapp/models.py
# class BlogStreamPage(Page):
#     author = models.CharField(max_length=255)
#     date = models.DateField("Post date")
#     body = StreamField(
#         [
#             ("heading", blocks.CharBlock(classname="full title", icon="title")),
#             ("paragraph", blocks.RichTextBlock(icon="pilcrow")),
#             ("media", TestMediaBlock(icon="media")),
#             ("video", VideoChooserBlock(icon="media")),
#             ("audio", AudioChooserBlock(icon="media")),
#         ]
#     )
# 
#     featured_media = models.ForeignKey(
#         "wagtailmedia.Media", on_delete=models.PROTECT, related_name="+"
#     )
# 
#     content_panels = Page.content_panels + [
#         FieldPanel("author"),
#         FieldPanel("date"),
#         StreamFieldPanel("body"),
#         MediaChooserPanel("featured_media"),
#         # the following are left here for local testing convenience
#         # MediaChooserPanel("featured_media", media_type="audio"),
#         # MediaChooserPanel("featured_media", media_type="video"),
#     ]


# Path: tests/test_edit_handlers.py
from django.contrib.auth.models import AnonymousUser
from django.core.files.base import ContentFile
from django.test import RequestFactory, TestCase
from django.urls import reverse
from wagtail import VERSION as WAGTAIL_VERSION
from wagtail.admin.edit_handlers import ObjectList
from wagtail.core.models import Page
from tests.testapp.models import BlogStreamPage
from wagtailmedia.edit_handlers import MediaChooserPanel
from wagtailmedia.models import Media
from wagtailmedia.widgets import AdminAudioChooser, AdminMediaChooser, AdminVideoChooser





class MediaChooserPanelTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.request = RequestFactory().get("/")
        cls.request.user = (
            AnonymousUser()
        )  # technically, Anonymous users cannot access the admin

        fake_file = ContentFile("Test")
        fake_file.name = "test.mp3"

        cls.audio = Media.objects.create(
            title="Test audio", duration=1000, file=fake_file, type="audio"
        )

        fake_file = ContentFile("Test")
        fake_file.name = "test.mp4"

        cls.video = Media.objects.create(
            title="Test video", duration=1024, file=fake_file, type="video"
        )

        # a MediaChooserPanel class that works on BlogStreamPage's 'video' field
        cls.edit_handler = ObjectList([MediaChooserPanel("featured_media")]).bind_to(

            model=BlogStreamPage, request=cls.request