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

Unified Diff: chrome/browser/supervised_user/experimental/supervised_user_report_url_apiary_unittest.cc

Issue 1813833002: Add report URL to safe search API functionality. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/supervised_user/experimental/supervised_user_report_url_apiary_unittest.cc
diff --git a/chrome/browser/supervised_user/experimental/supervised_user_report_url_apiary_unittest.cc b/chrome/browser/supervised_user/experimental/supervised_user_report_url_apiary_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a7aae56681018e20af460e5ab8ad2c4d12a5e8d4
--- /dev/null
+++ b/chrome/browser/supervised_user/experimental/supervised_user_report_url_apiary_unittest.cc
@@ -0,0 +1,119 @@
+// Copyright 2014 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 "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/thread_task_runner_handle.h"
+#include "base/values.h"
+#include "chrome/browser/supervised_user/experimental/supervised_user_report_url_apiary.h"
+#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
+#include "net/base/net_errors.h"
+#include "net/url_request/test_url_fetcher_factory.h"
+#include "net/url_request/url_request_test_util.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+const char kAccountId[] = "account@gmail.com";
+
+} //namespace
+
+class SupervisedUserReportURLApiaryTest : public testing::Test {
+ public:
+ SupervisedUserReportURLApiaryTest()
+ : request_context_(new net::TestURLRequestContextGetter(
+ base::ThreadTaskRunnerHandle::Get())),
+ report_url_(&token_service_,
+ kAccountId,
+ request_context_.get()) {
+ token_service_.UpdateCredentials(kAccountId, "refresh_token");
+ }
+ protected:
+ void IssueAccessTokens() {
+ token_service_.IssueAllTokensForAccount(
+ kAccountId,
+ "access_token",
+ base::Time::Now() + base::TimeDelta::FromHours(1));
+ }
+
+ void IssueAccessTokenErrors() {
+ token_service_.IssueErrorForAllPendingRequestsForAccount(
+ kAccountId,
+ GoogleServiceAuthError::FromServiceError("Error!"));
+ }
+
+ void CreateRequest(int url_fetcher_id, const GURL& url) {
+ report_url_.set_url_fetcher_id_for_testing(url_fetcher_id);
+ report_url_.CreateReportUrlRequest(
+ url,
+ base::Bind(&SupervisedUserReportURLApiaryTest::OnRequestCreated,
+ base::Unretained(this)));
+ }
+
+ net::TestURLFetcher* GetURLFetcher(int id) {
+ net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(id);
+ EXPECT_TRUE(url_fetcher);
+ return url_fetcher;
+ }
+
+ void SendResponse(int url_fetcher_id,
+ net::Error error) {
+ net::TestURLFetcher* url_fetcher = GetURLFetcher(url_fetcher_id);
+ url_fetcher->set_status(net::URLRequestStatus::FromError(error));
+ url_fetcher->set_response_code(net::HTTP_OK);
+ url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
+ }
+
+ void SendValidResponse(int url_fetcher_id) {
+ SendResponse(url_fetcher_id, net::OK);
+ }
+
+ void SendFailedResponse(int url_fetcher_id) {
+ SendResponse(url_fetcher_id, net::ERR_ABORTED);
+ }
+
+ MOCK_METHOD1(OnRequestCreated, void(bool sucess));
+
+ base::MessageLoop message_loop_;
+ FakeProfileOAuth2TokenService token_service_;
+ scoped_refptr<net::TestURLRequestContextGetter> request_context_;
+ net::TestURLFetcherFactory url_fetcher_factory_;
+ SupervisedUserReportURLApiary report_url_;
+};
+
+TEST_F(SupervisedUserReportURLApiaryTest, Success) {
+ CreateRequest(0, GURL("http://google.com"));
+ CreateRequest(1, GURL("http://url.com"));
+
+ EXPECT_GT(token_service_.GetPendingRequests().size(), 0U);
+
+ IssueAccessTokens();
+
+ EXPECT_CALL(*this, OnRequestCreated(true));
+ SendValidResponse(0);
+ EXPECT_CALL(*this, OnRequestCreated(true));
+ SendValidResponse(1);
+}
+
+TEST_F(SupervisedUserReportURLApiaryTest, AccessTokenError) {
+ CreateRequest(0, GURL("http://google.com"));
+
+ EXPECT_EQ(1U, token_service_.GetPendingRequests().size());
+
+ EXPECT_CALL(*this, OnRequestCreated(false));
+ IssueAccessTokenErrors();
+}
+
+TEST_F(SupervisedUserReportURLApiaryTest, NetworkError) {
+ CreateRequest(0, GURL("http://google.com"));
+
+ EXPECT_EQ(1U, token_service_.GetPendingRequests().size());
+
+ IssueAccessTokens();
+
+ EXPECT_CALL(*this, OnRequestCreated(false));
+ SendFailedResponse(0);
+}
+

Powered by Google App Engine
This is Rietveld 408576698