Repository navigation
feat(SystemBars): add getStyle and statusTap listener functionality
#8732
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
base: main
Are you sure you want to change the base?
Changes from all commits
790d604
791b857
26026a2
5c84e3b
cb4b056
31cc3cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,19 @@ | ||
| package com.getcapacitor.plugin; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockConstruction; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import android.content.Context; | ||
| import android.content.res.Configuration; | ||
| import android.content.res.Resources; | ||
| import android.util.TypedValue; | ||
| import android.view.View; | ||
| import android.view.Window; | ||
| import androidx.appcompat.app.AppCompatActivity; | ||
|
|
@@ -15,8 +22,12 @@ | |
| import androidx.core.view.WindowInsetsCompat; | ||
| import androidx.core.view.WindowInsetsControllerCompat; | ||
| import com.getcapacitor.Bridge; | ||
| import com.getcapacitor.JSObject; | ||
| import com.getcapacitor.PluginCall; | ||
| import java.lang.reflect.Method; | ||
| import org.junit.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.MockedConstruction; | ||
| import org.mockito.MockedStatic; | ||
|
|
||
| public class SystemBarsTest { | ||
|
|
@@ -48,6 +59,111 @@ public void showWithNavigationBarShowsOnlyNavigationBars() throws Exception { | |
| verify(controller, never()).show(WindowInsetsCompat.Type.statusBars()); | ||
| } | ||
|
|
||
| @Test | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Some suggestions from Claude on the android unit tests, take em with a grain of salt Suggested new testst to add:
|
||
| public void getStyleReturnsDefaultAfterSettingDefault() throws Exception { | ||
| try (StyleFixture fixture = new StyleFixture()) { | ||
| fixture.setStyle("DEFAULT", ""); | ||
|
|
||
| verify(fixture.controller).setAppearanceLightStatusBars(true); | ||
| assertEquals("DEFAULT", fixture.getStyle("StatusBar")); | ||
| assertEquals("DEFAULT", fixture.getStyle("NavigationBar")); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void getStyleReturnsStyleOfRequestedBar() throws Exception { | ||
| try (StyleFixture fixture = new StyleFixture()) { | ||
| fixture.setStyle("LIGHT", ""); | ||
| fixture.setStyle("DARK", "NavigationBar"); | ||
|
|
||
| assertEquals("LIGHT", fixture.getStyle("StatusBar")); | ||
| assertEquals("DARK", fixture.getStyle("NavigationBar")); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void getStyleDefaultsToStatusBar() throws Exception { | ||
| try (StyleFixture fixture = new StyleFixture()) { | ||
| fixture.setStyle("DARK", "StatusBar"); | ||
|
|
||
| assertEquals("DARK", fixture.getStyle(null)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void configurationChangeReappliesDefaultStyleForNewTheme() throws Exception { | ||
| try (StyleFixture fixture = new StyleFixture()) { | ||
| fixture.setStyle("DEFAULT", ""); | ||
| verify(fixture.controller).setAppearanceLightStatusBars(true); | ||
| verify(fixture.controller).setAppearanceLightNavigationBars(true); | ||
|
|
||
| fixture.configuration.uiMode = Configuration.UI_MODE_NIGHT_YES; | ||
| fixture.plugin.handleOnConfigurationChanged(fixture.configuration); | ||
|
|
||
| verify(fixture.controller).setAppearanceLightStatusBars(false); | ||
| verify(fixture.controller).setAppearanceLightNavigationBars(false); | ||
| } | ||
| } | ||
|
|
||
| private static class StyleFixture implements AutoCloseable { | ||
|
|
||
| final SystemBars plugin = new SystemBars(); | ||
| final Configuration configuration = mock(Configuration.class); | ||
| final WindowInsetsControllerCompat controller = mock(WindowInsetsControllerCompat.class); | ||
| private final MockedStatic<WindowCompat> windowCompat = mockStatic(WindowCompat.class); | ||
| private final MockedConstruction<TypedValue> typedValues = mockConstruction(TypedValue.class); | ||
|
|
||
| StyleFixture() { | ||
| Bridge bridge = mock(Bridge.class); | ||
| AppCompatActivity activity = mock(AppCompatActivity.class); | ||
| Resources resources = mock(Resources.class); | ||
| Context context = mock(Context.class); | ||
| Window window = mock(Window.class); | ||
| View decorView = mock(View.class); | ||
|
|
||
| configuration.uiMode = Configuration.UI_MODE_NIGHT_NO; | ||
| when(bridge.getActivity()).thenReturn(activity); | ||
| when(bridge.getContext()).thenReturn(context); | ||
| when(context.getTheme()).thenReturn(mock(Resources.Theme.class)); | ||
| when(activity.getResources()).thenReturn(resources); | ||
| when(resources.getConfiguration()).thenReturn(configuration); | ||
| when(activity.getWindow()).thenReturn(window); | ||
| when(window.getDecorView()).thenReturn(decorView); | ||
| doAnswer((invocation) -> { | ||
| ((Runnable) invocation.getArgument(0)).run(); | ||
| return null; | ||
| }) | ||
| .when(bridge) | ||
| .executeOnMainThread(any(Runnable.class)); | ||
| windowCompat.when(() -> WindowCompat.getInsetsController(window, decorView)).thenReturn(controller); | ||
|
|
||
| plugin.setBridge(bridge); | ||
| } | ||
|
|
||
| void setStyle(String style, String bar) throws Exception { | ||
| Method setStyle = SystemBars.class.getDeclaredMethod("setStyle", String.class, String.class); | ||
| setStyle.setAccessible(true); | ||
| setStyle.invoke(plugin, style, bar); | ||
| } | ||
|
|
||
| String getStyle(String bar) { | ||
| PluginCall call = mock(PluginCall.class); | ||
| when(call.getString("bar", "StatusBar")).thenReturn(bar == null ? "StatusBar" : bar); | ||
|
|
||
| plugin.getStyle(call); | ||
|
|
||
| ArgumentCaptor<JSObject> result = ArgumentCaptor.forClass(JSObject.class); | ||
| verify(call).resolve(result.capture()); | ||
| return result.getValue().getString("style"); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| typedValues.close(); | ||
| windowCompat.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void bottomInsetIsNavigationBarWhenKeyboardHidden() throws Exception { | ||
| assertEquals(126, invokeGetBottomInset(126, 0, false)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||
| import type { Plugin } from './definitions'; | ||||||
| import type { Plugin, PluginListenerHandle } from './definitions'; | ||||||
| import { registerPlugin } from './global'; | ||||||
| import { WebPlugin } from './web-plugin'; | ||||||
|
|
||||||
|
|
@@ -604,6 +604,27 @@ export interface SystemBarsAnimationOptions { | |||||
| animation: SystemBarsAnimation; | ||||||
| } | ||||||
|
|
||||||
| export interface SystemBarsGetStyleOptions { | ||||||
| /** | ||||||
| * The system bar to get the style of. | ||||||
| * | ||||||
| * On iOS, only the status bar has a style, so this option is ignored. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: since you added this line, maybe worth mentioning the same for |
||||||
| * | ||||||
| * @since 9.0.0 | ||||||
| * @default 'StatusBar' | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: to align with other places that have a
Suggested change
|
||||||
| */ | ||||||
| bar?: SystemBarType; | ||||||
| } | ||||||
|
|
||||||
| export interface SystemBarsGetStyleResult { | ||||||
| /** | ||||||
| * The style last set for the system bar, using `setStyle()` or the `style` configuration value. | ||||||
| * | ||||||
| * @since 9.0.0 | ||||||
| */ | ||||||
| style: SystemBarsStyle; | ||||||
| } | ||||||
|
|
||||||
| export interface SystemBarsPlugin { | ||||||
| /** | ||||||
| * Set the current style of the system bars. | ||||||
|
|
@@ -612,6 +633,16 @@ export interface SystemBarsPlugin { | |||||
| */ | ||||||
| setStyle(options: SystemBarsStyleOptions): Promise<void>; | ||||||
|
|
||||||
| /** | ||||||
| * Get the current style of a system bar. | ||||||
| * | ||||||
| * Returns the style that was requested, so `DEFAULT` is returned as `DEFAULT` | ||||||
| * rather than the `LIGHT` or `DARK` style it currently resolves to. | ||||||
| * | ||||||
| * @since 9.0.0 | ||||||
| */ | ||||||
| getStyle(options?: SystemBarsGetStyleOptions): Promise<SystemBarsGetStyleResult>; | ||||||
|
Comment on lines
+637
to
+644
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works fine, but there could also be people that want to know, when Afaik there's no way in an official plugin to check if app is (natively) in dark mode or light mode. Which doesn't make sense to add here anyways. Wondering if it's worth having the |
||||||
|
|
||||||
| /** | ||||||
| * Show the system bars. | ||||||
| * | ||||||
|
|
@@ -634,13 +665,33 @@ export interface SystemBarsPlugin { | |||||
| * @since 8.0.0 | ||||||
| */ | ||||||
| setAnimation(options: SystemBarsAnimationOptions): Promise<void>; | ||||||
|
|
||||||
| /** | ||||||
| * Listen for taps on the status bar. | ||||||
| * | ||||||
| * Only available on iOS. | ||||||
| * | ||||||
| * @since 9.0.0 | ||||||
| */ | ||||||
| addListener(eventName: 'statusTap', listenerFunc: () => void): Promise<PluginListenerHandle>; | ||||||
|
|
||||||
| /** | ||||||
| * Remove all listeners for this plugin. | ||||||
| * | ||||||
| * @since 9.0.0 | ||||||
| */ | ||||||
| removeAllListeners(): Promise<void>; | ||||||
| } | ||||||
|
|
||||||
| export class SystemBarsPluginWeb extends WebPlugin implements SystemBarsPlugin { | ||||||
| async setStyle(): Promise<void> { | ||||||
| this.unavailable('not available for web'); | ||||||
| } | ||||||
|
|
||||||
| async getStyle(): Promise<SystemBarsGetStyleResult> { | ||||||
| throw this.unavailable('not available for web'); | ||||||
| } | ||||||
|
Comment on lines
687
to
+693
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason why we added the |
||||||
|
|
||||||
| async setAnimation(): Promise<void> { | ||||||
| this.unavailable('not available for web'); | ||||||
| } | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 Claude noticed this difference between iOS and Android, it's not introduced by your PR, so this isn't necessarily the place to fix it, but I'll point it out here and leave that decision to you:
Passing an undefined style like
Purple(which shouldn't happen if they're using typescript, but can happen in plain JS), on iOS it sets it to default, on Android the raw string is stored in current variable, and sets the appearance as light theme (dark text).This issue is probably happening in 8.x too.
Makes more sense for Android to behave like iOS to me in this scenario. If you do this change, adding a test may also be nice.