1 // Copyright 2013 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/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/run_loop.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "base/version.h"
10 #include "components/update_client/crx_update_item.h"
11 #include "components/update_client/ping_manager.h"
12 #include "components/update_client/test_configurator.h"
13 #include "components/update_client/url_request_post_interceptor.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 namespace update_client
{
21 class ComponentUpdaterPingManagerTest
: public testing::Test
{
23 ComponentUpdaterPingManagerTest();
24 ~ComponentUpdaterPingManagerTest() override
{}
26 void RunThreadsUntilIdle();
28 // Overrides from testing::Test.
29 void SetUp() override
;
30 void TearDown() override
;
33 scoped_refptr
<TestConfigurator
> config_
;
34 scoped_ptr
<PingManager
> ping_manager_
;
37 base::MessageLoopForIO loop_
;
40 ComponentUpdaterPingManagerTest::ComponentUpdaterPingManagerTest() {
43 void ComponentUpdaterPingManagerTest::SetUp() {
44 config_
= new TestConfigurator(base::ThreadTaskRunnerHandle::Get(),
45 base::ThreadTaskRunnerHandle::Get());
46 ping_manager_
.reset(new PingManager(*config_
));
49 void ComponentUpdaterPingManagerTest::TearDown() {
50 ping_manager_
.reset();
54 void ComponentUpdaterPingManagerTest::RunThreadsUntilIdle() {
55 base::RunLoop().RunUntilIdle();
58 // Test is flaky: http://crbug.com/349547
59 TEST_F(ComponentUpdaterPingManagerTest
, DISABLED_PingManagerTest
) {
60 scoped_ptr
<InterceptorFactory
> interceptor_factory(
61 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get()));
62 URLRequestPostInterceptor
* interceptor
=
63 interceptor_factory
->CreateInterceptor();
64 EXPECT_TRUE(interceptor
);
66 // Test eventresult="1" is sent for successful updates.
69 item
.state
= CrxUpdateItem::State::kUpdated
;
70 item
.previous_version
= base::Version("1.0");
71 item
.next_version
= base::Version("2.0");
73 ping_manager_
->OnUpdateComplete(&item
);
74 base::RunLoop().RunUntilIdle();
76 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
77 EXPECT_NE(string::npos
,
78 interceptor
->GetRequests()[0].find(
79 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
80 "<event eventtype=\"3\" eventresult=\"1\"/></app>"))
81 << interceptor
->GetRequestsAsString();
84 // Test eventresult="0" is sent for failed updates.
85 item
= CrxUpdateItem();
87 item
.state
= CrxUpdateItem::State::kNoUpdate
;
88 item
.previous_version
= base::Version("1.0");
89 item
.next_version
= base::Version("2.0");
91 ping_manager_
->OnUpdateComplete(&item
);
92 base::RunLoop().RunUntilIdle();
94 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
95 EXPECT_NE(string::npos
,
96 interceptor
->GetRequests()[0].find(
97 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
98 "<event eventtype=\"3\" eventresult=\"0\"/></app>"))
99 << interceptor
->GetRequestsAsString();
100 interceptor
->Reset();
102 // Test the error values and the fingerprints.
103 item
= CrxUpdateItem();
105 item
.state
= CrxUpdateItem::State::kNoUpdate
;
106 item
.previous_version
= base::Version("1.0");
107 item
.next_version
= base::Version("2.0");
108 item
.previous_fp
= "prev fp";
109 item
.next_fp
= "next fp";
110 item
.error_category
= 1;
112 item
.extra_code1
= -1;
113 item
.diff_error_category
= 10;
114 item
.diff_error_code
= 20;
115 item
.diff_extra_code1
= -10;
116 item
.diff_update_failed
= true;
117 item
.crx_diffurls
.push_back(GURL("http://host/path"));
119 ping_manager_
->OnUpdateComplete(&item
);
120 base::RunLoop().RunUntilIdle();
122 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
123 EXPECT_NE(string::npos
,
124 interceptor
->GetRequests()[0].find(
125 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
126 "<event eventtype=\"3\" eventresult=\"0\" errorcat=\"1\" "
127 "errorcode=\"2\" extracode1=\"-1\" diffresult=\"0\" "
128 "differrorcat=\"10\" "
129 "differrorcode=\"20\" diffextracode1=\"-10\" "
130 "previousfp=\"prev fp\" nextfp=\"next fp\"/></app>"))
131 << interceptor
->GetRequestsAsString();
132 interceptor
->Reset();
134 // Test the download metrics.
135 item
= CrxUpdateItem();
137 item
.state
= CrxUpdateItem::State::kUpdated
;
138 item
.previous_version
= base::Version("1.0");
139 item
.next_version
= base::Version("2.0");
141 CrxDownloader::DownloadMetrics download_metrics
;
142 download_metrics
.url
= GURL("http://host1/path1");
143 download_metrics
.downloader
= CrxDownloader::DownloadMetrics::kUrlFetcher
;
144 download_metrics
.error
= -1;
145 download_metrics
.downloaded_bytes
= 123;
146 download_metrics
.total_bytes
= 456;
147 download_metrics
.download_time_ms
= 987;
148 item
.download_metrics
.push_back(download_metrics
);
150 download_metrics
= CrxDownloader::DownloadMetrics();
151 download_metrics
.url
= GURL("http://host2/path2");
152 download_metrics
.downloader
= CrxDownloader::DownloadMetrics::kBits
;
153 download_metrics
.error
= 0;
154 download_metrics
.downloaded_bytes
= 1230;
155 download_metrics
.total_bytes
= 4560;
156 download_metrics
.download_time_ms
= 9870;
157 item
.download_metrics
.push_back(download_metrics
);
159 ping_manager_
->OnUpdateComplete(&item
);
160 base::RunLoop().RunUntilIdle();
162 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
165 interceptor
->GetRequests()[0].find(
166 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
167 "<event eventtype=\"3\" eventresult=\"1\"/>"
168 "<event eventtype=\"14\" eventresult=\"0\" downloader=\"direct\" "
169 "errorcode=\"-1\" url=\"http://host1/path1\" downloaded=\"123\" "
170 "total=\"456\" download_time_ms=\"987\"/>"
171 "<event eventtype=\"14\" eventresult=\"1\" downloader=\"bits\" "
172 "url=\"http://host2/path2\" downloaded=\"1230\" total=\"4560\" "
173 "download_time_ms=\"9870\"/></app>"))
174 << interceptor
->GetRequestsAsString();
175 interceptor
->Reset();
178 } // namespace update_client