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
;
12 import javax
.annotation
.Nullable
;
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.
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
;
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.
35 ContextualSearchFakeServer(ContextualSearchNetworkCommunicator baseManager
) {
36 mBaseManager
= baseManager
;
40 public void startSearchTermResolutionRequest(String selection
) {
42 mSearchTermRequested
= selection
;
46 public void continueSearchTermResolutionRequest() {
51 public void loadUrl(String url
) {
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
);
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
);
67 @Nullable public URL
getBasePageUrl() {
68 URL baseUrl
= mBaseManager
.getBasePageUrl();
69 if (mShouldUseHttps
&& baseUrl
!= null) {
71 return new URL(baseUrl
.toString().replace("http://", "https://"));
72 } catch (MalformedURLException e
) {
73 // TODO(donnd): Auto-generated catch block
81 public void handleDidNavigateMainFrame(String url
, int httpResultCode
) {
82 mBaseManager
.handleDidNavigateMainFrame(url
, httpResultCode
);
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.
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.
104 String
getSearchTermRequested() {
105 return mSearchTermRequested
;
109 * @return the loaded search result page URL if any was requested.
112 String
getLoadedUrl() {
117 * @return The number of times we loaded a URL in the Content View.
120 int loadedUrlCount() {
121 return mLoadedUrlCount
;
125 * Sets whether to return an HTTPS URL instead of HTTP, from {@link #getBasePageUrl}.
128 void setShouldUseHttps(boolean setting
) {
129 mShouldUseHttps
= setting
;
133 * @return Whether we tried to create the Search Content View.
136 boolean isSearchContentViewCreated() {
137 return mIsSearchContentViewCreated
;
141 * Resets the fake server's member data.
146 mSearchTermRequested
= null;
147 mShouldUseHttps
= false;
149 mIsSearchContentViewCreated
= false;