OLD | NEW |
(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.contextualsearch; |
| 6 |
| 7 import org.chromium.base.VisibleForTesting; |
| 8 |
| 9 import java.net.MalformedURLException; |
| 10 import java.net.URL; |
| 11 |
| 12 import javax.annotation.Nullable; |
| 13 |
| 14 /** |
| 15 * Implements a fake Contextual Search server, for testing purposes. |
| 16 * TODO(donnd): add more functionality to this class once the overall approach h
as been validated. |
| 17 * TODO(donnd): rename this class when we refactor and rename the interface it i
mplements. Should |
| 18 * be something like ContextualSearchFakeEnvironment. |
| 19 */ |
| 20 @VisibleForTesting |
| 21 class ContextualSearchFakeServer implements ContextualSearchNetworkCommunicator
{ |
| 22 |
| 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; |
| 29 |
| 30 /** |
| 31 * Constructs a fake Contextual Search server that will callback to the give
n baseManager. |
| 32 * @param baseManager The manager to call back to for server responses. |
| 33 */ |
| 34 @VisibleForTesting |
| 35 ContextualSearchFakeServer(ContextualSearchNetworkCommunicator baseManager)
{ |
| 36 mBaseManager = baseManager; |
| 37 } |
| 38 |
| 39 @Override |
| 40 public void startSearchTermResolutionRequest(String selection) { |
| 41 mLoadedUrl = null; |
| 42 mSearchTermRequested = selection; |
| 43 } |
| 44 |
| 45 @Override |
| 46 public void continueSearchTermResolutionRequest() { |
| 47 mLoadedUrl = null; |
| 48 } |
| 49 |
| 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); |
| 57 } |
| 58 |
| 59 @Override |
| 60 public void handleSearchTermResolutionResponse(boolean isNetworkUnavailable,
int responseCode, |
| 61 String searchTerm, String displayText, String alternateTerm, boolean
doPreventPreload) { |
| 62 mBaseManager.handleSearchTermResolutionResponse(isNetworkUnavailable, re
sponseCode, |
| 63 searchTerm, displayText, alternateTerm, doPreventPreload); |
| 64 } |
| 65 |
| 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(); |
| 75 } |
| 76 } |
| 77 return baseUrl; |
| 78 } |
| 79 |
| 80 @Override |
| 81 public void handleDidNavigateMainFrame(String url, int httpResultCode) { |
| 82 mBaseManager.handleDidNavigateMainFrame(url, httpResultCode); |
| 83 } |
| 84 |
| 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 i
f 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. |
| 92 } |
| 93 |
| 94 @Override |
| 95 public void destroySearchContentView() { |
| 96 mIsSearchContentViewCreated = false; |
| 97 mBaseManager.destroySearchContentView(); |
| 98 } |
| 99 |
| 100 /** |
| 101 * @return The search term requested, or {@code null} if no search term was
requested. |
| 102 */ |
| 103 @VisibleForTesting |
| 104 String getSearchTermRequested() { |
| 105 return mSearchTermRequested; |
| 106 } |
| 107 |
| 108 /** |
| 109 * @return the loaded search result page URL if any was requested. |
| 110 */ |
| 111 @VisibleForTesting |
| 112 String getLoadedUrl() { |
| 113 return mLoadedUrl; |
| 114 } |
| 115 |
| 116 /** |
| 117 * @return The number of times we loaded a URL in the Content View. |
| 118 */ |
| 119 @VisibleForTesting |
| 120 int loadedUrlCount() { |
| 121 return mLoadedUrlCount; |
| 122 } |
| 123 |
| 124 /** |
| 125 * Sets whether to return an HTTPS URL instead of HTTP, from {@link #getBase
PageUrl}. |
| 126 */ |
| 127 @VisibleForTesting |
| 128 void setShouldUseHttps(boolean setting) { |
| 129 mShouldUseHttps = setting; |
| 130 } |
| 131 |
| 132 /** |
| 133 * @return Whether we tried to create the Search Content View. |
| 134 */ |
| 135 @VisibleForTesting |
| 136 boolean isSearchContentViewCreated() { |
| 137 return mIsSearchContentViewCreated; |
| 138 } |
| 139 |
| 140 /** |
| 141 * Resets the fake server's member data. |
| 142 */ |
| 143 @VisibleForTesting |
| 144 void reset() { |
| 145 mLoadedUrl = null; |
| 146 mSearchTermRequested = null; |
| 147 mShouldUseHttps = false; |
| 148 mLoadedUrlCount = 0; |
| 149 mIsSearchContentViewCreated = false; |
| 150 } |
| 151 } |
OLD | NEW |