OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.widget; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.DialogInterface; |
| 9 import android.support.test.InstrumentationRegistry; |
| 10 import android.support.test.filters.SmallTest; |
| 11 import android.view.View; |
| 12 import android.view.View.MeasureSpec; |
| 13 import android.view.ViewGroup; |
| 14 import android.widget.LinearLayout; |
| 15 |
| 16 import org.junit.Assert; |
| 17 import org.junit.Test; |
| 18 import org.junit.runner.RunWith; |
| 19 |
| 20 import org.chromium.base.ThreadUtils; |
| 21 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 22 import org.chromium.base.test.util.CallbackHelper; |
| 23 import org.chromium.chrome.R; |
| 24 import org.chromium.chrome.browser.widget.PromoDialog.DialogParams; |
| 25 |
| 26 import java.util.concurrent.Callable; |
| 27 |
| 28 /** |
| 29 * Tests for the PromoDialog and PromoDialogLayout. |
| 30 */ |
| 31 @RunWith(BaseJUnit4ClassRunner.class) |
| 32 public class PromoDialogTest { |
| 33 /** |
| 34 * Creates a PromoDialog. Doesn't call {@link PromoDialog#show} because the
re is no Window to |
| 35 * attach them to, but it does create them and inflate the layouts. |
| 36 */ |
| 37 private static class PromoDialogWrapper implements Callable<PromoDialog> { |
| 38 public final CallbackHelper primaryCallback = new CallbackHelper(); |
| 39 public final CallbackHelper secondaryCallback = new CallbackHelper(); |
| 40 public final PromoDialog dialog; |
| 41 |
| 42 private final Context mContext; |
| 43 private final DialogParams mDialogParams; |
| 44 |
| 45 PromoDialogWrapper(final DialogParams dialogParams) throws Exception { |
| 46 mContext = InstrumentationRegistry.getTargetContext(); |
| 47 mDialogParams = dialogParams; |
| 48 dialog = ThreadUtils.runOnUiThreadBlocking(this); |
| 49 } |
| 50 |
| 51 @Override |
| 52 public PromoDialog call() { |
| 53 PromoDialog dialog = new PromoDialog(mContext) { |
| 54 @Override |
| 55 public DialogParams getDialogParams() { |
| 56 return mDialogParams; |
| 57 } |
| 58 |
| 59 @Override |
| 60 public void onDismiss(DialogInterface dialog) {} |
| 61 |
| 62 @Override |
| 63 public void onClick(View view) { |
| 64 if (view.getId() == R.id.button_primary) { |
| 65 primaryCallback.notifyCalled(); |
| 66 } else if (view.getId() == R.id.button_secondary) { |
| 67 secondaryCallback.notifyCalled(); |
| 68 } |
| 69 } |
| 70 }; |
| 71 dialog.onCreate(null); |
| 72 |
| 73 // Measure the PromoDialogLayout so that the controls have some size
. |
| 74 PromoDialogLayout promoDialogLayout = |
| 75 (PromoDialogLayout) dialog.getWindow().getDecorView().findVi
ewById( |
| 76 R.id.promo_dialog_layout); |
| 77 int widthMeasureSpec = MeasureSpec.makeMeasureSpec(500, MeasureSpec.
EXACTLY); |
| 78 int heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000, MeasureSpe
c.EXACTLY); |
| 79 promoDialogLayout.measure(widthMeasureSpec, heightMeasureSpec); |
| 80 |
| 81 return dialog; |
| 82 } |
| 83 } |
| 84 |
| 85 @Test |
| 86 @SmallTest |
| 87 public void testBasic_Visibility() throws Exception { |
| 88 // Create a full dialog. |
| 89 DialogParams dialogParams = new DialogParams(); |
| 90 dialogParams.drawableResource = R.drawable.search_sogou; |
| 91 dialogParams.headerStringResource = R.string.search_with_sogou; |
| 92 dialogParams.subheaderStringResource = R.string.sogou_explanation; |
| 93 dialogParams.primaryButtonStringResource = R.string.ok; |
| 94 dialogParams.secondaryButtonStringResource = R.string.cancel; |
| 95 dialogParams.footerStringResource = R.string.search_engine_dialog_footer
; |
| 96 checkDialogControlVisibility(dialogParams); |
| 97 |
| 98 // Create a minimal dialog. |
| 99 dialogParams = new DialogParams(); |
| 100 dialogParams.headerStringResource = R.string.search_with_sogou; |
| 101 dialogParams.primaryButtonStringResource = R.string.ok; |
| 102 checkDialogControlVisibility(dialogParams); |
| 103 } |
| 104 |
| 105 /** Confirm that PromoDialogs are constructed with all the elements expected
. */ |
| 106 private void checkDialogControlVisibility(final DialogParams dialogParams) t
hrows Exception { |
| 107 PromoDialogWrapper wrapper = new PromoDialogWrapper(dialogParams); |
| 108 PromoDialog dialog = wrapper.dialog; |
| 109 PromoDialogLayout promoDialogLayout = |
| 110 (PromoDialogLayout) dialog.getWindow().getDecorView().findViewBy
Id( |
| 111 R.id.promo_dialog_layout); |
| 112 |
| 113 View illustration = promoDialogLayout.findViewById(R.id.illustration); |
| 114 View header = promoDialogLayout.findViewById(R.id.header); |
| 115 View subheader = promoDialogLayout.findViewById(R.id.subheader); |
| 116 View primary = promoDialogLayout.findViewById(R.id.button_primary); |
| 117 View secondary = promoDialogLayout.findViewById(R.id.button_secondary); |
| 118 View footer = promoDialogLayout.findViewById(R.id.footer); |
| 119 |
| 120 // Any controls not specified by the DialogParams won't exist. |
| 121 checkControlVisibility(illustration, dialogParams.drawableResource != 0)
; |
| 122 checkControlVisibility(header, dialogParams.headerStringResource != 0); |
| 123 checkControlVisibility(subheader, dialogParams.subheaderStringResource !
= 0); |
| 124 checkControlVisibility(primary, dialogParams.primaryButtonStringResource
!= 0); |
| 125 checkControlVisibility(secondary, dialogParams.secondaryButtonStringReso
urce != 0); |
| 126 checkControlVisibility(footer, dialogParams.footerStringResource != 0); |
| 127 } |
| 128 |
| 129 /** Check if a control should be visible. */ |
| 130 private void checkControlVisibility(View view, boolean shouldBeVisible) { |
| 131 Assert.assertEquals(shouldBeVisible, view != null); |
| 132 if (view != null) { |
| 133 Assert.assertTrue(view.getMeasuredWidth() > 0); |
| 134 Assert.assertTrue(view.getMeasuredHeight() > 0); |
| 135 } |
| 136 } |
| 137 |
| 138 @Test |
| 139 @SmallTest |
| 140 public void testBasic_Orientation() throws Exception { |
| 141 DialogParams dialogParams = new DialogParams(); |
| 142 dialogParams.drawableResource = R.drawable.search_sogou; |
| 143 dialogParams.headerStringResource = R.string.search_with_sogou; |
| 144 dialogParams.subheaderStringResource = R.string.sogou_explanation; |
| 145 dialogParams.primaryButtonStringResource = R.string.ok; |
| 146 dialogParams.secondaryButtonStringResource = R.string.cancel; |
| 147 dialogParams.footerStringResource = R.string.search_engine_dialog_footer
; |
| 148 |
| 149 PromoDialogWrapper wrapper = new PromoDialogWrapper(dialogParams); |
| 150 final PromoDialogLayout promoDialogLayout = |
| 151 (PromoDialogLayout) wrapper.dialog.getWindow().getDecorView().fi
ndViewById( |
| 152 R.id.promo_dialog_layout); |
| 153 LinearLayout flippableLayout = |
| 154 (LinearLayout) promoDialogLayout.findViewById(R.id.full_promo_co
ntent); |
| 155 |
| 156 // Tall screen should keep the illustration above everything else. |
| 157 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 158 @Override |
| 159 public void run() { |
| 160 int widthMeasureSpec = MeasureSpec.makeMeasureSpec(500, MeasureS
pec.EXACTLY); |
| 161 int heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000, Measur
eSpec.EXACTLY); |
| 162 promoDialogLayout.measure(widthMeasureSpec, heightMeasureSpec); |
| 163 } |
| 164 }); |
| 165 Assert.assertEquals(LinearLayout.VERTICAL, flippableLayout.getOrientatio
n()); |
| 166 |
| 167 // Wide screen should move the image left. |
| 168 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 169 @Override |
| 170 public void run() { |
| 171 int widthMeasureSpec = MeasureSpec.makeMeasureSpec(1000, Measure
Spec.EXACTLY); |
| 172 int heightMeasureSpec = MeasureSpec.makeMeasureSpec(500, Measure
Spec.EXACTLY); |
| 173 promoDialogLayout.measure(widthMeasureSpec, heightMeasureSpec); |
| 174 } |
| 175 }); |
| 176 Assert.assertEquals(LinearLayout.HORIZONTAL, flippableLayout.getOrientat
ion()); |
| 177 } |
| 178 |
| 179 @Test |
| 180 @SmallTest |
| 181 public void testBasic_ButtonClicks() throws Exception { |
| 182 DialogParams dialogParams = new DialogParams(); |
| 183 dialogParams.headerStringResource = R.string.search_with_sogou; |
| 184 dialogParams.primaryButtonStringResource = R.string.ok; |
| 185 dialogParams.secondaryButtonStringResource = R.string.cancel; |
| 186 |
| 187 PromoDialogWrapper wrapper = new PromoDialogWrapper(dialogParams); |
| 188 final PromoDialogLayout promoDialogLayout = |
| 189 (PromoDialogLayout) wrapper.dialog.getWindow().getDecorView().fi
ndViewById( |
| 190 R.id.promo_dialog_layout); |
| 191 |
| 192 // Nothing should have been clicked yet. |
| 193 Assert.assertEquals(0, wrapper.primaryCallback.getCallCount()); |
| 194 Assert.assertEquals(0, wrapper.secondaryCallback.getCallCount()); |
| 195 |
| 196 // Only the primary button should register a click. |
| 197 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 198 @Override |
| 199 public void run() { |
| 200 promoDialogLayout.findViewById(R.id.button_primary).performClick
(); |
| 201 } |
| 202 }); |
| 203 Assert.assertEquals(1, wrapper.primaryCallback.getCallCount()); |
| 204 Assert.assertEquals(0, wrapper.secondaryCallback.getCallCount()); |
| 205 |
| 206 // Only the secondary button should register a click. |
| 207 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 208 @Override |
| 209 public void run() { |
| 210 promoDialogLayout.findViewById(R.id.button_secondary).performCli
ck(); |
| 211 } |
| 212 }); |
| 213 Assert.assertEquals(1, wrapper.primaryCallback.getCallCount()); |
| 214 Assert.assertEquals(1, wrapper.secondaryCallback.getCallCount()); |
| 215 } |
| 216 |
| 217 @Test |
| 218 @SmallTest |
| 219 public void testBasic_HeaderBehavior() throws Exception { |
| 220 // Without an illustration, the header View becomes locked to the top of
the layout. |
| 221 { |
| 222 DialogParams dialogParams = new DialogParams(); |
| 223 dialogParams.headerStringResource = R.string.search_with_sogou; |
| 224 dialogParams.primaryButtonStringResource = R.string.ok; |
| 225 |
| 226 PromoDialogWrapper wrapper = new PromoDialogWrapper(dialogParams); |
| 227 PromoDialogLayout promoDialogLayout = |
| 228 (PromoDialogLayout) wrapper.dialog.getWindow().getDecorView(
).findViewById( |
| 229 R.id.promo_dialog_layout); |
| 230 |
| 231 View header = promoDialogLayout.findViewById(R.id.header); |
| 232 Assert.assertEquals(promoDialogLayout.getChildAt(0), header); |
| 233 } |
| 234 |
| 235 // With an illustration, the header View is part of the scrollable conte
nt. |
| 236 { |
| 237 DialogParams dialogParams = new DialogParams(); |
| 238 dialogParams.drawableResource = R.drawable.search_sogou; |
| 239 dialogParams.headerStringResource = R.string.search_with_sogou; |
| 240 dialogParams.primaryButtonStringResource = R.string.ok; |
| 241 |
| 242 PromoDialogWrapper wrapper = new PromoDialogWrapper(dialogParams); |
| 243 PromoDialogLayout promoDialogLayout = |
| 244 (PromoDialogLayout) wrapper.dialog.getWindow().getDecorView(
).findViewById( |
| 245 R.id.promo_dialog_layout); |
| 246 ViewGroup scrollableLayout = |
| 247 (ViewGroup) promoDialogLayout.findViewById(R.id.scrollable_p
romo_content); |
| 248 |
| 249 View header = promoDialogLayout.findViewById(R.id.header); |
| 250 Assert.assertEquals(scrollableLayout.getChildAt(0), header); |
| 251 } |
| 252 } |
| 253 } |
OLD | NEW |