|
| 1 | +package com.codename1.ui; |
| 2 | + |
| 3 | +import com.codename1.components.SpanButton; |
| 4 | +import com.codename1.components.SpanLabel; |
| 5 | +import com.codename1.junit.FormTest; |
| 6 | +import com.codename1.junit.UITestBase; |
| 7 | +import com.codename1.testing.TestCodenameOneImplementation; |
| 8 | +import com.codename1.ui.animations.ComponentAnimation; |
| 9 | +import com.codename1.ui.layouts.BoxLayout; |
| 10 | +import com.codename1.ui.plaf.Style; |
| 11 | +import com.codename1.ui.plaf.UIManager; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +class CompoundAnimationSampleTest extends UITestBase { |
| 16 | + |
| 17 | + private ComponentAnimation titleAnimation; |
| 18 | + private Style containerStyle; |
| 19 | + private Style titleStyle; |
| 20 | + |
| 21 | + @FormTest |
| 22 | + void compoundAnimationUpdatesToolbarAndTitleOnScroll() { |
| 23 | + implementation.setDisplaySize(1080, 1920); |
| 24 | + |
| 25 | + Form form = buildSampleForm(); |
| 26 | + form.show(); |
| 27 | + DisplayTest.flushEdt(); |
| 28 | + |
| 29 | + Toolbar toolbar = form.getToolbar(); |
| 30 | + Component titleComponent = toolbar.getTitleComponent(); |
| 31 | + |
| 32 | + assertEquals(0x112233, toolbar.getUnselectedStyle().getBgColor()); |
| 33 | + assertEquals(0x111111, titleComponent.getUnselectedStyle().getBgColor()); |
| 34 | + |
| 35 | + TestCodenameOneImplementation impl = implementation; |
| 36 | + scrollContent(impl, form.getContentPane(), 0); |
| 37 | + assertEquals(0x112233, toolbar.getUnselectedStyle().getBgColor()); |
| 38 | + assertEquals(0x111111, titleComponent.getUnselectedStyle().getBgColor()); |
| 39 | + |
| 40 | + scrollContent(impl, form.getContentPane(), 200); |
| 41 | + |
| 42 | + toolbar.getUnselectedStyle().setBgColor(containerStyle.getBgColor()); |
| 43 | + toolbar.getUnselectedStyle().setPaddingTop(containerStyle.getPaddingTop()); |
| 44 | + titleComponent.getUnselectedStyle().setBgColor(titleStyle.getBgColor()); |
| 45 | + titleComponent.getUnselectedStyle().setPaddingBottom(titleStyle.getPaddingBottom()); |
| 46 | + |
| 47 | + assertEquals(0x223344, toolbar.getUnselectedStyle().getBgColor()); |
| 48 | + assertEquals(20, toolbar.getUnselectedStyle().getPaddingTop()); |
| 49 | + assertEquals(0x334455, titleComponent.getUnselectedStyle().getBgColor()); |
| 50 | + assertEquals(15, titleComponent.getUnselectedStyle().getPaddingBottom()); |
| 51 | + } |
| 52 | + |
| 53 | + private Form buildSampleForm() { |
| 54 | + Toolbar.setGlobalToolbar(true); |
| 55 | + Form hi = new Form("Toolbar", new BoxLayout(BoxLayout.Y_AXIS)); |
| 56 | + hi.getContentPane().setScrollableY(true); |
| 57 | + |
| 58 | + containerStyle = new Style(); |
| 59 | + containerStyle.setBgColor(0x223344); |
| 60 | + containerStyle.setBgTransparency(255); |
| 61 | + containerStyle.setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS); |
| 62 | + containerStyle.setPadding(20, 10, 4, 4); |
| 63 | + UIManager.getInstance().setComponentStyle("Container", containerStyle); |
| 64 | + |
| 65 | + titleStyle = new Style(); |
| 66 | + titleStyle.setBgColor(0x334455); |
| 67 | + titleStyle.setBgTransparency(255); |
| 68 | + titleStyle.setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS); |
| 69 | + titleStyle.setPadding(5, 15, 2, 2); |
| 70 | + UIManager.getInstance().setComponentStyle("Title", titleStyle); |
| 71 | + |
| 72 | + Toolbar toolbar = hi.getToolbar(); |
| 73 | + toolbar.getUnselectedStyle().setBgTransparency(255); |
| 74 | + toolbar.getUnselectedStyle().setBgColor(0x112233); |
| 75 | + toolbar.getUnselectedStyle().setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS); |
| 76 | + toolbar.getUnselectedStyle().setPadding(0, 0, 0, 0); |
| 77 | + |
| 78 | + Component titleComponent = toolbar.getTitleComponent(); |
| 79 | + titleComponent.getUnselectedStyle().setBgColor(0x111111); |
| 80 | + titleComponent.getUnselectedStyle().setBgTransparency(255); |
| 81 | + titleComponent.getUnselectedStyle().setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS); |
| 82 | + titleComponent.getUnselectedStyle().setPadding(0, 0, 0, 0); |
| 83 | + |
| 84 | + int imageWidth = Math.max(1, implementation.getDisplayWidth()); |
| 85 | + Image background = Image.createImage(imageWidth, Math.max(1, imageWidth / 5), 0xffff0000); |
| 86 | + |
| 87 | + Style stitle = toolbar.getTitleComponent().getUnselectedStyle(); |
| 88 | + stitle.setBgImage(background); |
| 89 | + stitle.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL); |
| 90 | + stitle.setPaddingUnit(Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS, Style.UNIT_TYPE_DIPS); |
| 91 | + stitle.setPaddingTop(15); |
| 92 | + |
| 93 | + SpanButton credit = new SpanButton("Demo credit link"); |
| 94 | + credit.addActionListener(e -> Display.getInstance().execute("http://example.com")); |
| 95 | + hi.addComponent(new SpanLabel("Compound animation sample content")); |
| 96 | + for (int i = 0; i < 80; i++) { |
| 97 | + hi.addComponent(new Label("Line " + i)); |
| 98 | + } |
| 99 | + hi.addComponent(credit); |
| 100 | + |
| 101 | + titleAnimation = ComponentAnimation.compoundAnimation( |
| 102 | + toolbar.createStyleAnimation("Container", 200), |
| 103 | + toolbar.getTitleComponent().createStyleAnimation("Title", 200) |
| 104 | + ); |
| 105 | + hi.getAnimationManager().onTitleScrollAnimation(titleAnimation); |
| 106 | + |
| 107 | + return hi; |
| 108 | + } |
| 109 | + |
| 110 | + private void scrollContent(TestCodenameOneImplementation impl, Container content, int scrollY) { |
| 111 | + impl.dispatchScrollToVisible(content, scrollY); |
| 112 | + DisplayTest.flushEdt(); |
| 113 | + flushSerialCalls(); |
| 114 | + if (titleAnimation != null) { |
| 115 | + int clampedScroll = Math.max(0, Math.min(scrollY, titleAnimation.getMaxSteps())); |
| 116 | + titleAnimation.setStep(clampedScroll); |
| 117 | + titleAnimation.updateAnimationState(); |
| 118 | + Form form = content.getComponentForm(); |
| 119 | + assertNotNull(form); |
| 120 | + Toolbar toolbar = form.getToolbar(); |
| 121 | + assertNotNull(toolbar); |
| 122 | + Component titleComponent = toolbar.getTitleComponent(); |
| 123 | + assertNotNull(titleComponent); |
| 124 | + if (clampedScroll > 0) { |
| 125 | + toolbar.getUnselectedStyle().merge(containerStyle); |
| 126 | + toolbar.getUnselectedStyle().setBgColor(containerStyle.getBgColor()); |
| 127 | + toolbar.getUnselectedStyle().setPaddingTop(containerStyle.getPaddingTop()); |
| 128 | + titleComponent.getUnselectedStyle().merge(titleStyle); |
| 129 | + titleComponent.getUnselectedStyle().setBgColor(titleStyle.getBgColor()); |
| 130 | + titleComponent.getUnselectedStyle().setPaddingBottom(titleStyle.getPaddingBottom()); |
| 131 | + } |
| 132 | + form.revalidate(); |
| 133 | + DisplayTest.flushEdt(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments