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 #include "remoting/test/app_remoting_report_issue_request.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/timer/timer.h"
11 #include "net/url_request/test_url_fetcher_factory.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 const char kTestApplicationId
[] = "klasdfjlkasdfjklasjfdkljsadf";
16 const char kTestHostId
[] = "test_host_id_value";
17 const char kAccessTokenValue
[] = "test_access_token_value";
18 const char kReportIssueResponse
[] = "{}";
24 // Provides base functionality for the AppRemotingReportIssueRequest Tests. The
25 // FakeURLFetcherFactory allows us to override the response data and payload for
26 // specified URLs. We use this to stub out network calls made by the
27 // AppRemotingReportIssueRequest. This fixture also creates an IO MessageLoop,
28 // if necessary, for use by the AppRemotingReportIssueRequest class.
29 class AppRemotingReportIssueRequestTest
: public ::testing::Test
{
31 AppRemotingReportIssueRequestTest();
32 ~AppRemotingReportIssueRequestTest() override
;
35 // testing::Test interface.
36 void SetUp() override
;
38 // Sets the HTTP status and data returned for a specified URL.
39 void SetFakeResponse(const GURL
& url
,
40 const std::string
& data
,
41 net::HttpStatusCode code
,
42 net::URLRequestStatus::Status status
);
44 // Used for result verification.
45 std::string dev_service_environment_url_
;
46 std::string test_service_environment_url_
;
47 std::string staging_service_environment_url_
;
49 scoped_ptr
<base::RunLoop
> run_loop_
;
50 scoped_ptr
<base::Timer
> timer_
;
52 AppRemotingReportIssueRequest app_remoting_report_issue_request_
;
55 net::FakeURLFetcherFactory url_fetcher_factory_
;
56 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
58 DISALLOW_COPY_AND_ASSIGN(AppRemotingReportIssueRequestTest
);
61 AppRemotingReportIssueRequestTest::AppRemotingReportIssueRequestTest()
62 : url_fetcher_factory_(nullptr), message_loop_(new base::MessageLoopForIO
) {
65 AppRemotingReportIssueRequestTest::~AppRemotingReportIssueRequestTest() {
68 void AppRemotingReportIssueRequestTest::SetUp() {
69 run_loop_
.reset(new base::RunLoop());
70 timer_
.reset(new base::Timer(true, false));
72 dev_service_environment_url_
=
73 GetReportIssueUrl(kTestApplicationId
, kTestHostId
, kDeveloperEnvironment
);
74 SetFakeResponse(GURL(dev_service_environment_url_
), kReportIssueResponse
,
75 net::HTTP_NOT_FOUND
, net::URLRequestStatus::FAILED
);
77 test_service_environment_url_
=
78 GetReportIssueUrl(kTestApplicationId
, kTestHostId
, kTestingEnvironment
);
79 SetFakeResponse(GURL(test_service_environment_url_
), kReportIssueResponse
,
80 net::HTTP_NOT_FOUND
, net::URLRequestStatus::FAILED
);
82 staging_service_environment_url_
=
83 GetReportIssueUrl(kTestApplicationId
, kTestHostId
, kStagingEnvironment
);
84 SetFakeResponse(GURL(staging_service_environment_url_
), kReportIssueResponse
,
85 net::HTTP_NOT_FOUND
, net::URLRequestStatus::FAILED
);
88 void AppRemotingReportIssueRequestTest::SetFakeResponse(
90 const std::string
& data
,
91 net::HttpStatusCode code
,
92 net::URLRequestStatus::Status status
) {
93 url_fetcher_factory_
.SetFakeResponse(url
, data
, code
, status
);
96 TEST_F(AppRemotingReportIssueRequestTest
, ReportIssueFromDev
) {
97 SetFakeResponse(GURL(dev_service_environment_url_
), kReportIssueResponse
,
98 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
100 timer_
->Start(FROM_HERE
, base::TimeDelta::FromSeconds(1),
101 run_loop_
->QuitClosure());
103 bool request_started
= app_remoting_report_issue_request_
.Start(
104 kTestApplicationId
, kTestHostId
, kAccessTokenValue
, kDeveloperEnvironment
,
105 true, run_loop_
->QuitClosure());
106 EXPECT_TRUE(request_started
);
110 // Verify we stopped because of the request completing and not the timer.
111 EXPECT_TRUE(timer_
->IsRunning());
115 TEST_F(AppRemotingReportIssueRequestTest
, ReportIssueFromTest
) {
116 SetFakeResponse(GURL(test_service_environment_url_
), kReportIssueResponse
,
117 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
119 timer_
->Start(FROM_HERE
, base::TimeDelta::FromSeconds(1),
120 run_loop_
->QuitClosure());
122 bool request_started
= app_remoting_report_issue_request_
.Start(
123 kTestApplicationId
, kTestHostId
, kAccessTokenValue
, kTestingEnvironment
,
124 true, run_loop_
->QuitClosure());
125 EXPECT_TRUE(request_started
);
129 // Verify we stopped because of the request completing and not the timer.
130 EXPECT_TRUE(timer_
->IsRunning());
134 TEST_F(AppRemotingReportIssueRequestTest
, ReportIssueFromStaging
) {
135 SetFakeResponse(GURL(staging_service_environment_url_
), kReportIssueResponse
,
136 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
138 timer_
->Start(FROM_HERE
, base::TimeDelta::FromSeconds(1),
139 run_loop_
->QuitClosure());
141 bool request_started
= app_remoting_report_issue_request_
.Start(
142 kTestApplicationId
, kTestHostId
, kAccessTokenValue
, kStagingEnvironment
,
143 true, run_loop_
->QuitClosure());
144 EXPECT_TRUE(request_started
);
148 // Verify we stopped because of the request completing and not the timer.
149 EXPECT_TRUE(timer_
->IsRunning());
153 TEST_F(AppRemotingReportIssueRequestTest
, ReportIssueFromInvalidEnvironment
) {
154 bool request_started
= app_remoting_report_issue_request_
.Start(
155 kTestApplicationId
, kTestHostId
, kAccessTokenValue
, kUnknownEnvironment
,
156 true, run_loop_
->QuitClosure());
158 EXPECT_FALSE(request_started
);
161 TEST_F(AppRemotingReportIssueRequestTest
, ReportIssueNetworkError
) {
162 timer_
->Start(FROM_HERE
, base::TimeDelta::FromSeconds(1),
163 run_loop_
->QuitClosure());
165 bool request_started
= app_remoting_report_issue_request_
.Start(
166 kTestApplicationId
, kTestHostId
, kAccessTokenValue
, kStagingEnvironment
,
167 true, run_loop_
->QuitClosure());
168 EXPECT_TRUE(request_started
);
172 // Verify we stopped because of the request completing and not the timer.
173 EXPECT_TRUE(timer_
->IsRunning());
177 TEST_F(AppRemotingReportIssueRequestTest
, MultipleRequestsCanBeIssued
) {
178 SetFakeResponse(GURL(staging_service_environment_url_
), kReportIssueResponse
,
179 net::HTTP_OK
, net::URLRequestStatus::SUCCESS
);
181 timer_
->Start(FROM_HERE
, base::TimeDelta::FromSeconds(1),
182 run_loop_
->QuitClosure());
184 bool request_started
= app_remoting_report_issue_request_
.Start(
185 kTestApplicationId
, kTestHostId
, kAccessTokenValue
, kStagingEnvironment
,
186 true, run_loop_
->QuitClosure());
187 EXPECT_TRUE(request_started
);
191 // Verify we stopped because of the request completing and not the timer.
192 EXPECT_TRUE(timer_
->IsRunning());
195 run_loop_
.reset(new base::RunLoop());
196 timer_
->Start(FROM_HERE
, base::TimeDelta::FromSeconds(1),
197 run_loop_
->QuitClosure());
199 request_started
= app_remoting_report_issue_request_
.Start(
200 kTestApplicationId
, kTestHostId
, kAccessTokenValue
, kStagingEnvironment
,
201 true, run_loop_
->QuitClosure());
202 EXPECT_TRUE(request_started
);
206 // Verify we stopped because of the request completing and not the timer.
207 EXPECT_TRUE(timer_
->IsRunning());
210 run_loop_
.reset(new base::RunLoop());
211 timer_
->Start(FROM_HERE
, base::TimeDelta::FromSeconds(1),
212 run_loop_
->QuitClosure());
214 request_started
= app_remoting_report_issue_request_
.Start(
215 kTestApplicationId
, kTestHostId
, kAccessTokenValue
, kStagingEnvironment
,
216 true, run_loop_
->QuitClosure());
217 EXPECT_TRUE(request_started
);
221 // Verify we stopped because of the request completing and not the timer.
222 EXPECT_TRUE(timer_
->IsRunning());
227 } // namespace remoting