-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add ExtraWindowEventListener interface #55721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
29 changes: 29 additions & 0 deletions
29
...tive/ReactAndroid/src/main/java/com/facebook/react/interfaces/ExtraWindowEventListener.kt
This file contains hidden or 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,29 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.interfaces | ||
|
|
||
| import android.view.Window | ||
|
|
||
| /** | ||
| * Listener for receiving extra window creation and destruction events. | ||
| * | ||
| * This allows modules to react to new windows being added or removed, such as Dialog windows | ||
| * registered by Modal components. Modules like StatusBarModule can implement this interface to | ||
| * apply their configuration to all active windows. | ||
| * | ||
| * Third-party libraries can both implement this listener and emit window events through | ||
| * [ReactContext.onExtraWindowCreate] and [ReactContext.onExtraWindowDestroy]. | ||
| */ | ||
| public interface ExtraWindowEventListener { | ||
|
|
||
| /** Called when a new [Window] is created (e.g. a Dialog window for a Modal). */ | ||
| public fun onExtraWindowCreate(window: Window) | ||
|
|
||
| /** Called when a [Window] is destroyed (e.g. on Dialog window dismiss). */ | ||
| public fun onExtraWindowDestroy(window: Window) | ||
| } |
This file contains hidden or 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 hidden or 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
124 changes: 124 additions & 0 deletions
124
.../ReactAndroid/src/test/java/com/facebook/react/interfaces/ExtraWindowEventListenerTest.kt
This file contains hidden or 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,124 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.interfaces | ||
|
|
||
| import android.view.Window | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import com.facebook.react.bridge.ReactTestHelper | ||
| import com.facebook.testutils.shadows.ShadowSoLoader | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.never | ||
| import org.mockito.kotlin.times | ||
| import org.mockito.kotlin.verify | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(shadows = [ShadowSoLoader::class]) | ||
| class ExtraWindowEventListenerTest { | ||
| private lateinit var reactContext: ReactApplicationContext | ||
| private lateinit var window: Window | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| reactContext = ReactTestHelper.createCatalystContextForTest() | ||
| window = mock() | ||
| } | ||
|
|
||
| @Test | ||
| fun testOnExtraWindowCreateNotifiesListener() { | ||
| val listener: ExtraWindowEventListener = mock() | ||
|
|
||
| reactContext.addExtraWindowEventListener(listener) | ||
| reactContext.onExtraWindowCreate(window) | ||
|
|
||
| verify(listener, times(1)).onExtraWindowCreate(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testOnExtraWindowDestroyNotifiesListener() { | ||
| val listener: ExtraWindowEventListener = mock() | ||
|
|
||
| reactContext.addExtraWindowEventListener(listener) | ||
| reactContext.onExtraWindowDestroy(window) | ||
|
|
||
| verify(listener, times(1)).onExtraWindowDestroy(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testMultipleListenersAreNotified() { | ||
| val listener1: ExtraWindowEventListener = mock() | ||
| val listener2: ExtraWindowEventListener = mock() | ||
|
|
||
| reactContext.addExtraWindowEventListener(listener1) | ||
| reactContext.addExtraWindowEventListener(listener2) | ||
| reactContext.onExtraWindowCreate(window) | ||
|
|
||
| verify(listener1, times(1)).onExtraWindowCreate(window) | ||
| verify(listener2, times(1)).onExtraWindowCreate(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testRemovedListenerIsNotNotified() { | ||
| val listener: ExtraWindowEventListener = mock() | ||
|
|
||
| reactContext.addExtraWindowEventListener(listener) | ||
| reactContext.removeExtraWindowEventListener(listener) | ||
| reactContext.onExtraWindowCreate(window) | ||
|
|
||
| verify(listener, never()).onExtraWindowCreate(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testOnlyRemovedListenerStopsReceivingEvents() { | ||
| val listener1: ExtraWindowEventListener = mock() | ||
| val listener2: ExtraWindowEventListener = mock() | ||
|
|
||
| reactContext.addExtraWindowEventListener(listener1) | ||
| reactContext.addExtraWindowEventListener(listener2) | ||
| reactContext.removeExtraWindowEventListener(listener1) | ||
| reactContext.onExtraWindowDestroy(window) | ||
|
|
||
| verify(listener1, never()).onExtraWindowDestroy(window) | ||
| verify(listener2, times(1)).onExtraWindowDestroy(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testListenerReceivesBothCreateAndDestroyEvents() { | ||
| val listener: ExtraWindowEventListener = mock() | ||
|
|
||
| reactContext.addExtraWindowEventListener(listener) | ||
| reactContext.onExtraWindowCreate(window) | ||
| reactContext.onExtraWindowDestroy(window) | ||
|
|
||
| verify(listener, times(1)).onExtraWindowCreate(window) | ||
| verify(listener, times(1)).onExtraWindowDestroy(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testNoListenersDoesNotCrash() { | ||
| // Should not throw when no listeners are registered | ||
| reactContext.onExtraWindowCreate(window) | ||
| reactContext.onExtraWindowDestroy(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testDuplicateAddIsIdempotent() { | ||
| val listener: ExtraWindowEventListener = mock() | ||
|
|
||
| reactContext.addExtraWindowEventListener(listener) | ||
| reactContext.addExtraWindowEventListener(listener) | ||
| reactContext.onExtraWindowCreate(window) | ||
|
|
||
| // CopyOnWriteArraySet deduplicates, so listener should only be called once | ||
| verify(listener, times(1)).onExtraWindowCreate(window) | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
...-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ThemedReactContextTest.kt
This file contains hidden or 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,84 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.uimanager | ||
|
|
||
| import android.view.Window | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import com.facebook.react.bridge.ReactTestHelper | ||
| import com.facebook.react.interfaces.ExtraWindowEventListener | ||
| import com.facebook.testutils.shadows.ShadowSoLoader | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.times | ||
| import org.mockito.kotlin.verify | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.RuntimeEnvironment | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(shadows = [ShadowSoLoader::class]) | ||
| class ThemedReactContextTest { | ||
| private lateinit var reactApplicationContext: ReactApplicationContext | ||
| private lateinit var themedReactContext: ThemedReactContext | ||
| private lateinit var window: Window | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| reactApplicationContext = ReactTestHelper.createCatalystContextForTest() | ||
| themedReactContext = | ||
| ThemedReactContext(reactApplicationContext, RuntimeEnvironment.getApplication(), null, -1) | ||
| window = mock() | ||
| } | ||
|
|
||
| @Test | ||
| fun testAddExtraWindowEventListenerDelegatesToReactApplicationContext() { | ||
| val listener: ExtraWindowEventListener = mock() | ||
|
|
||
| themedReactContext.addExtraWindowEventListener(listener) | ||
| // Verify the listener was registered on the underlying context by dispatching an event | ||
| reactApplicationContext.onExtraWindowCreate(window) | ||
|
|
||
| verify(listener, times(1)).onExtraWindowCreate(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testRemoveExtraWindowEventListenerDelegatesToReactApplicationContext() { | ||
| val listener: ExtraWindowEventListener = mock() | ||
|
|
||
| themedReactContext.addExtraWindowEventListener(listener) | ||
| themedReactContext.removeExtraWindowEventListener(listener) | ||
| // After removal via ThemedReactContext, the listener should not be notified | ||
| reactApplicationContext.onExtraWindowCreate(window) | ||
|
|
||
| verify(listener, times(0)).onExtraWindowCreate(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testOnExtraWindowCreateDelegatesToReactApplicationContext() { | ||
| val listener: ExtraWindowEventListener = mock() | ||
|
|
||
| reactApplicationContext.addExtraWindowEventListener(listener) | ||
| // Dispatching via ThemedReactContext should reach listeners on the underlying context | ||
| themedReactContext.onExtraWindowCreate(window) | ||
|
|
||
| verify(listener, times(1)).onExtraWindowCreate(window) | ||
| } | ||
|
|
||
| @Test | ||
| fun testOnExtraWindowDestroyDelegatesToReactApplicationContext() { | ||
| val listener: ExtraWindowEventListener = mock() | ||
|
|
||
| reactApplicationContext.addExtraWindowEventListener(listener) | ||
| // Dispatching via ThemedReactContext should reach listeners on the underlying context | ||
| themedReactContext.onExtraWindowDestroy(window) | ||
|
|
||
| verify(listener, times(1)).onExtraWindowDestroy(window) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.