Upstream oodles of Chrome for Android code into Chromium.
[chromium-blink-merge.git] / chrome / android / javatests / src / org / chromium / chrome / browser / contextualsearch / ContextualSearchFakeServer.java
blobd16e0656c0928010e9f9765120810bb1d2ca2b52
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.
5 package org.chromium.chrome.browser.contextualsearch;
7 import org.chromium.base.VisibleForTesting;
9 import java.net.MalformedURLException;
10 import java.net.URL;
12 import javax.annotation.Nullable;
14 /**
15 * Implements a fake Contextual Search server, for testing purposes.
16 * TODO(donnd): add more functionality to this class once the overall approach has been validated.
17 * TODO(donnd): rename this class when we refactor and rename the interface it implements. Should
18 * be something like ContextualSearchFakeEnvironment.
20 @VisibleForTesting
21 class ContextualSearchFakeServer implements ContextualSearchNetworkCommunicator {
23 private final ContextualSearchNetworkCommunicator mBaseManager;
24 private String mLoadedUrl;
25 private String mSearchTermRequested;
26 private boolean mShouldUseHttps;
27 private int mLoadedUrlCount;
28 private boolean mIsSearchContentViewCreated;
30 /**
31 * Constructs a fake Contextual Search server that will callback to the given baseManager.
32 * @param baseManager The manager to call back to for server responses.
34 @VisibleForTesting
35 ContextualSearchFakeServer(ContextualSearchNetworkCommunicator baseManager) {
36 mBaseManager = baseManager;
39 @Override
40 public void startSearchTermResolutionRequest(String selection) {
41 mLoadedUrl = null;
42 mSearchTermRequested = selection;
45 @Override
46 public void continueSearchTermResolutionRequest() {
47 mLoadedUrl = null;
50 @Override
51 public void loadUrl(String url) {
52 mLoadedUrl = url;
53 mLoadedUrlCount++;
54 // This will not actually load a URL because no Search Content View will be created
55 // when under test -- see comments in createNewSearchContentView.
56 mBaseManager.loadUrl(url);
59 @Override
60 public void handleSearchTermResolutionResponse(boolean isNetworkUnavailable, int responseCode,
61 String searchTerm, String displayText, String alternateTerm, boolean doPreventPreload) {
62 mBaseManager.handleSearchTermResolutionResponse(isNetworkUnavailable, responseCode,
63 searchTerm, displayText, alternateTerm, doPreventPreload);
66 @Override
67 @Nullable public URL getBasePageUrl() {
68 URL baseUrl = mBaseManager.getBasePageUrl();
69 if (mShouldUseHttps && baseUrl != null) {
70 try {
71 return new URL(baseUrl.toString().replace("http://", "https://"));
72 } catch (MalformedURLException e) {
73 // TODO(donnd): Auto-generated catch block
74 e.printStackTrace();
77 return baseUrl;
80 @Override
81 public void handleDidNavigateMainFrame(String url, int httpResultCode) {
82 mBaseManager.handleDidNavigateMainFrame(url, httpResultCode);
85 @Override
86 public void createNewSearchContentView() {
87 mIsSearchContentViewCreated = true;
88 // Don't call the super method because that will cause loadUrl to make a live request!
89 // This method is only called by loadUrl, which will subseqently check if the CV was
90 // successfully created before issuing the search request.
91 // TODO(donnd): This is brittle, improve! E.g. make live requests to a local server.
94 @Override
95 public void destroySearchContentView() {
96 mIsSearchContentViewCreated = false;
97 mBaseManager.destroySearchContentView();
101 * @return The search term requested, or {@code null} if no search term was requested.
103 @VisibleForTesting
104 String getSearchTermRequested() {
105 return mSearchTermRequested;
109 * @return the loaded search result page URL if any was requested.
111 @VisibleForTesting
112 String getLoadedUrl() {
113 return mLoadedUrl;
117 * @return The number of times we loaded a URL in the Content View.
119 @VisibleForTesting
120 int loadedUrlCount() {
121 return mLoadedUrlCount;
125 * Sets whether to return an HTTPS URL instead of HTTP, from {@link #getBasePageUrl}.
127 @VisibleForTesting
128 void setShouldUseHttps(boolean setting) {
129 mShouldUseHttps = setting;
133 * @return Whether we tried to create the Search Content View.
135 @VisibleForTesting
136 boolean isSearchContentViewCreated() {
137 return mIsSearchContentViewCreated;
141 * Resets the fake server's member data.
143 @VisibleForTesting
144 void reset() {
145 mLoadedUrl = null;
146 mSearchTermRequested = null;
147 mShouldUseHttps = false;
148 mLoadedUrlCount = 0;
149 mIsSearchContentViewCreated = false;