1 // Copyright 2014 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.
5 package org.chromium.chrome.browser.dom_distiller;
7 import android.test.suitebuilder.annotation.SmallTest;
8 import android.test.UiThreadTest;
10 import org.chromium.base.ThreadUtils;
11 import org.chromium.base.test.util.Feature;
12 import org.chromium.chrome.browser.profiles.Profile;
13 import org.chromium.chrome.shell.ChromeShellTestBase;
14 import org.chromium.components.dom_distiller.core.DistilledPagePrefs;
15 import org.chromium.components.dom_distiller.core.DomDistillerService;
16 import org.chromium.components.dom_distiller.core.FontFamily;
17 import org.chromium.components.dom_distiller.core.Theme;
18 import org.chromium.content.browser.test.util.UiUtils;
21 * Test class for {@link DistilledPagePrefs}.
23 public class DistilledPagePrefsTest extends ChromeShellTestBase {
25 private DistilledPagePrefs mDistilledPagePrefs;
28 public void setUp() throws Exception {
30 startChromeBrowserProcessSync(getInstrumentation().getTargetContext());
31 getDistilledPagePrefs();
34 private void getDistilledPagePrefs() {
35 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
37 DomDistillerService domDistillerService = DomDistillerServiceFactory.
38 getForProfile(Profile.getLastUsedProfile());
39 mDistilledPagePrefs = domDistillerService.getDistilledPagePrefs();
46 @Feature({"DomDistiller"})
47 public void testGetAndSetTheme() throws InterruptedException {
48 // Check the default theme.
49 assertEquals(Theme.LIGHT, mDistilledPagePrefs.getTheme());
50 // Check that theme can be correctly set.
52 assertEquals(Theme.DARK, mDistilledPagePrefs.getTheme());
53 setTheme(Theme.LIGHT);
54 assertEquals(Theme.LIGHT, mDistilledPagePrefs.getTheme());
55 setTheme(Theme.SEPIA);
56 assertEquals(Theme.SEPIA, mDistilledPagePrefs.getTheme());
60 @Feature({"DomDistiller"})
61 public void testSingleObserverTheme() throws InterruptedException {
62 TestingObserver testObserver = new TestingObserver();
63 mDistilledPagePrefs.addObserver(testObserver);
66 // Assumes that callback does not occur immediately.
67 assertNull(testObserver.getTheme());
68 UiUtils.settleDownUI(getInstrumentation());
69 // Check that testObserver's theme has been updated,
70 assertEquals(Theme.DARK, testObserver.getTheme());
71 mDistilledPagePrefs.removeObserver(testObserver);
75 @Feature({"DomDistiller"})
76 public void testMultipleObserversTheme() throws InterruptedException {
77 TestingObserver testObserverOne = new TestingObserver();
78 mDistilledPagePrefs.addObserver(testObserverOne);
79 TestingObserver testObserverTwo = new TestingObserver();
80 mDistilledPagePrefs.addObserver(testObserverTwo);
82 setTheme(Theme.SEPIA);
83 UiUtils.settleDownUI(getInstrumentation());
84 assertEquals(Theme.SEPIA, testObserverOne.getTheme());
85 assertEquals(Theme.SEPIA, testObserverTwo.getTheme());
86 mDistilledPagePrefs.removeObserver(testObserverOne);
89 UiUtils.settleDownUI(getInstrumentation());
90 // Check that testObserverOne's theme is not changed but testObserverTwo's is.
91 assertEquals(Theme.SEPIA, testObserverOne.getTheme());
92 assertEquals(Theme.DARK, testObserverTwo.getTheme());
93 mDistilledPagePrefs.removeObserver(testObserverTwo);
98 @Feature({"DomDistiller"})
99 public void testGetAndSetFontFamily() throws InterruptedException {
100 // Check the default font family.
101 assertEquals(FontFamily.SANS_SERIF, mDistilledPagePrefs.getFontFamily());
102 // Check that font family can be correctly set.
103 setFontFamily(FontFamily.SERIF);
104 assertEquals(FontFamily.SERIF, mDistilledPagePrefs.getFontFamily());
108 @Feature({"DomDistiller"})
109 public void testSingleObserverFontFamily() throws InterruptedException {
110 TestingObserver testObserver = new TestingObserver();
111 mDistilledPagePrefs.addObserver(testObserver);
113 setFontFamily(FontFamily.SERIF);
114 // Assumes that callback does not occur immediately.
115 assertNull(testObserver.getFontFamily());
116 UiUtils.settleDownUI(getInstrumentation());
117 // Check that testObserver's font family has been updated,
118 assertEquals(FontFamily.SERIF, testObserver.getFontFamily());
119 mDistilledPagePrefs.removeObserver(testObserver);
123 @Feature({"DomDistiller"})
124 public void testMultipleObserversFontFamily() throws InterruptedException {
125 TestingObserver testObserverOne = new TestingObserver();
126 mDistilledPagePrefs.addObserver(testObserverOne);
127 TestingObserver testObserverTwo = new TestingObserver();
128 mDistilledPagePrefs.addObserver(testObserverTwo);
130 setFontFamily(FontFamily.MONOSPACE);
131 UiUtils.settleDownUI(getInstrumentation());
132 assertEquals(FontFamily.MONOSPACE, testObserverOne.getFontFamily());
133 assertEquals(FontFamily.MONOSPACE, testObserverTwo.getFontFamily());
134 mDistilledPagePrefs.removeObserver(testObserverOne);
136 setFontFamily(FontFamily.SERIF);
137 UiUtils.settleDownUI(getInstrumentation());
138 // Check that testObserverOne's font family is not changed but testObserverTwo's is.
139 assertEquals(FontFamily.MONOSPACE, testObserverOne.getFontFamily());
140 assertEquals(FontFamily.SERIF, testObserverTwo.getFontFamily());
141 mDistilledPagePrefs.removeObserver(testObserverTwo);
145 @Feature({"DomDistiller"})
146 public void testRepeatedAddAndDeleteObserver() throws InterruptedException {
147 TestingObserver test = new TestingObserver();
149 // Should successfully add the observer the first time.
150 assertTrue(mDistilledPagePrefs.addObserver(test));
151 // Observer cannot be added again, should return false.
152 assertFalse(mDistilledPagePrefs.addObserver(test));
154 // Delete the observer the first time.
155 assertTrue(mDistilledPagePrefs.removeObserver(test));
156 // Observer cannot be deleted again, should return false.
157 assertFalse(mDistilledPagePrefs.removeObserver(test));
160 private static class TestingObserver implements DistilledPagePrefs.Observer {
161 private FontFamily mFontFamily;
162 private Theme mTheme;
164 public TestingObserver() {}
166 public FontFamily getFontFamily() {
170 public void onChangeFontFamily(FontFamily font) {
174 public Theme getTheme() {
178 public void onChangeTheme(Theme theme) {
183 private void setFontFamily(final FontFamily font) {
184 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
186 mDistilledPagePrefs.setFontFamily(font);
191 private void setTheme(final Theme theme) {
192 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
194 mDistilledPagePrefs.setTheme(theme);