|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.tests; |
| 9 | + |
| 10 | +import android.content.res.Resources; |
| 11 | +import android.util.DisplayMetrics; |
| 12 | +import android.util.TypedValue; |
| 13 | +import android.view.View; |
| 14 | +import android.view.ViewGroup; |
| 15 | +import android.widget.FrameLayout; |
| 16 | +import android.widget.ProgressBar; |
| 17 | +import com.facebook.react.ReactRootView; |
| 18 | +import com.facebook.react.bridge.CatalystInstance; |
| 19 | +import com.facebook.react.bridge.JavaScriptModule; |
| 20 | +import com.facebook.react.bridge.UiThreadUtil; |
| 21 | +import com.facebook.react.modules.appstate.AppStateModule; |
| 22 | +import com.facebook.react.modules.deviceinfo.DeviceInfoModule; |
| 23 | +import com.facebook.react.modules.systeminfo.AndroidInfoModule; |
| 24 | +import com.facebook.react.testing.FakeWebSocketModule; |
| 25 | +import com.facebook.react.testing.ReactIntegrationTestCase; |
| 26 | +import com.facebook.react.testing.ReactTestHelper; |
| 27 | +import com.facebook.react.uimanager.UIManagerModule; |
| 28 | +import com.facebook.react.uimanager.ViewManager; |
| 29 | +import com.facebook.react.views.progressbar.ReactProgressBarViewManager; |
| 30 | +import com.facebook.react.views.view.ReactViewManager; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test to verify that Progress bar renders as a view of the right size |
| 37 | + */ |
| 38 | +public class ProgressBarTestCase extends ReactIntegrationTestCase { |
| 39 | + |
| 40 | + // Has same order of progressBars in ProgressBarTestModule |
| 41 | + private static final String[] styleList = |
| 42 | + new String[] {"Horizontal", "Small", "Large", "Inverse", "SmallInverse", "LargeInverse"}; |
| 43 | + private static final HashMap<String, Integer> styles = new HashMap<String, Integer>(); |
| 44 | + |
| 45 | + static { |
| 46 | + styles.put("Horizontal", android.R.attr.progressBarStyleHorizontal); |
| 47 | + styles.put("Small", android.R.attr.progressBarStyleSmall); |
| 48 | + styles.put("Large", android.R.attr.progressBarStyleLarge); |
| 49 | + styles.put("Inverse", android.R.attr.progressBarStyleInverse); |
| 50 | + styles.put("SmallInverse", android.R.attr.progressBarStyleSmallInverse); |
| 51 | + styles.put("LargeInverse", android.R.attr.progressBarStyleLargeInverse); |
| 52 | +} |
| 53 | + |
| 54 | + private static interface ProgressBarTestModule extends JavaScriptModule { |
| 55 | + public void renderProgressBarApplication(int rootTag); |
| 56 | + } |
| 57 | + |
| 58 | + private UIManagerModule mUIManager; |
| 59 | + private CatalystInstance mInstance; |
| 60 | + private ReactRootView mRootView; |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void setUp() throws Exception { |
| 64 | + super.setUp(); |
| 65 | + |
| 66 | + List<ViewManager> viewManagers = Arrays.<ViewManager>asList( |
| 67 | + new ReactViewManager(), |
| 68 | + new ReactProgressBarViewManager()); |
| 69 | + mUIManager = |
| 70 | + new UIManagerModule(getContext(), viewManagers, 0); |
| 71 | + UiThreadUtil.runOnUiThread( |
| 72 | + new Runnable() { |
| 73 | + @Override |
| 74 | + public void run() { |
| 75 | + mUIManager.onHostResume(); |
| 76 | + } |
| 77 | + }); |
| 78 | + waitForIdleSync(); |
| 79 | + |
| 80 | + mInstance = ReactTestHelper.catalystInstanceBuilder(this) |
| 81 | + .addNativeModule(mUIManager) |
| 82 | + .addNativeModule(new AndroidInfoModule(getContext())) |
| 83 | + .addNativeModule(new DeviceInfoModule(getContext())) |
| 84 | + .addNativeModule(new AppStateModule(getContext())) |
| 85 | + .addNativeModule(new FakeWebSocketModule()) |
| 86 | + .build(); |
| 87 | + |
| 88 | + mRootView = new ReactRootView(getContext()); |
| 89 | + DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); |
| 90 | + mRootView.setLayoutParams( |
| 91 | + new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels)); |
| 92 | + int rootTag = mUIManager.addRootView(mRootView); |
| 93 | + mInstance.getJSModule(ProgressBarTestModule.class).renderProgressBarApplication(rootTag); |
| 94 | + waitForBridgeAndUIIdle(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Test that the sizes of the progressBars are setup correctly |
| 99 | + */ |
| 100 | + public void testProgressBarSizes() { |
| 101 | + for (String style : styleList) { |
| 102 | + ProgressBar newProgressBar = |
| 103 | + new ProgressBar(getContext(), null, styles.get(style)); |
| 104 | + final int spec = View.MeasureSpec.makeMeasureSpec( |
| 105 | + ViewGroup.LayoutParams.WRAP_CONTENT, |
| 106 | + View.MeasureSpec.UNSPECIFIED); |
| 107 | + newProgressBar.measure(spec, spec); |
| 108 | + final int expectedHeight = newProgressBar.getMeasuredHeight(); |
| 109 | + |
| 110 | + // verify correct size of view containing ProgressBar |
| 111 | + View viewContainingProgressBar = getViewByTestId(mRootView, style); |
| 112 | + assertEquals(expectedHeight, viewContainingProgressBar.getHeight()); |
| 113 | + |
| 114 | + assertTrue(((ViewGroup) viewContainingProgressBar).getChildAt(0) instanceof ProgressBar); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public void testProgressBarWidth() { |
| 119 | + View viewContainingProgressBar = getViewByTestId(mRootView, "Horizontal200"); |
| 120 | + assertEquals(viewContainingProgressBar.getWidth(), dpToPixels(200)); |
| 121 | + ProgressBar progressBar = (ProgressBar) ((ViewGroup) viewContainingProgressBar).getChildAt(0); |
| 122 | + assertEquals(progressBar.getWidth(), dpToPixels(200)); |
| 123 | + } |
| 124 | + |
| 125 | + private int dpToPixels(int dp) { |
| 126 | + Resources r = getContext().getResources(); |
| 127 | + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); |
| 128 | + } |
| 129 | +} |
0 commit comments