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

Unified Diff: components/history/core/browser/fake_web_history_service.cc

Issue 1454413002: Move FakeWebHistoryService to components/history (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Include net Created 5 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: components/history/core/browser/fake_web_history_service.cc
diff --git a/components/history/core/browser/fake_web_history_service.cc b/components/history/core/browser/fake_web_history_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d25717d6d5c16eb649b025a7c903d2c8a04a2da6
--- /dev/null
+++ b/components/history/core/browser/fake_web_history_service.cc
@@ -0,0 +1,135 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/history/core/browser/fake_web_history_service.h"
+
+#include "base/callback.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/time/time.h"
+#include "net/base/url_util.h"
+#include "net/url_request/url_request_context_getter.h"
+#include "url/gurl.h"
+
+namespace history {
+
+// FakeWebHistoryService::Request ----------------------------------------------
+
+FakeWebHistoryService::Request::Request(
+ FakeWebHistoryService* service,
+ bool emulate_success,
+ int emulate_response_code,
+ const CompletionCallback& callback,
+ base::Time begin,
+ base::Time end,
+ int max_count)
+ : service_(service),
+ emulate_success_(emulate_success),
+ emulate_response_code_(emulate_response_code),
+ callback_(callback),
+ begin_(begin),
+ end_(end),
+ max_count_(max_count),
+ is_pending_(false) {
+}
+
+bool FakeWebHistoryService::Request::IsPending() {
+ return is_pending_;
+}
+
+int FakeWebHistoryService::Request::GetResponseCode() {
+ return emulate_response_code_;
+}
+
+const std::string& FakeWebHistoryService::Request::GetResponseBody() {
+ int count = service_->GetNumberOfVisitsBetween(begin_, end_);
+ if (max_count_ && max_count_ < count)
+ count = max_count_;
+
+ response_body_ = "{ \"event\": [";
+ for (int i = 0; i < count; ++i)
+ response_body_ += i ? ", {}" : "{}";
+ response_body_ += "] }";
+ return response_body_;
+}
+
+void FakeWebHistoryService::Request::SetPostData(const std::string& post_data) {
+ // Unused.
+};
+
+void FakeWebHistoryService::Request::Start() {
+ is_pending_ = true;
+ callback_.Run(this, emulate_success_);
+}
+
+// FakeWebHistoryService -------------------------------------------------------
+
+FakeWebHistoryService::FakeWebHistoryService(
+ OAuth2TokenService* token_service,
+ SigninManagerBase* signin_manager,
+ const scoped_refptr<net::URLRequestContextGetter>& request_context)
+ : history::WebHistoryService(
+ token_service,
+ signin_manager,
+ request_context) {
+}
+
+FakeWebHistoryService::~FakeWebHistoryService() {
+}
+
+void FakeWebHistoryService::SetupFakeResponse(
+ bool emulate_success, int emulate_response_code) {
+ emulate_success_ = emulate_success;
+ emulate_response_code_ = emulate_response_code;
+}
+
+void FakeWebHistoryService::AddSyncedVisit(
+ std::string url, base::Time timestamp) {
+ visits_.push_back(make_pair(url, timestamp));
+}
+
+void FakeWebHistoryService::ClearSyncedVisits() {
+ visits_.clear();
+}
+
+int FakeWebHistoryService::GetNumberOfVisitsBetween(
+ const base::Time& begin, const base::Time& end) {
+ int result = 0;
+ for (const Visit& visit : visits_) {
+ if (visit.second >= begin && visit.second < end)
+ ++result;
+ }
+ return result;
+}
+
+base::Time FakeWebHistoryService::GetTimeForKeyInQuery(
+ const GURL& url, const std::string& key) {
+ std::string value;
+ if (!net::GetValueForKeyInQuery(url, key, &value))
+ return base::Time();
+
+ int64 us;
+ if (!base::StringToInt64(value, &us))
+ return base::Time();
+ return base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(us);
+}
+
+FakeWebHistoryService::Request* FakeWebHistoryService::CreateRequest(
+ const GURL& url, const CompletionCallback& callback) {
+ // Find the time range endpoints in the URL.
+ base::Time begin = GetTimeForKeyInQuery(url, "min");
+ base::Time end = GetTimeForKeyInQuery(url, "max");
+
+ if (end.is_null())
+ end = base::Time::Max();
+
+ int max_count = 0;
+ std::string max_count_str;
+ if (net::GetValueForKeyInQuery(url, "num", &max_count_str))
+ base::StringToInt(max_count_str, &max_count);
+
+ return new Request(this, emulate_success_, emulate_response_code_,
+ callback, begin, end, max_count);
+}
+
+} // namespace history

Powered by Google App Engine
This is Rietveld 408576698