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/run_loop.h"
7 #include "base/version.h"
8 #include "chrome/browser/component_updater/component_updater_ping_manager.h"
9 #include "chrome/browser/component_updater/crx_update_item.h"
10 #include "chrome/browser/component_updater/test/component_updater_service_unittest.h"
11 #include "chrome/browser/component_updater/test/url_request_post_interceptor.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread
;
19 namespace component_updater
{
21 class ComponentUpdaterPingManagerTest
: public testing::Test
{
23 ComponentUpdaterPingManagerTest();
24 virtual ~ComponentUpdaterPingManagerTest();
26 void RunThreadsUntilIdle();
28 // Overrides from testing::Test.
29 virtual void SetUp() OVERRIDE
;
30 virtual void TearDown() OVERRIDE
;
33 scoped_ptr
<PingManager
> ping_manager_
;
36 scoped_refptr
<net::TestURLRequestContextGetter
> context_
;
37 content::TestBrowserThreadBundle thread_bundle_
;
40 ComponentUpdaterPingManagerTest::ComponentUpdaterPingManagerTest()
41 : context_(new net::TestURLRequestContextGetter(
42 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
))),
43 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
46 ComponentUpdaterPingManagerTest::~ComponentUpdaterPingManagerTest() {
50 void ComponentUpdaterPingManagerTest::SetUp() {
52 new PingManager(GURL("http://localhost2/update2"), context_
));
55 void ComponentUpdaterPingManagerTest::TearDown() {
56 ping_manager_
.reset();
59 void ComponentUpdaterPingManagerTest::RunThreadsUntilIdle() {
60 base::RunLoop().RunUntilIdle();
63 // Test is flaky: http://crbug.com/349547
64 TEST_F(ComponentUpdaterPingManagerTest
, DISABLED_PingManagerTest
) {
65 scoped_ptr
<InterceptorFactory
> interceptor_factory(new InterceptorFactory
);
66 URLRequestPostInterceptor
* interceptor
=
67 interceptor_factory
->CreateInterceptor();
68 EXPECT_TRUE(interceptor
);
70 // Test eventresult="1" is sent for successful updates.
73 item
.status
= CrxUpdateItem::kUpdated
;
74 item
.previous_version
= base::Version("1.0");
75 item
.next_version
= base::Version("2.0");
77 ping_manager_
->OnUpdateComplete(&item
);
78 base::RunLoop().RunUntilIdle();
80 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
81 EXPECT_NE(string::npos
,
82 interceptor
->GetRequests()[0].find(
83 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
84 "<event eventtype=\"3\" eventresult=\"1\"/></app>"))
85 << interceptor
->GetRequestsAsString();
88 // Test eventresult="0" is sent for failed updates.
89 item
= CrxUpdateItem();
91 item
.status
= CrxUpdateItem::kNoUpdate
;
92 item
.previous_version
= base::Version("1.0");
93 item
.next_version
= base::Version("2.0");
95 ping_manager_
->OnUpdateComplete(&item
);
96 base::RunLoop().RunUntilIdle();
98 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
99 EXPECT_NE(string::npos
,
100 interceptor
->GetRequests()[0].find(
101 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
102 "<event eventtype=\"3\" eventresult=\"0\"/></app>"))
103 << interceptor
->GetRequestsAsString();
104 interceptor
->Reset();
106 // Test the error values and the fingerprints.
107 item
= CrxUpdateItem();
109 item
.status
= CrxUpdateItem::kNoUpdate
;
110 item
.previous_version
= base::Version("1.0");
111 item
.next_version
= base::Version("2.0");
112 item
.previous_fp
= "prev fp";
113 item
.next_fp
= "next fp";
114 item
.error_category
= 1;
116 item
.extra_code1
= -1;
117 item
.diff_error_category
= 10;
118 item
.diff_error_code
= 20;
119 item
.diff_extra_code1
= -10;
120 item
.diff_update_failed
= true;
121 item
.crx_diffurls
.push_back(GURL("http://host/path"));
123 ping_manager_
->OnUpdateComplete(&item
);
124 base::RunLoop().RunUntilIdle();
126 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
127 EXPECT_NE(string::npos
,
128 interceptor
->GetRequests()[0].find(
129 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
130 "<event eventtype=\"3\" eventresult=\"0\" errorcat=\"1\" "
131 "errorcode=\"2\" extracode1=\"-1\" diffresult=\"0\" "
132 "differrorcat=\"10\" "
133 "differrorcode=\"20\" diffextracode1=\"-10\" "
134 "previousfp=\"prev fp\" nextfp=\"next fp\"/></app>"))
135 << interceptor
->GetRequestsAsString();
136 interceptor
->Reset();
138 // Test the download metrics.
139 item
= CrxUpdateItem();
141 item
.status
= CrxUpdateItem::kUpdated
;
142 item
.previous_version
= base::Version("1.0");
143 item
.next_version
= base::Version("2.0");
145 CrxDownloader::DownloadMetrics download_metrics
;
146 download_metrics
.url
= GURL("http://host1/path1");
147 download_metrics
.downloader
= CrxDownloader::DownloadMetrics::kUrlFetcher
;
148 download_metrics
.error
= -1;
149 download_metrics
.downloaded_bytes
= 123;
150 download_metrics
.total_bytes
= 456;
151 download_metrics
.download_time_ms
= 987;
152 item
.download_metrics
.push_back(download_metrics
);
154 download_metrics
= CrxDownloader::DownloadMetrics();
155 download_metrics
.url
= GURL("http://host2/path2");
156 download_metrics
.downloader
= CrxDownloader::DownloadMetrics::kBits
;
157 download_metrics
.error
= 0;
158 download_metrics
.downloaded_bytes
= 1230;
159 download_metrics
.total_bytes
= 4560;
160 download_metrics
.download_time_ms
= 9870;
161 item
.download_metrics
.push_back(download_metrics
);
163 ping_manager_
->OnUpdateComplete(&item
);
164 base::RunLoop().RunUntilIdle();
166 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
169 interceptor
->GetRequests()[0].find(
170 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
171 "<event eventtype=\"3\" eventresult=\"1\"/>"
172 "<event eventtype=\"14\" eventresult=\"0\" downloader=\"direct\" "
173 "errorcode=\"-1\" url=\"http://host1/path1\" downloaded=\"123\" "
174 "total=\"456\" download_time_ms=\"987\"/>"
175 "<event eventtype=\"14\" eventresult=\"1\" downloader=\"bits\" "
176 "url=\"http://host2/path2\" downloaded=\"1230\" total=\"4560\" "
177 "download_time_ms=\"9870\"/></app>"))
178 << interceptor
->GetRequestsAsString();
179 interceptor
->Reset();
182 } // namespace component_updater