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