From 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:47:29 +0200 Subject: Adding upstream version 115.8.0esr. Signed-off-by: Daniel Baumann --- .../tests/gtest/TestMediaKeysEventMac.mm | 136 +++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 dom/media/mediacontrol/tests/gtest/TestMediaKeysEventMac.mm (limited to 'dom/media/mediacontrol/tests/gtest/TestMediaKeysEventMac.mm') diff --git a/dom/media/mediacontrol/tests/gtest/TestMediaKeysEventMac.mm b/dom/media/mediacontrol/tests/gtest/TestMediaKeysEventMac.mm new file mode 100644 index 0000000000..3c8ce8cd9e --- /dev/null +++ b/dom/media/mediacontrol/tests/gtest/TestMediaKeysEventMac.mm @@ -0,0 +1,136 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#import +#import +#import +#import +#import + +#include "gtest/gtest.h" +#include "MediaHardwareKeysEventSourceMac.h" +#include "MediaKeyListenerTest.h" +#include "mozilla/Maybe.h" + +using namespace mozilla::dom; +using namespace mozilla::widget; + +static const int kSystemDefinedEventMediaKeysSubtype = 8; + +static void SendFakeEvent(RefPtr& aSource, int aKeyData) { + NSEvent* event = [NSEvent otherEventWithType:NSEventTypeSystemDefined + location:NSZeroPoint + modifierFlags:0 + timestamp:0 + windowNumber:0 + context:nil + subtype:kSystemDefinedEventMediaKeysSubtype + data1:aKeyData + data2:0]; + aSource->EventTapCallback(nullptr, static_cast(0), [event CGEvent], aSource.get()); +} + +static void NotifyFakeNonMediaKey(RefPtr& aSource, + bool aIsKeyPressed) { + int keyData = 0 | ((aIsKeyPressed ? 0xA : 0xB) << 8); + SendFakeEvent(aSource, keyData); +} + +static void NotifyFakeMediaControlKey(RefPtr& aSource, + MediaControlKey aEvent, bool aIsKeyPressed) { + int keyData = 0; + if (aEvent == MediaControlKey::Playpause) { + keyData = NX_KEYTYPE_PLAY << 16; + } else if (aEvent == MediaControlKey::Nexttrack) { + keyData = NX_KEYTYPE_NEXT << 16; + } else if (aEvent == MediaControlKey::Previoustrack) { + keyData = NX_KEYTYPE_PREVIOUS << 16; + } + keyData |= ((aIsKeyPressed ? 0xA : 0xB) << 8); + SendFakeEvent(aSource, keyData); +} + +static void NotifyKeyPressedMediaKey(RefPtr& aSource, + MediaControlKey aEvent) { + NotifyFakeMediaControlKey(aSource, aEvent, true /* key pressed */); +} + +static void NotifyKeyReleasedMediaKeysEvent(RefPtr& aSource, + MediaControlKey aEvent) { + NotifyFakeMediaControlKey(aSource, aEvent, false /* key released */); +} + +static void NotifyKeyPressedNonMediaKeysEvents(RefPtr& aSource) { + NotifyFakeNonMediaKey(aSource, true /* key pressed */); +} + +static void NotifyKeyReleasedNonMediaKeysEvents(RefPtr& aSource) { + NotifyFakeNonMediaKey(aSource, false /* key released */); +} + +TEST(MediaHardwareKeysEventSourceMac, TestKeyPressedMediaKeysEvent) +{ + RefPtr source = new MediaHardwareKeysEventSourceMac(); + ASSERT_TRUE(source->GetListenersNum() == 0); + + RefPtr listener = new MediaKeyListenerTest(); + source->AddListener(listener.get()); + ASSERT_TRUE(source->GetListenersNum() == 1); + ASSERT_TRUE(!listener->IsReceivedResult()); + + NotifyKeyPressedMediaKey(source, MediaControlKey::Playpause); + ASSERT_TRUE(listener->IsResultEqualTo(MediaControlKey::Playpause)); + + NotifyKeyPressedMediaKey(source, MediaControlKey::Nexttrack); + ASSERT_TRUE(listener->IsResultEqualTo(MediaControlKey::Nexttrack)); + + NotifyKeyPressedMediaKey(source, MediaControlKey::Previoustrack); + ASSERT_TRUE(listener->IsResultEqualTo(MediaControlKey::Previoustrack)); + + source->RemoveListener(listener); + ASSERT_TRUE(source->GetListenersNum() == 0); +} + +TEST(MediaHardwareKeysEventSourceMac, TestKeyReleasedMediaKeysEvent) +{ + RefPtr source = new MediaHardwareKeysEventSourceMac(); + ASSERT_TRUE(source->GetListenersNum() == 0); + + RefPtr listener = new MediaKeyListenerTest(); + source->AddListener(listener.get()); + ASSERT_TRUE(source->GetListenersNum() == 1); + ASSERT_TRUE(!listener->IsReceivedResult()); + + NotifyKeyReleasedMediaKeysEvent(source, MediaControlKey::Playpause); + ASSERT_TRUE(!listener->IsReceivedResult()); + + NotifyKeyReleasedMediaKeysEvent(source, MediaControlKey::Nexttrack); + ASSERT_TRUE(!listener->IsReceivedResult()); + + NotifyKeyReleasedMediaKeysEvent(source, MediaControlKey::Previoustrack); + ASSERT_TRUE(!listener->IsReceivedResult()); + + source->RemoveListener(listener); + ASSERT_TRUE(source->GetListenersNum() == 0); +} + +TEST(MediaHardwareKeysEventSourceMac, TestNonMediaKeysEvent) +{ + RefPtr source = new MediaHardwareKeysEventSourceMac(); + ASSERT_TRUE(source->GetListenersNum() == 0); + + RefPtr listener = new MediaKeyListenerTest(); + source->AddListener(listener.get()); + ASSERT_TRUE(source->GetListenersNum() == 1); + ASSERT_TRUE(!listener->IsReceivedResult()); + + NotifyKeyPressedNonMediaKeysEvents(source); + ASSERT_TRUE(!listener->IsReceivedResult()); + + NotifyKeyReleasedNonMediaKeysEvents(source); + ASSERT_TRUE(!listener->IsReceivedResult()); + + source->RemoveListener(listener); + ASSERT_TRUE(source->GetListenersNum() == 0); +} -- cgit v1.2.3