OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
6 | 6 |
| 7 import android.support.test.InstrumentationRegistry; |
7 import android.support.test.filters.MediumTest; | 8 import android.support.test.filters.MediumTest; |
8 | 9 |
| 10 import org.junit.Assert; |
| 11 import org.junit.Before; |
| 12 import org.junit.Rule; |
| 13 import org.junit.Test; |
| 14 import org.junit.runner.RunWith; |
| 15 |
9 import org.chromium.base.ThreadUtils; | 16 import org.chromium.base.ThreadUtils; |
10 import org.chromium.base.test.util.CallbackHelper; | 17 import org.chromium.base.test.util.CallbackHelper; |
| 18 import org.chromium.base.test.util.CommandLineFlags; |
11 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
12 import org.chromium.base.test.util.Restriction; | 20 import org.chromium.base.test.util.Restriction; |
13 import org.chromium.base.test.util.RetryOnFailure; | 21 import org.chromium.base.test.util.RetryOnFailure; |
14 import org.chromium.chrome.browser.tab.EmptyTabObserver; | 22 import org.chromium.chrome.browser.tab.EmptyTabObserver; |
15 import org.chromium.chrome.browser.tab.Tab; | 23 import org.chromium.chrome.browser.tab.Tab; |
16 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 24 import org.chromium.chrome.test.ChromeActivityTestRule; |
| 25 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
17 import org.chromium.chrome.test.util.ChromeRestriction; | 26 import org.chromium.chrome.test.util.ChromeRestriction; |
18 import org.chromium.net.test.EmbeddedTestServer; | 27 import org.chromium.net.test.EmbeddedTestServer; |
19 | 28 |
20 import java.util.concurrent.Callable; | 29 import java.util.concurrent.Callable; |
21 import java.util.concurrent.ExecutionException; | 30 import java.util.concurrent.ExecutionException; |
22 import java.util.concurrent.TimeoutException; | 31 import java.util.concurrent.TimeoutException; |
23 | 32 |
24 /** | 33 /** |
25 * Tests related to the Tab's theme color. | 34 * Tests related to the Tab's theme color. |
26 */ | 35 */ |
27 public class TabThemeTest extends ChromeActivityTestCaseBase<ChromeTabbedActivit
y> { | 36 @RunWith(ChromeJUnit4ClassRunner.class) |
| 37 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, |
| 38 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG}) |
| 39 public class TabThemeTest { |
| 40 @Rule |
| 41 public ChromeActivityTestRule<ChromeActivity> mActivityTestRule = |
| 42 new ChromeActivityTestRule<>(ChromeActivity.class); |
28 | 43 |
29 private static final String TEST_PAGE = "/chrome/test/data/android/simple.ht
ml"; | 44 private static final String TEST_PAGE = "/chrome/test/data/android/simple.ht
ml"; |
30 private static final String THEMED_TEST_PAGE = | 45 private static final String THEMED_TEST_PAGE = |
31 "/chrome/test/data/android/theme_color_test.html"; | 46 "/chrome/test/data/android/theme_color_test.html"; |
32 | 47 |
33 // The theme_color_test.html page uses a pure red theme color. | 48 // The theme_color_test.html page uses a pure red theme color. |
34 private static final int THEME_COLOR = 0xffff0000; | 49 private static final int THEME_COLOR = 0xffff0000; |
35 | 50 |
36 /** | 51 /** |
37 * A WebContentsObserver for watching changes in the theme color. | 52 * A WebContentsObserver for watching changes in the theme color. |
(...skipping 14 matching lines...) Expand all Loading... |
52 | 67 |
53 public CallbackHelper getCallbackHelper() { | 68 public CallbackHelper getCallbackHelper() { |
54 return mCallbackHelper; | 69 return mCallbackHelper; |
55 } | 70 } |
56 | 71 |
57 public int getColor() { | 72 public int getColor() { |
58 return mColor; | 73 return mColor; |
59 } | 74 } |
60 } | 75 } |
61 | 76 |
62 public TabThemeTest() { | 77 @Before |
63 super(ChromeTabbedActivity.class); | 78 public void setUp() throws InterruptedException { |
64 } | 79 mActivityTestRule.startMainActivityOnBlankPage(); |
65 | |
66 @Override | |
67 public void startMainActivity() throws InterruptedException { | |
68 startMainActivityOnBlankPage(); | |
69 } | 80 } |
70 | 81 |
71 /** | 82 /** |
72 * AssertEquals two colors as strings so the text output shows their hex val
ue. | 83 * AssertEquals two colors as strings so the text output shows their hex val
ue. |
73 */ | 84 */ |
74 private void assertColorsEqual(int color1, int color2) { | 85 private void assertColorsEqual(int color1, int color2) { |
75 assertEquals(Integer.toHexString(color1), Integer.toHexString(color2)); | 86 Assert.assertEquals(Integer.toHexString(color1), Integer.toHexString(col
or2)); |
76 } | 87 } |
77 | 88 |
78 /** | 89 /** |
79 * Test that the toolbar has the correct color set. | 90 * Test that the toolbar has the correct color set. |
80 */ | 91 */ |
| 92 @Test |
81 @Feature({"Toolbar-Theme-Color"}) | 93 @Feature({"Toolbar-Theme-Color"}) |
82 @MediumTest | 94 @MediumTest |
83 @Restriction(ChromeRestriction.RESTRICTION_TYPE_PHONE) | 95 @Restriction(ChromeRestriction.RESTRICTION_TYPE_PHONE) |
84 @RetryOnFailure | 96 @RetryOnFailure |
85 public void testThemeColorIsCorrect() | 97 public void testThemeColorIsCorrect() |
86 throws ExecutionException, InterruptedException, TimeoutException { | 98 throws ExecutionException, InterruptedException, TimeoutException { |
| 99 EmbeddedTestServer testServer = EmbeddedTestServer.createAndStartServer( |
| 100 InstrumentationRegistry.getInstrumentation().getContext()); |
87 | 101 |
88 EmbeddedTestServer testServer = EmbeddedTestServer.createAndStartServer( | 102 final Tab tab = mActivityTestRule.getActivity().getActivityTab(); |
89 getInstrumentation().getContext()); | |
90 | |
91 final Tab tab = getActivity().getActivityTab(); | |
92 | 103 |
93 ThemeColorWebContentsObserver colorObserver = new ThemeColorWebContentsO
bserver(); | 104 ThemeColorWebContentsObserver colorObserver = new ThemeColorWebContentsO
bserver(); |
94 CallbackHelper themeColorHelper = colorObserver.getCallbackHelper(); | 105 CallbackHelper themeColorHelper = colorObserver.getCallbackHelper(); |
95 tab.addObserver(colorObserver); | 106 tab.addObserver(colorObserver); |
96 | 107 |
97 // Navigate to a themed page. | 108 // Navigate to a themed page. |
98 int curCallCount = themeColorHelper.getCallCount(); | 109 int curCallCount = themeColorHelper.getCallCount(); |
99 loadUrl(testServer.getURL(THEMED_TEST_PAGE)); | 110 mActivityTestRule.loadUrl(testServer.getURL(THEMED_TEST_PAGE)); |
100 themeColorHelper.waitForCallback(curCallCount, 1); | 111 themeColorHelper.waitForCallback(curCallCount, 1); |
101 assertColorsEqual(THEME_COLOR, tab.getThemeColor()); | 112 assertColorsEqual(THEME_COLOR, tab.getThemeColor()); |
102 | 113 |
103 // Navigate to a native page from a themed page. | 114 // Navigate to a native page from a themed page. |
104 loadUrl("chrome://newtab"); | 115 mActivityTestRule.loadUrl("chrome://newtab"); |
105 // WebContents does not set theme color for native pages, so don't wait
for the call. | 116 // WebContents does not set theme color for native pages, so don't wait
for the call. |
106 int nativePageThemeColor = ThreadUtils.runOnUiThreadBlocking(new Callabl
e<Integer>() { | 117 int nativePageThemeColor = ThreadUtils.runOnUiThreadBlocking(new Callabl
e<Integer>() { |
107 @Override | 118 @Override |
108 public Integer call() { | 119 public Integer call() { |
109 return tab.getNativePage().getThemeColor(); | 120 return tab.getNativePage().getThemeColor(); |
110 } | 121 } |
111 }); | 122 }); |
112 assertColorsEqual(nativePageThemeColor, tab.getThemeColor()); | 123 assertColorsEqual(nativePageThemeColor, tab.getThemeColor()); |
113 | 124 |
114 // Navigate to a themed page from a native page. | 125 // Navigate to a themed page from a native page. |
115 curCallCount = themeColorHelper.getCallCount(); | 126 curCallCount = themeColorHelper.getCallCount(); |
116 loadUrl(testServer.getURL(THEMED_TEST_PAGE)); | 127 mActivityTestRule.loadUrl(testServer.getURL(THEMED_TEST_PAGE)); |
117 themeColorHelper.waitForCallback(curCallCount, 1); | 128 themeColorHelper.waitForCallback(curCallCount, 1); |
118 assertColorsEqual(THEME_COLOR, colorObserver.getColor()); | 129 assertColorsEqual(THEME_COLOR, colorObserver.getColor()); |
119 assertColorsEqual(THEME_COLOR, tab.getThemeColor()); | 130 assertColorsEqual(THEME_COLOR, tab.getThemeColor()); |
120 | 131 |
121 // Navigate to a non-native non-themed page. | 132 // Navigate to a non-native non-themed page. |
122 curCallCount = themeColorHelper.getCallCount(); | 133 curCallCount = themeColorHelper.getCallCount(); |
123 loadUrl(testServer.getURL(TEST_PAGE)); | 134 mActivityTestRule.loadUrl(testServer.getURL(TEST_PAGE)); |
124 themeColorHelper.waitForCallback(curCallCount, 1); | 135 themeColorHelper.waitForCallback(curCallCount, 1); |
125 assertColorsEqual(tab.getDefaultThemeColor(), colorObserver.getColor()); | 136 assertColorsEqual(tab.getDefaultThemeColor(), colorObserver.getColor()); |
126 assertColorsEqual(tab.getDefaultThemeColor(), tab.getThemeColor()); | 137 assertColorsEqual(tab.getDefaultThemeColor(), tab.getThemeColor()); |
127 | 138 |
128 // Navigate to a themed page from a non-native page. | 139 // Navigate to a themed page from a non-native page. |
129 curCallCount = themeColorHelper.getCallCount(); | 140 curCallCount = themeColorHelper.getCallCount(); |
130 loadUrl(testServer.getURL(THEMED_TEST_PAGE)); | 141 mActivityTestRule.loadUrl(testServer.getURL(THEMED_TEST_PAGE)); |
131 themeColorHelper.waitForCallback(curCallCount, 1); | 142 themeColorHelper.waitForCallback(curCallCount, 1); |
132 assertColorsEqual(THEME_COLOR, colorObserver.getColor()); | 143 assertColorsEqual(THEME_COLOR, colorObserver.getColor()); |
133 assertColorsEqual(THEME_COLOR, tab.getThemeColor()); | 144 assertColorsEqual(THEME_COLOR, tab.getThemeColor()); |
134 } | 145 } |
135 } | 146 } |
OLD | NEW |