Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / supervised_user / permission_request_creator_apiary_unittest.cc
blob7469933e259af0262fcc47d4b6a22ede35fe5378
1 // Copyright 2014 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 #include "base/json/json_writer.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/values.h"
9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
10 #include "chrome/browser/supervised_user/permission_request_creator_apiary.h"
11 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace {
19 const char kAccountId[] = "account@gmail.com";
20 const char kApiScope[] = "api_scope";
21 const char kPermissionRequestApiUrl[] = "https://myapis.com/permissions";
23 std::string BuildResponse() {
24 base::DictionaryValue dict;
25 base::DictionaryValue* permission_dict = new base::DictionaryValue;
26 permission_dict->SetStringWithoutPathExpansion("id", "requestid");
27 dict.SetWithoutPathExpansion("permissionRequest", permission_dict);
28 std::string result;
29 base::JSONWriter::Write(&dict, &result);
30 return result;
33 class FakeSupervisedUserSigninManagerWrapper
34 : public SupervisedUserSigninManagerWrapper {
35 public:
36 FakeSupervisedUserSigninManagerWrapper()
37 : SupervisedUserSigninManagerWrapper(NULL, NULL) {}
39 virtual std::string GetEffectiveUsername() const override {
40 return kAccountId;
42 virtual std::string GetAccountIdToUse() const override {
43 return kAccountId;
45 virtual std::string GetSyncScopeToUse() const override {
46 return kApiScope;
50 } // namespace
52 class PermissionRequestCreatorApiaryTest : public testing::Test {
53 public:
54 PermissionRequestCreatorApiaryTest()
55 : request_context_(new net::TestURLRequestContextGetter(
56 base::MessageLoopProxy::current())),
57 permission_creator_(
58 &token_service_,
59 make_scoped_ptr(new FakeSupervisedUserSigninManagerWrapper),
60 request_context_.get(),
61 GURL(kPermissionRequestApiUrl)) {
62 token_service_.IssueRefreshTokenForUser(kAccountId, "refresh_token");
65 protected:
66 void IssueAccessTokens() {
67 token_service_.IssueAllTokensForAccount(
68 kAccountId,
69 "access_token",
70 base::Time::Now() + base::TimeDelta::FromHours(1));
73 void IssueAccessTokenErrors() {
74 token_service_.IssueErrorForAllPendingRequestsForAccount(
75 kAccountId,
76 GoogleServiceAuthError::FromServiceError("Error!"));
79 void CreateRequest(int url_fetcher_id, const GURL& url) {
80 permission_creator_.set_url_fetcher_id_for_testing(url_fetcher_id);
81 permission_creator_.CreatePermissionRequest(
82 url,
83 base::Bind(&PermissionRequestCreatorApiaryTest::OnRequestCreated,
84 base::Unretained(this)));
87 net::TestURLFetcher* GetURLFetcher(int id) {
88 net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(id);
89 EXPECT_TRUE(url_fetcher);
90 return url_fetcher;
93 void SendResponse(int url_fetcher_id,
94 net::URLRequestStatus::Status status,
95 const std::string& response) {
96 net::TestURLFetcher* url_fetcher = GetURLFetcher(url_fetcher_id);
97 url_fetcher->set_status(net::URLRequestStatus(status, 0));
98 url_fetcher->set_response_code(net::HTTP_OK);
99 url_fetcher->SetResponseString(response);
100 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
103 void SendValidResponse(int url_fetcher_id) {
104 SendResponse(url_fetcher_id,
105 net::URLRequestStatus::SUCCESS,
106 BuildResponse());
109 void SendFailedResponse(int url_fetcher_id) {
110 SendResponse(url_fetcher_id,
111 net::URLRequestStatus::CANCELED,
112 std::string());
115 MOCK_METHOD1(OnRequestCreated, void(bool success));
117 base::MessageLoop message_loop_;
118 FakeProfileOAuth2TokenService token_service_;
119 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
120 net::TestURLFetcherFactory url_fetcher_factory_;
121 PermissionRequestCreatorApiary permission_creator_;
124 TEST_F(PermissionRequestCreatorApiaryTest, Success) {
125 CreateRequest(0, GURL("http://randomurl.com"));
126 CreateRequest(1, GURL("http://anotherurl.com"));
128 // We should have gotten a request for an access token.
129 EXPECT_GT(token_service_.GetPendingRequests().size(), 0U);
131 IssueAccessTokens();
133 EXPECT_CALL(*this, OnRequestCreated(true));
134 SendValidResponse(0);
135 EXPECT_CALL(*this, OnRequestCreated(true));
136 SendValidResponse(1);
139 TEST_F(PermissionRequestCreatorApiaryTest, AccessTokenError) {
140 CreateRequest(0, GURL("http://randomurl.com"));
142 // We should have gotten a request for an access token.
143 EXPECT_EQ(1U, token_service_.GetPendingRequests().size());
145 // Our callback should get called immediately on an error.
146 EXPECT_CALL(*this, OnRequestCreated(false));
147 IssueAccessTokenErrors();
150 TEST_F(PermissionRequestCreatorApiaryTest, NetworkError) {
151 CreateRequest(0, GURL("http://randomurl.com"));
153 // We should have gotten a request for an access token.
154 EXPECT_EQ(1U, token_service_.GetPendingRequests().size());
156 IssueAccessTokens();
158 // Our callback should get called on an error.
159 EXPECT_CALL(*this, OnRequestCreated(false));
160 SendFailedResponse(0);