Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(916)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/ntp/NativePageFactoryTest.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.ntp;
6
7 import android.app.Activity;
8 import android.test.InstrumentationTestCase;
9 import android.test.UiThreadTest;
10 import android.test.suitebuilder.annotation.SmallTest;
11 import android.view.View;
12
13 import org.chromium.chrome.browser.NativePage;
14 import org.chromium.chrome.browser.Tab;
15 import org.chromium.chrome.browser.UrlConstants;
16 import org.chromium.chrome.browser.ntp.NativePageFactory.NativePageType;
17 import org.chromium.chrome.browser.tabmodel.TabModelSelector;
18
19 /**
20 * Tests public methods in NativePageFactory.
21 */
22 public class NativePageFactoryTest extends InstrumentationTestCase {
23
24 private static class MockNativePage implements NativePage {
25 public final NativePageType type;
26 public int updateForUrlCalls;
27
28 public MockNativePage(NativePageType type) {
29 this.type = type;
30 }
31
32 @Override
33 public void updateForUrl(String url) {
34 updateForUrlCalls++;
35 }
36
37 @Override
38 public String getUrl() {
39 return null;
40 }
41
42 @Override
43 public String getHost() {
44 switch (type) {
45 case NTP:
46 return UrlConstants.NTP_HOST;
47 case BOOKMARKS:
48 return UrlConstants.BOOKMARKS_HOST;
49 case RECENT_TABS:
50 return UrlConstants.RECENT_TABS_HOST;
51 default:
52 fail("Unexpected NativePageType: " + type);
53 return null;
54 }
55 }
56
57 @Override
58 public void destroy() {}
59
60 @Override
61 public String getTitle() {
62 return null;
63 }
64
65 @Override
66 public int getBackgroundColor() {
67 return 0;
68 }
69
70 @Override
71 public View getView() {
72 return null;
73 }
74 }
75
76 private static class MockNativePageBuilder extends NativePageFactory.NativeP ageBuilder {
77 @Override
78 public NativePage buildNewTabPage(Activity activity, Tab tab,
79 TabModelSelector tabModelSelector) {
80 return new MockNativePage(NativePageType.NTP);
81 }
82
83 @Override
84 public NativePage buildBookmarksPage(Activity activity, Tab tab,
85 TabModelSelector tabModelSelector) {
86 return new MockNativePage(NativePageType.BOOKMARKS);
87 }
88
89 @Override
90 public NativePage buildRecentTabsPage(Activity activity, Tab tab) {
91 return new MockNativePage(NativePageType.RECENT_TABS);
92 }
93 }
94
95 private static class UrlCombo {
96 public String url;
97 public NativePageType expectedType;
98
99 public UrlCombo(String url, NativePageType expectedType) {
100 this.url = url;
101 this.expectedType = expectedType;
102 }
103 }
104
105 private static final UrlCombo[] VALID_URLS = {
106 new UrlCombo("chrome-native://newtab", NativePageType.NTP),
107 new UrlCombo("chrome-native://newtab/", NativePageType.NTP),
108 new UrlCombo("chrome-native://bookmarks", NativePageType.BOOKMARKS),
109 new UrlCombo("chrome-native://bookmarks/", NativePageType.BOOKMARKS),
110 new UrlCombo("chrome-native://bookmarks/#245", NativePageType.BOOKMARKS) ,
111 new UrlCombo("chrome-native://recent-tabs", NativePageType.RECENT_TABS),
112 new UrlCombo("chrome-native://recent-tabs/", NativePageType.RECENT_TABS) ,
113 };
114
115 private static final String[] INVALID_URLS = {
116 null,
117 "",
118 "newtab",
119 "newtab@google.com:80",
120 "/newtab",
121 "://newtab",
122 "chrome://",
123 "chrome://newtab",
124 "chrome://newtab#bookmarks",
125 "chrome://newtab/#open_tabs",
126 "chrome://recent-tabs",
127 "chrome://most_visited",
128 "chrome-native://",
129 "chrome-native://newtablet",
130 "chrome-native://bookmarks-inc",
131 "chrome-native://recent_tabs",
132 "chrome-native://recent-tabswitcher",
133 "chrome-native://most_visited",
134 "chrome-native://astronaut",
135 "chrome-internal://newtab",
136 "french-fries://newtab",
137 "http://bookmarks",
138 "https://recent-tabs",
139 "newtab://recent-tabs",
140 "recent-tabs bookmarks",
141 };
142
143 private boolean isValidInIncognito(UrlCombo urlCombo) {
144 return urlCombo.expectedType != NativePageType.RECENT_TABS;
145 }
146
147 /**
148 * Ensures that NativePageFactory.isNativePageUrl() returns true for native page URLs.
149 */
150 private void runTestPostiveIsNativePageUrl() {
151 for (UrlCombo urlCombo : VALID_URLS) {
152 String url = urlCombo.url;
153 assertTrue(url, NativePageFactory.isNativePageUrl(url, false));
154 if (isValidInIncognito(urlCombo)) {
155 assertTrue(url, NativePageFactory.isNativePageUrl(url, true));
156 }
157 }
158 }
159
160 /**
161 * Ensures that NativePageFactory.isNativePageUrl() returns false for URLs t hat don't
162 * correspond to a native page.
163 */
164 private void runTestNegativeIsNativePageUrl() {
165 for (String invalidUrl : INVALID_URLS) {
166 assertFalse(invalidUrl, NativePageFactory.isNativePageUrl(invalidUrl , false));
167 assertFalse(invalidUrl, NativePageFactory.isNativePageUrl(invalidUrl , true));
168 }
169 }
170
171 /**
172 * Ensures that NativePageFactory.createNativePageForURL() returns a native page of the right
173 * type and reuses the candidate page if it's the right type.
174 */
175 private void runTestCreateNativePage() {
176 NativePageType[] candidateTypes = new NativePageType[] { NativePageType. NONE,
177 NativePageType.NTP, NativePageType.BOOKMARKS, NativePageType.RECENT_ TABS };
178 for (boolean isIncognito : new boolean[] {true, false}) {
179 for (UrlCombo urlCombo : VALID_URLS) {
180 if (isIncognito && !isValidInIncognito(urlCombo)) continue;
181 for (NativePageType candidateType : candidateTypes) {
182 MockNativePage candidate = candidateType == NativePageType.N ONE ? null
183 : new MockNativePage(candidateType);
184 MockNativePage page = (MockNativePage) NativePageFactory.cre ateNativePageForURL(
185 urlCombo.url, candidate, null, null, null, isIncogni to);
186 String debugMessage = String.format(
187 "Failed test case: isIncognito=%s, urlCombo={%s,%s}, candidateType=%s",
188 isIncognito, urlCombo.url, urlCombo.expectedType, ca ndidateType);
189 assertNotNull(debugMessage, page);
190 assertEquals(debugMessage, 1, page.updateForUrlCalls);
191 assertEquals(debugMessage, urlCombo.expectedType, page.type) ;
192 if (candidateType == urlCombo.expectedType) {
193 assertSame(debugMessage, candidate, page);
194 } else {
195 assertNotSame(debugMessage, candidate, page);
196 }
197 }
198 }
199 }
200 }
201
202 /**
203 * Ensures that NativePageFactory.createNativePageForURL() returns null for URLs that don't
204 * correspond to a native page.
205 */
206 private void runTestCreateNativePageWithInvalidUrl() {
207 for (UrlCombo urlCombo : VALID_URLS) {
208 if (!isValidInIncognito(urlCombo)) {
209 assertNull(urlCombo.url, NativePageFactory.createNativePageForUR L(urlCombo.url,
210 null, null, null, null, true));
211 }
212 }
213 for (boolean isIncognito : new boolean[] {true, false}) {
214 for (String invalidUrl : INVALID_URLS) {
215 assertNull(invalidUrl, NativePageFactory.createNativePageForURL( invalidUrl, null,
216 null, null, null, isIncognito));
217 }
218 }
219 }
220
221 /**
222 * Runs all the runTest* methods defined above.
223 */
224 @SmallTest
225 @UiThreadTest
226 public void testNativePageFactory() {
227 NativePageFactory.setNativePageBuilderForTesting(new MockNativePageBuild er());
228 runTestPostiveIsNativePageUrl();
229 runTestNegativeIsNativePageUrl();
230 runTestCreateNativePage();
231 runTestCreateNativePageWithInvalidUrl();
232 }
233 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698