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/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "base/version.h"
9 #include "components/update_client/crx_update_item.h"
10 #include "components/update_client/ping_manager.h"
11 #include "components/update_client/test/test_configurator.h"
12 #include "components/update_client/test/url_request_post_interceptor.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 namespace update_client
{
20 class ComponentUpdaterPingManagerTest
: public testing::Test
{
22 ComponentUpdaterPingManagerTest();
23 ~ComponentUpdaterPingManagerTest() override
{}
25 void RunThreadsUntilIdle();
27 // Overrides from testing::Test.
28 void SetUp() override
;
29 void TearDown() override
;
32 scoped_ptr
<TestConfigurator
> config_
;
33 scoped_ptr
<PingManager
> ping_manager_
;
36 base::MessageLoopForIO loop_
;
39 ComponentUpdaterPingManagerTest::ComponentUpdaterPingManagerTest()
40 : config_(new TestConfigurator(base::MessageLoopProxy::current(),
41 base::MessageLoopProxy::current())) {
44 void ComponentUpdaterPingManagerTest::SetUp() {
45 ping_manager_
.reset(new PingManager(*config_
));
48 void ComponentUpdaterPingManagerTest::TearDown() {
49 ping_manager_
.reset();
53 void ComponentUpdaterPingManagerTest::RunThreadsUntilIdle() {
54 base::RunLoop().RunUntilIdle();
57 // Test is flaky: http://crbug.com/349547
58 TEST_F(ComponentUpdaterPingManagerTest
, DISABLED_PingManagerTest
) {
59 scoped_ptr
<InterceptorFactory
> interceptor_factory(
60 new InterceptorFactory(base::MessageLoopProxy::current()));
61 URLRequestPostInterceptor
* interceptor
=
62 interceptor_factory
->CreateInterceptor();
63 EXPECT_TRUE(interceptor
);
65 // Test eventresult="1" is sent for successful updates.
68 item
.status
= CrxUpdateItem::kUpdated
;
69 item
.previous_version
= base::Version("1.0");
70 item
.next_version
= base::Version("2.0");
72 ping_manager_
->OnUpdateComplete(&item
);
73 base::RunLoop().RunUntilIdle();
75 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
76 EXPECT_NE(string::npos
,
77 interceptor
->GetRequests()[0].find(
78 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
79 "<event eventtype=\"3\" eventresult=\"1\"/></app>"))
80 << interceptor
->GetRequestsAsString();
83 // Test eventresult="0" is sent for failed updates.
84 item
= CrxUpdateItem();
86 item
.status
= CrxUpdateItem::kNoUpdate
;
87 item
.previous_version
= base::Version("1.0");
88 item
.next_version
= base::Version("2.0");
90 ping_manager_
->OnUpdateComplete(&item
);
91 base::RunLoop().RunUntilIdle();
93 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
94 EXPECT_NE(string::npos
,
95 interceptor
->GetRequests()[0].find(
96 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
97 "<event eventtype=\"3\" eventresult=\"0\"/></app>"))
98 << interceptor
->GetRequestsAsString();
101 // Test the error values and the fingerprints.
102 item
= CrxUpdateItem();
104 item
.status
= CrxUpdateItem::kNoUpdate
;
105 item
.previous_version
= base::Version("1.0");
106 item
.next_version
= base::Version("2.0");
107 item
.previous_fp
= "prev fp";
108 item
.next_fp
= "next fp";
109 item
.error_category
= 1;
111 item
.extra_code1
= -1;
112 item
.diff_error_category
= 10;
113 item
.diff_error_code
= 20;
114 item
.diff_extra_code1
= -10;
115 item
.diff_update_failed
= true;
116 item
.crx_diffurls
.push_back(GURL("http://host/path"));
118 ping_manager_
->OnUpdateComplete(&item
);
119 base::RunLoop().RunUntilIdle();
121 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
122 EXPECT_NE(string::npos
,
123 interceptor
->GetRequests()[0].find(
124 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
125 "<event eventtype=\"3\" eventresult=\"0\" errorcat=\"1\" "
126 "errorcode=\"2\" extracode1=\"-1\" diffresult=\"0\" "
127 "differrorcat=\"10\" "
128 "differrorcode=\"20\" diffextracode1=\"-10\" "
129 "previousfp=\"prev fp\" nextfp=\"next fp\"/></app>"))
130 << interceptor
->GetRequestsAsString();
131 interceptor
->Reset();
133 // Test the download metrics.
134 item
= CrxUpdateItem();
136 item
.status
= CrxUpdateItem::kUpdated
;
137 item
.previous_version
= base::Version("1.0");
138 item
.next_version
= base::Version("2.0");
140 CrxDownloader::DownloadMetrics download_metrics
;
141 download_metrics
.url
= GURL("http://host1/path1");
142 download_metrics
.downloader
= CrxDownloader::DownloadMetrics::kUrlFetcher
;
143 download_metrics
.error
= -1;
144 download_metrics
.downloaded_bytes
= 123;
145 download_metrics
.total_bytes
= 456;
146 download_metrics
.download_time_ms
= 987;
147 item
.download_metrics
.push_back(download_metrics
);
149 download_metrics
= CrxDownloader::DownloadMetrics();
150 download_metrics
.url
= GURL("http://host2/path2");
151 download_metrics
.downloader
= CrxDownloader::DownloadMetrics::kBits
;
152 download_metrics
.error
= 0;
153 download_metrics
.downloaded_bytes
= 1230;
154 download_metrics
.total_bytes
= 4560;
155 download_metrics
.download_time_ms
= 9870;
156 item
.download_metrics
.push_back(download_metrics
);
158 ping_manager_
->OnUpdateComplete(&item
);
159 base::RunLoop().RunUntilIdle();
161 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
164 interceptor
->GetRequests()[0].find(
165 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
166 "<event eventtype=\"3\" eventresult=\"1\"/>"
167 "<event eventtype=\"14\" eventresult=\"0\" downloader=\"direct\" "
168 "errorcode=\"-1\" url=\"http://host1/path1\" downloaded=\"123\" "
169 "total=\"456\" download_time_ms=\"987\"/>"
170 "<event eventtype=\"14\" eventresult=\"1\" downloader=\"bits\" "
171 "url=\"http://host2/path2\" downloaded=\"1230\" total=\"4560\" "
172 "download_time_ms=\"9870\"/></app>"))
173 << interceptor
->GetRequestsAsString();
174 interceptor
->Reset();
177 } // namespace update_client