OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.toolbar; |
| 6 |
| 7 import android.graphics.Color; |
| 8 import android.test.FlakyTest; |
| 9 import android.test.suitebuilder.annotation.SmallTest; |
| 10 import android.text.TextUtils; |
| 11 |
| 12 import com.google.android.apps.chrome.R; |
| 13 |
| 14 import org.chromium.base.ThreadUtils; |
| 15 import org.chromium.base.test.util.Feature; |
| 16 import org.chromium.base.test.util.UrlUtils; |
| 17 import org.chromium.chrome.test.DocumentActivityTestBase; |
| 18 import org.chromium.chrome.test.util.DisableInTabbedMode; |
| 19 import org.chromium.content.browser.InterstitialPageDelegateAndroid; |
| 20 import org.chromium.content.browser.test.util.Criteria; |
| 21 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 22 |
| 23 import java.util.concurrent.Callable; |
| 24 |
| 25 /** |
| 26 * Contains tests for the brand color feature. |
| 27 */ |
| 28 @DisableInTabbedMode |
| 29 public class BrandColorTest extends DocumentActivityTestBase { |
| 30 private static final String BRAND_COLOR_1 = "#482329"; |
| 31 private static final String BRAND_COLOR_2 = "#505050"; |
| 32 private static final String INTERSTITIAL_HTML = "<html><head></head><body>te
st</body></html>"; |
| 33 |
| 34 private ToolbarPhone mToolbar; |
| 35 private ToolbarDataProvider mToolbarDataProvider; |
| 36 |
| 37 private static String getUrlWithBrandColor(String brandColor) { |
| 38 String brandColorMetaTag = TextUtils.isEmpty(brandColor) |
| 39 ? "" : "<meta name='theme-color' content='" + brandColor + "'>"; |
| 40 return UrlUtils.encodeHtmlDataUri( |
| 41 "<html>" |
| 42 + " <head>" |
| 43 + " " + brandColorMetaTag |
| 44 + " </head>" |
| 45 + " <body>" |
| 46 + " Theme color set to " + brandColor |
| 47 + " </body>" |
| 48 + "</html>"); |
| 49 } |
| 50 |
| 51 @Override |
| 52 public void startMainActivity() { |
| 53 // Don't launch activity automatically. |
| 54 } |
| 55 |
| 56 private void checkForBrandColor(final int brandColor) { |
| 57 checkNoColorTransition(); |
| 58 ThreadUtils.postOnUiThread(new Runnable() { |
| 59 @Override |
| 60 public void run() { |
| 61 assertEquals("The data provider doesn't contain the right color"
, |
| 62 brandColor, mToolbarDataProvider.getPrimaryColor()); |
| 63 assertEquals("The toolbar view doesn't contain the right color", |
| 64 brandColor, mToolbar.getBackgroundDrawable().getColor())
; |
| 65 assertEquals("The overlay drawable doesn't contain the right col
or", |
| 66 brandColor, mToolbar.getOverlayDrawable().getColor()); |
| 67 } |
| 68 }); |
| 69 } |
| 70 |
| 71 private void checkNoColorTransition() { |
| 72 try { |
| 73 CriteriaHelper.pollForUIThreadCriteria(new Criteria() { |
| 74 @Override |
| 75 public boolean isSatisfied() { |
| 76 return mToolbarDataProvider.getPrimaryColor() |
| 77 == mToolbar.getBackgroundDrawable().getColor(); |
| 78 } |
| 79 }); |
| 80 } catch (InterruptedException e) { |
| 81 fail(); |
| 82 } |
| 83 } |
| 84 |
| 85 @Override |
| 86 protected void startMainActivityWithURL(String url) throws InterruptedExcept
ion { |
| 87 super.startMainActivityWithURL(url); |
| 88 mToolbar = (ToolbarPhone) getActivity().findViewById(R.id.toolbar); |
| 89 mToolbarDataProvider = mToolbar.getToolbarDataProvider(); |
| 90 } |
| 91 |
| 92 /** |
| 93 * Test for having default primary color working correctly. |
| 94 */ |
| 95 @SmallTest |
| 96 @Feature({"Omnibox"}) |
| 97 public void testNoBrandColor() throws InterruptedException { |
| 98 startMainActivityWithURL(getUrlWithBrandColor("")); |
| 99 checkForBrandColor(getActivity().getResources().getColor(R.color.default
_primary_color)); |
| 100 } |
| 101 |
| 102 /** |
| 103 * Test for adding a brand color for a url. |
| 104 */ |
| 105 @SmallTest |
| 106 @Feature({"Omnibox"}) |
| 107 public void testBrandColorNoAlpha() throws InterruptedException { |
| 108 startMainActivityWithURL(getUrlWithBrandColor(BRAND_COLOR_1)); |
| 109 checkForBrandColor(Color.parseColor(BRAND_COLOR_1)); |
| 110 } |
| 111 |
| 112 /** |
| 113 * Test to make sure onLoadStarted doesn't reset the brand color. |
| 114 */ |
| 115 @SmallTest |
| 116 @Feature({"Omnibox"}) |
| 117 public void testBrandColorWithLoadStarted() throws InterruptedException { |
| 118 startMainActivityWithURL(getUrlWithBrandColor(BRAND_COLOR_1)); |
| 119 ThreadUtils.postOnUiThread(new Runnable() { |
| 120 @Override |
| 121 public void run() { |
| 122 getActivity().getActivityTab() |
| 123 .getChromeWebContentsDelegateAndroid().onLoadStarted(); |
| 124 } |
| 125 }); |
| 126 checkForBrandColor(Color.parseColor(BRAND_COLOR_1)); |
| 127 } |
| 128 |
| 129 /** |
| 130 * Test for checking navigating to new brand color updates correctly. |
| 131 * |
| 132 * Bug: http://crbug.com/474414 |
| 133 * @SmallTest |
| 134 * @Feature({"Omnibox"}) |
| 135 */ |
| 136 @FlakyTest |
| 137 public void testNavigatingToNewBrandColor() throws InterruptedException { |
| 138 startMainActivityWithURL(getUrlWithBrandColor(BRAND_COLOR_1)); |
| 139 checkForBrandColor(Color.parseColor(BRAND_COLOR_1)); |
| 140 loadUrl(getUrlWithBrandColor(BRAND_COLOR_2)); |
| 141 checkForBrandColor(Color.parseColor(BRAND_COLOR_2)); |
| 142 } |
| 143 |
| 144 /** |
| 145 * Test for interstitial page loads resetting brand color. |
| 146 */ |
| 147 @SmallTest |
| 148 @Feature({"Omnibox"}) |
| 149 public void testBrandColorInterstitial() throws InterruptedException { |
| 150 final String brandColorUrl = getUrlWithBrandColor(BRAND_COLOR_1); |
| 151 startMainActivityWithURL(brandColorUrl); |
| 152 checkForBrandColor(Color.parseColor(BRAND_COLOR_1)); |
| 153 final InterstitialPageDelegateAndroid delegate = |
| 154 new InterstitialPageDelegateAndroid(INTERSTITIAL_HTML); |
| 155 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 156 @Override |
| 157 public void run() { |
| 158 getActivity().getActivityTab().getWebContents().showInterstitial
Page( |
| 159 brandColorUrl, delegate.getNative()); |
| 160 } |
| 161 }); |
| 162 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 163 @Override |
| 164 public boolean isSatisfied() { |
| 165 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable
<Boolean>() { |
| 166 @Override |
| 167 public Boolean call() throws Exception { |
| 168 return getActivity().getActivityTab().isShowingInterstit
ialPage(); |
| 169 } |
| 170 }); |
| 171 } |
| 172 })); |
| 173 checkForBrandColor(getActivity().getResources().getColor(R.color.default
_primary_color)); |
| 174 } |
| 175 } |
OLD | NEW |