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_
;
41 ComponentUpdaterPingManagerTest::ComponentUpdaterPingManagerTest()
42 : context_(new net::TestURLRequestContextGetter(
43 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
))),
44 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
47 ComponentUpdaterPingManagerTest::~ComponentUpdaterPingManagerTest() {
51 void ComponentUpdaterPingManagerTest::SetUp() {
52 ping_manager_
.reset(new PingManager(
53 GURL("http://localhost2/update2"), context_
));
56 void ComponentUpdaterPingManagerTest::TearDown() {
57 ping_manager_
.reset();
60 void ComponentUpdaterPingManagerTest::RunThreadsUntilIdle() {
61 base::RunLoop().RunUntilIdle();
64 TEST_F(ComponentUpdaterPingManagerTest
, 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
, interceptor
->GetRequests()[0].find(
82 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
83 "<event eventtype=\"3\" eventresult=\"1\"/></app>"))
84 << interceptor
->GetRequestsAsString();
87 // Test eventresult="0" is sent for failed updates.
88 item
= CrxUpdateItem();
90 item
.status
= CrxUpdateItem::kNoUpdate
;
91 item
.previous_version
= base::Version("1.0");
92 item
.next_version
= base::Version("2.0");
94 ping_manager_
->OnUpdateComplete(&item
);
95 base::RunLoop().RunUntilIdle();
97 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
98 EXPECT_NE(string::npos
, interceptor
->GetRequests()[0].find(
99 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
100 "<event eventtype=\"3\" eventresult=\"0\"/></app>"))
101 << interceptor
->GetRequestsAsString();
102 interceptor
->Reset();
104 // Test the error values and the fingerprints.
105 item
= CrxUpdateItem();
107 item
.status
= CrxUpdateItem::kNoUpdate
;
108 item
.previous_version
= base::Version("1.0");
109 item
.next_version
= base::Version("2.0");
110 item
.previous_fp
= "prev fp";
111 item
.next_fp
= "next fp";
112 item
.error_category
= 1;
114 item
.extra_code1
= -1;
115 item
.diff_error_category
= 10;
116 item
.diff_error_code
= 20;
117 item
.diff_extra_code1
= -10;
118 item
.diff_update_failed
= true;
119 item
.crx_diffurls
.push_back(GURL("http://host/path"));
121 ping_manager_
->OnUpdateComplete(&item
);
122 base::RunLoop().RunUntilIdle();
124 EXPECT_EQ(1, interceptor
->GetCount()) << interceptor
->GetRequestsAsString();
125 EXPECT_NE(string::npos
, interceptor
->GetRequests()[0].find(
126 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
127 "<event eventtype=\"3\" eventresult=\"0\" errorcat=\"1\" "
128 "errorcode=\"2\" extracode1=\"-1\" diffresult=\"0\" 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
.status
= CrxUpdateItem::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
.bytes_downloaded
= 123;
146 download_metrics
.bytes_total
= 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
.bytes_downloaded
= 1230;
155 download_metrics
.bytes_total
= 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();
163 EXPECT_NE(string::npos
, interceptor
->GetRequests()[0].find(
164 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
165 "<event eventtype=\"3\" eventresult=\"1\"/>"
166 "<event eventtype=\"14\" eventresult=\"0\" downloader=\"direct\" "
167 "errorcode=\"-1\" url=\"http://host1/path1\" downloaded=\"123\" "
168 "total=\"456\" download_time_ms=\"987\"/>"
169 "<event eventtype=\"14\" eventresult=\"1\" downloader=\"bits\" "
170 "url=\"http://host2/path2\" downloaded=\"1230\" total=\"4560\" "
171 "download_time_ms=\"9870\"/></app>"))
172 << interceptor
->GetRequestsAsString();
173 interceptor
->Reset();
176 } // namespace component_updater