Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: chrome/browser/component_updater/sw_reporter_installer_win_unittest.cc

Issue 2226133005: Add support for the ExperimentalSwReporterEngine field trial. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix case of flags constants Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/component_updater/sw_reporter_installer_win.h" 5 #include "chrome/browser/component_updater/sw_reporter_installer_win.h"
6 6
7 #include <map> 7 #include <map>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 19 matching lines...) Expand all
30 30
31 namespace component_updater { 31 namespace component_updater {
32 32
33 namespace { 33 namespace {
34 34
35 constexpr char kRegistrySuffixSwitch[] = "registry-suffix"; 35 constexpr char kRegistrySuffixSwitch[] = "registry-suffix";
36 constexpr char kErrorHistogramName[] = "SoftwareReporter.ExperimentErrors"; 36 constexpr char kErrorHistogramName[] = "SoftwareReporter.ExperimentErrors";
37 constexpr char kExperimentTag[] = "experiment_tag"; 37 constexpr char kExperimentTag[] = "experiment_tag";
38 constexpr char kMissingTag[] = "missing_tag"; 38 constexpr char kMissingTag[] = "missing_tag";
39 39
40 using safe_browsing::SwReporterInvocation;
41
40 } // namespace 42 } // namespace
41 43
42 class SwReporterInstallerTest : public ::testing::Test { 44 class SwReporterInstallerTest : public ::testing::Test {
43 public: 45 public:
44 SwReporterInstallerTest() 46 SwReporterInstallerTest()
45 : launched_callback_( 47 : launched_callback_(
46 base::Bind(&SwReporterInstallerTest::SwReporterLaunched, 48 base::Bind(&SwReporterInstallerTest::SwReporterLaunched,
47 base::Unretained(this))), 49 base::Unretained(this))),
48 default_version_("1.2.3"), 50 default_version_("1.2.3"),
49 default_path_(L"C:\\full\\path\\to\\download") {} 51 default_path_(L"C:\\full\\path\\to\\download"),
52 launched_version_("0.0.0") {}
50 53
51 ~SwReporterInstallerTest() override {} 54 ~SwReporterInstallerTest() override {}
52 55
53 protected: 56 protected:
54 void SwReporterLaunched(const safe_browsing::SwReporterInvocation& invocation, 57 void SwReporterLaunched(const safe_browsing::SwReporterQueue& invocations,
55 const base::Version& version) { 58 const base::Version& version) {
56 ASSERT_TRUE(launched_invocations_.empty()); 59 ASSERT_TRUE(launched_invocations_.empty());
57 launched_invocations_.push_back(invocation); 60 auto mutable_invocations = invocations;
Sorin Jianu 2016/09/01 15:59:09 Can we use an algorithm to filter out items instea
Joe Mason 2016/09/02 02:24:30 You can't use an algorithm on std::queue because i
61 while (!mutable_invocations.empty()) {
62 launched_invocations_.push_back(mutable_invocations.front());
63 mutable_invocations.pop();
64 }
58 launched_version_ = version; 65 launched_version_ = version;
59 } 66 }
60 67
61 base::FilePath MakeTestFilePath(const base::FilePath& path) const { 68 base::FilePath MakeTestFilePath(const base::FilePath& path) const {
62 return path.Append(L"software_reporter_tool.exe"); 69 return path.Append(L"software_reporter_tool.exe");
63 } 70 }
64 71
65 void ExpectEmptyAttributes(const SwReporterInstallerTraits& traits) const { 72 void ExpectEmptyAttributes(const SwReporterInstallerTraits& traits) const {
66 update_client::InstallerAttributes attributes = 73 update_client::InstallerAttributes attributes =
67 traits.GetInstallerAttributes(); 74 traits.GetInstallerAttributes();
68 EXPECT_TRUE(attributes.empty()); 75 EXPECT_TRUE(attributes.empty());
69 } 76 }
70 77
71 // Expects that the SwReporter was launched exactly once, with no arguments. 78 // Expects that the SwReporter was launched exactly once, with no arguments.
72 void ExpectDefaultInvocation() const { 79 void ExpectDefaultInvocation() const {
73 EXPECT_EQ(default_version_, launched_version_); 80 EXPECT_EQ(default_version_, launched_version_);
74 ASSERT_EQ(1U, launched_invocations_.size()); 81 ASSERT_EQ(1U, launched_invocations_.size());
75 82
76 const safe_browsing::SwReporterInvocation& invocation = 83 const SwReporterInvocation& invocation = launched_invocations_[0];
77 launched_invocations_[0];
78 EXPECT_EQ(MakeTestFilePath(default_path_), 84 EXPECT_EQ(MakeTestFilePath(default_path_),
79 invocation.command_line.GetProgram()); 85 invocation.command_line.GetProgram());
80 EXPECT_TRUE(invocation.command_line.GetSwitches().empty()); 86 EXPECT_TRUE(invocation.command_line.GetSwitches().empty());
81 EXPECT_TRUE(invocation.command_line.GetArgs().empty()); 87 EXPECT_TRUE(invocation.command_line.GetArgs().empty());
82 EXPECT_TRUE(invocation.suffix.empty()); 88 EXPECT_TRUE(invocation.suffix.empty());
83 EXPECT_FALSE(invocation.is_experimental); 89 EXPECT_EQ(SwReporterInvocation::FLAG_LOG_TO_RAPPOR |
90 SwReporterInvocation::FLAG_LOG_TO_PREFS |
91 SwReporterInvocation::FLAG_TRIGGER_PROMPT,
92 invocation.flags);
84 } 93 }
85 94
86 // |ComponentReady| asserts that it is run on the UI thread, so we must 95 // |ComponentReady| asserts that it is run on the UI thread, so we must
87 // create test threads before calling it. 96 // create test threads before calling it.
88 content::TestBrowserThreadBundle threads_; 97 content::TestBrowserThreadBundle threads_;
89 98
90 // Bound callback to the |SwReporterLaunched| method. 99 // Bound callback to the |SwReporterLaunched| method.
91 SwReporterRunner launched_callback_; 100 SwReporterRunner launched_callback_;
92 101
93 // Default parameters for |ComponentReady|. 102 // Default parameters for |ComponentReady|.
94 base::Version default_version_; 103 base::Version default_version_;
95 base::FilePath default_path_; 104 base::FilePath default_path_;
96 105
97 // Results of running |ComponentReady|. 106 // Results of running |ComponentReady|.
98 std::vector<safe_browsing::SwReporterInvocation> launched_invocations_; 107 std::vector<SwReporterInvocation> launched_invocations_;
99 base::Version launched_version_; 108 base::Version launched_version_;
100 109
101 private: 110 private:
102 DISALLOW_COPY_AND_ASSIGN(SwReporterInstallerTest); 111 DISALLOW_COPY_AND_ASSIGN(SwReporterInstallerTest);
103 }; 112 };
104 113
105 // This class contains extended setup that is only used for tests of the 114 // This class contains extended setup that is only used for tests of the
106 // experimental reporter. 115 // experimental reporter.
107 class ExperimentalSwReporterInstallerTest : public SwReporterInstallerTest { 116 class ExperimentalSwReporterInstallerTest : public SwReporterInstallerTest {
108 public: 117 public:
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 // |expected_suffix| and one |expected_additional_argument| on the 163 // |expected_suffix| and one |expected_additional_argument| on the
155 // command-line. (|expected_additional_argument| mainly exists to test that 164 // command-line. (|expected_additional_argument| mainly exists to test that
156 // arguments are included at all, so there is no need to test for 165 // arguments are included at all, so there is no need to test for
157 // combinations of multiple arguments and switches in this function.) 166 // combinations of multiple arguments and switches in this function.)
158 void ExpectExperimentalInvocation( 167 void ExpectExperimentalInvocation(
159 const std::string& expected_suffix, 168 const std::string& expected_suffix,
160 const base::string16& expected_additional_argument) { 169 const base::string16& expected_additional_argument) {
161 EXPECT_EQ(default_version_, launched_version_); 170 EXPECT_EQ(default_version_, launched_version_);
162 ASSERT_EQ(1U, launched_invocations_.size()); 171 ASSERT_EQ(1U, launched_invocations_.size());
163 172
164 const safe_browsing::SwReporterInvocation& invocation = 173 const SwReporterInvocation& invocation = launched_invocations_[0];
165 launched_invocations_[0];
166 EXPECT_EQ(MakeTestFilePath(default_path_), 174 EXPECT_EQ(MakeTestFilePath(default_path_),
167 invocation.command_line.GetProgram()); 175 invocation.command_line.GetProgram());
168 176
169 if (expected_suffix.empty()) { 177 if (expected_suffix.empty()) {
170 EXPECT_TRUE(invocation.command_line.GetSwitches().empty()); 178 EXPECT_TRUE(invocation.command_line.GetSwitches().empty());
171 EXPECT_TRUE(invocation.suffix.empty()); 179 EXPECT_TRUE(invocation.suffix.empty());
172 } else { 180 } else {
173 EXPECT_EQ(1U, invocation.command_line.GetSwitches().size()); 181 EXPECT_EQ(1U, invocation.command_line.GetSwitches().size());
174 EXPECT_EQ(expected_suffix, invocation.command_line.GetSwitchValueASCII( 182 EXPECT_EQ(expected_suffix, invocation.command_line.GetSwitchValueASCII(
175 kRegistrySuffixSwitch)); 183 kRegistrySuffixSwitch));
176 EXPECT_EQ(expected_suffix, invocation.suffix); 184 EXPECT_EQ(expected_suffix, invocation.suffix);
177 } 185 }
178 186
179 if (expected_additional_argument.empty()) { 187 if (expected_additional_argument.empty()) {
180 EXPECT_TRUE(invocation.command_line.GetArgs().empty()); 188 EXPECT_TRUE(invocation.command_line.GetArgs().empty());
181 } else { 189 } else {
182 EXPECT_EQ(1U, invocation.command_line.GetArgs().size()); 190 EXPECT_EQ(1U, invocation.command_line.GetArgs().size());
183 EXPECT_EQ(expected_additional_argument, 191 EXPECT_EQ(expected_additional_argument,
184 invocation.command_line.GetArgs()[0]); 192 invocation.command_line.GetArgs()[0]);
185 } 193 }
186 194
187 EXPECT_TRUE(invocation.is_experimental); 195 EXPECT_EQ(0, invocation.flags);
188 histograms_.ExpectTotalCount(kErrorHistogramName, 0); 196 histograms_.ExpectTotalCount(kErrorHistogramName, 0);
189 } 197 }
190 198
191 void ExpectLaunchError() { 199 void ExpectLaunchError() {
192 // The SwReporter should not be launched, and an error should be logged. 200 // The SwReporter should not be launched, and an error should be logged.
193 EXPECT_TRUE(launched_invocations_.empty()); 201 EXPECT_TRUE(launched_invocations_.empty());
194 histograms_.ExpectUniqueSample(kErrorHistogramName, 202 histograms_.ExpectUniqueSample(kErrorHistogramName,
195 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1); 203 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1);
196 } 204 }
197 205
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 275
268 TEST_F(ExperimentalSwReporterInstallerTest, SingleInvocation) { 276 TEST_F(ExperimentalSwReporterInstallerTest, SingleInvocation) {
269 SwReporterInstallerTraits traits(launched_callback_, true); 277 SwReporterInstallerTraits traits(launched_callback_, true);
270 CreateFeatureWithTag(kExperimentTag); 278 CreateFeatureWithTag(kExperimentTag);
271 ExpectAttributesWithTag(traits, kExperimentTag); 279 ExpectAttributesWithTag(traits, kExperimentTag);
272 280
273 static constexpr char kTestManifest[] = 281 static constexpr char kTestManifest[] =
274 "{\"launch_params\": [" 282 "{\"launch_params\": ["
275 " {" 283 " {"
276 " \"arguments\": [\"--engine=experimental\", \"random argument\"]," 284 " \"arguments\": [\"--engine=experimental\", \"random argument\"],"
277 " \"suffix\": \"TestSuffix\"" 285 " \"suffix\": \"TestSuffix\","
286 " \"prompt\": false"
278 " }" 287 " }"
279 "]}"; 288 "]}";
280 traits.ComponentReady( 289 traits.ComponentReady(
281 default_version_, default_path_, 290 default_version_, default_path_,
282 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest))); 291 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest)));
283 292
284 // The SwReporter should be launched once with the given arguments. 293 // The SwReporter should be launched once with the given arguments.
285 EXPECT_EQ(default_version_, launched_version_); 294 EXPECT_EQ(default_version_, launched_version_);
286 ASSERT_EQ(1U, launched_invocations_.size()); 295 ASSERT_EQ(1U, launched_invocations_.size());
287 296
288 const safe_browsing::SwReporterInvocation& invocation = 297 const SwReporterInvocation& invocation = launched_invocations_[0];
289 launched_invocations_[0];
290 EXPECT_EQ(MakeTestFilePath(default_path_), 298 EXPECT_EQ(MakeTestFilePath(default_path_),
291 invocation.command_line.GetProgram()); 299 invocation.command_line.GetProgram());
292 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size()); 300 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size());
293 EXPECT_EQ("experimental", 301 EXPECT_EQ("experimental",
294 invocation.command_line.GetSwitchValueASCII("engine")); 302 invocation.command_line.GetSwitchValueASCII("engine"));
295 EXPECT_EQ("TestSuffix", 303 EXPECT_EQ("TestSuffix",
296 invocation.command_line.GetSwitchValueASCII(kRegistrySuffixSwitch)); 304 invocation.command_line.GetSwitchValueASCII(kRegistrySuffixSwitch));
297 ASSERT_EQ(1U, invocation.command_line.GetArgs().size()); 305 ASSERT_EQ(1U, invocation.command_line.GetArgs().size());
298 EXPECT_EQ(L"random argument", invocation.command_line.GetArgs()[0]); 306 EXPECT_EQ(L"random argument", invocation.command_line.GetArgs()[0]);
299 EXPECT_EQ("TestSuffix", invocation.suffix); 307 EXPECT_EQ("TestSuffix", invocation.suffix);
300 EXPECT_TRUE(invocation.is_experimental); 308 EXPECT_EQ(0, invocation.flags);
301 histograms_.ExpectTotalCount(kErrorHistogramName, 0); 309 histograms_.ExpectTotalCount(kErrorHistogramName, 0);
302 } 310 }
303 311
304 TEST_F(ExperimentalSwReporterInstallerTest, MultipleInvocations) { 312 TEST_F(ExperimentalSwReporterInstallerTest, MultipleInvocations) {
305 SwReporterInstallerTraits traits(launched_callback_, true); 313 SwReporterInstallerTraits traits(launched_callback_, true);
306 CreateFeatureWithTag(kExperimentTag); 314 CreateFeatureWithTag(kExperimentTag);
307 ExpectAttributesWithTag(traits, kExperimentTag); 315 ExpectAttributesWithTag(traits, kExperimentTag);
308 316
309 static constexpr char kTestManifest[] = 317 static constexpr char kTestManifest[] =
310 "{\"launch_params\": [" 318 "{\"launch_params\": ["
311 " {" 319 " {"
312 " \"arguments\": [\"--engine=experimental\", \"random argument\"]," 320 " \"arguments\": [\"--engine=experimental\", \"random argument\"],"
313 " \"suffix\": \"TestSuffix\"" 321 " \"suffix\": \"TestSuffix\","
322 " \"prompt\": false"
314 " }," 323 " },"
315 " {" 324 " {"
316 " \"arguments\": [\"--engine=second\"]," 325 " \"arguments\": [\"--engine=second\"],"
317 " \"suffix\": \"SecondSuffix\"" 326 " \"suffix\": \"SecondSuffix\","
327 " \"prompt\": true"
328 " },"
329 " {"
330 " \"arguments\": [\"--engine=third\"],"
331 " \"suffix\": \"ThirdSuffix\""
318 " }" 332 " }"
319 "]}"; 333 "]}";
320 traits.ComponentReady( 334 traits.ComponentReady(
321 default_version_, default_path_, 335 default_version_, default_path_,
322 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest))); 336 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest)));
323 337
324 // Not supported yet. 338 // The SwReporter should be launched three times with the given arguments.
325 ExpectLaunchError(); 339 EXPECT_EQ(default_version_, launched_version_);
340 ASSERT_EQ(3U, launched_invocations_.size());
341
342 {
343 const SwReporterInvocation& invocation = launched_invocations_[0];
344 EXPECT_EQ(MakeTestFilePath(default_path_),
345 invocation.command_line.GetProgram());
346 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size());
347 EXPECT_EQ("experimental",
348 invocation.command_line.GetSwitchValueASCII("engine"));
349 EXPECT_EQ("TestSuffix", invocation.command_line.GetSwitchValueASCII(
350 kRegistrySuffixSwitch));
351 ASSERT_EQ(1U, invocation.command_line.GetArgs().size());
352 EXPECT_EQ(L"random argument", invocation.command_line.GetArgs()[0]);
353 EXPECT_EQ("TestSuffix", invocation.suffix);
354 EXPECT_EQ(0, invocation.flags);
355 }
356
357 {
358 const SwReporterInvocation& invocation = launched_invocations_[1];
359 EXPECT_EQ(MakeTestFilePath(default_path_),
360 invocation.command_line.GetProgram());
361 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size());
362 EXPECT_EQ("second", invocation.command_line.GetSwitchValueASCII("engine"));
363 EXPECT_EQ("SecondSuffix", invocation.command_line.GetSwitchValueASCII(
364 kRegistrySuffixSwitch));
365 ASSERT_TRUE(invocation.command_line.GetArgs().empty());
366 EXPECT_EQ("SecondSuffix", invocation.suffix);
367 EXPECT_EQ(SwReporterInvocation::FLAG_TRIGGER_PROMPT, invocation.flags);
368 }
369
370 {
371 const SwReporterInvocation& invocation = launched_invocations_[2];
372 EXPECT_EQ(MakeTestFilePath(default_path_),
373 invocation.command_line.GetProgram());
374 EXPECT_EQ(2U, invocation.command_line.GetSwitches().size());
375 EXPECT_EQ("third", invocation.command_line.GetSwitchValueASCII("engine"));
376 EXPECT_EQ("ThirdSuffix", invocation.command_line.GetSwitchValueASCII(
377 kRegistrySuffixSwitch));
378 ASSERT_TRUE(invocation.command_line.GetArgs().empty());
379 EXPECT_EQ("ThirdSuffix", invocation.suffix);
380 // A missing "prompt" key means "false".
381 EXPECT_EQ(0, invocation.flags);
382 }
383
384 histograms_.ExpectTotalCount(kErrorHistogramName, 0);
326 } 385 }
327 386
328 TEST_F(ExperimentalSwReporterInstallerTest, MissingSuffix) { 387 TEST_F(ExperimentalSwReporterInstallerTest, MissingSuffix) {
329 SwReporterInstallerTraits traits(launched_callback_, true); 388 SwReporterInstallerTraits traits(launched_callback_, true);
330 CreateFeatureWithTag(kExperimentTag); 389 CreateFeatureWithTag(kExperimentTag);
331 390
332 static constexpr char kTestManifest[] = 391 static constexpr char kTestManifest[] =
333 "{\"launch_params\": [" 392 "{\"launch_params\": ["
334 " {" 393 " {"
335 " \"arguments\": [\"random argument\"]" 394 " \"arguments\": [\"random argument\"]"
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 traits.ComponentReady( 675 traits.ComponentReady(
617 default_version_, default_path_, 676 default_version_, default_path_,
618 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest))); 677 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest)));
619 678
620 // The SwReporter should not be launched, and an error should be logged. 679 // The SwReporter should not be launched, and an error should be logged.
621 EXPECT_TRUE(launched_invocations_.empty()); 680 EXPECT_TRUE(launched_invocations_.empty());
622 histograms_.ExpectUniqueSample(kErrorHistogramName, 681 histograms_.ExpectUniqueSample(kErrorHistogramName,
623 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1); 682 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1);
624 } 683 }
625 684
685 TEST_F(ExperimentalSwReporterInstallerTest, BadTypesInManifest4) {
686 SwReporterInstallerTraits traits(launched_callback_, true);
687 CreateFeatureWithTag(kExperimentTag);
688
689 // This has an int instead of a bool for prompt.
690 static constexpr char kTestManifest[] =
691 "{\"launch_params\": ["
692 " {"
693 " \"arguments\": [\"--engine=experimental\"],"
694 " \"suffix\": \"TestSuffix\","
695 " \"prompt\": 1"
696 " }"
697 "]}";
698 traits.ComponentReady(
699 default_version_, default_path_,
700 base::DictionaryValue::From(base::JSONReader::Read(kTestManifest)));
701
702 // The SwReporter should not be launched, and an error should be logged.
703 EXPECT_TRUE(launched_invocations_.empty());
704 histograms_.ExpectUniqueSample(kErrorHistogramName,
705 SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS, 1);
706 }
626 } // namespace component_updater 707 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698