feedback_common_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2014 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.
  4. #include "components/feedback/feedback_common.h"
  5. #include "base/bind.h"
  6. #include "build/chromeos_buildflags.h"
  7. #include "components/feedback/feedback_report.h"
  8. #include "components/feedback/proto/common.pb.h"
  9. #include "components/feedback/proto/dom.pb.h"
  10. #include "components/feedback/proto/extension.pb.h"
  11. #include "components/feedback/proto/math.pb.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace {
  14. constexpr char kOne[] = "one";
  15. constexpr char kTwo[] = "two";
  16. constexpr char kThree[] = "three";
  17. constexpr char kFour[] = "four";
  18. #define TEN_LINES "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n"
  19. constexpr char kLongLog[] = TEN_LINES TEN_LINES TEN_LINES TEN_LINES TEN_LINES;
  20. constexpr char kLogsAttachmentName[] = "system_logs.zip";
  21. constexpr int kTestProductId = 3490;
  22. #if BUILDFLAG(IS_CHROMEOS_ASH)
  23. constexpr int kDefaultProductId = 208; // ChromeOS default product ID.
  24. #else
  25. constexpr int kDefaultProductId = 237; // Chrome default product ID.
  26. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  27. } // namespace
  28. class FeedbackCommonTest : public testing::Test {
  29. protected:
  30. FeedbackCommonTest() : feedback_(new FeedbackCommon()) {}
  31. ~FeedbackCommonTest() override {}
  32. void CompressLogs() { feedback_->CompressLogs(); }
  33. bool FeedbackHasProductId() const { return feedback_->HasProductId(); }
  34. scoped_refptr<FeedbackCommon> feedback_;
  35. userfeedback::ExtensionSubmit report_;
  36. };
  37. TEST_F(FeedbackCommonTest, TestBasicData) {
  38. // Test that basic data can be set and propagates to the request.
  39. feedback_->set_category_tag(kOne);
  40. feedback_->set_description(kTwo);
  41. feedback_->set_page_url(kThree);
  42. feedback_->set_user_email(kFour);
  43. EXPECT_FALSE(FeedbackHasProductId());
  44. feedback_->set_product_id(kTestProductId);
  45. EXPECT_TRUE(FeedbackHasProductId());
  46. feedback_->PrepareReport(&report_);
  47. EXPECT_EQ(kOne, report_.bucket());
  48. EXPECT_EQ(kTwo, report_.common_data().description());
  49. EXPECT_EQ(kThree, report_.web_data().url());
  50. EXPECT_EQ(kFour, report_.common_data().user_email());
  51. EXPECT_EQ(kTestProductId, report_.product_id());
  52. }
  53. // If an feedback requester doesn't set the product ID, the report will be sent
  54. // with the default product ID for Chrome/ChromeOS depending on the platform.
  55. TEST_F(FeedbackCommonTest, TestDefaultProductId) {
  56. EXPECT_FALSE(FeedbackHasProductId());
  57. feedback_->PrepareReport(&report_);
  58. EXPECT_EQ(kDefaultProductId, report_.product_id());
  59. }
  60. TEST_F(FeedbackCommonTest, TestAddLogs) {
  61. feedback_->AddLog(kOne, kTwo);
  62. feedback_->AddLog(kThree, kFour);
  63. EXPECT_EQ(2U, feedback_->sys_info()->size());
  64. }
  65. TEST_F(FeedbackCommonTest, TestCompressionThreshold) {
  66. // Add a large and small log, verify that only the small log gets
  67. // included in the report.
  68. feedback_->AddLog(kOne, kTwo);
  69. feedback_->AddLog(kThree, kLongLog);
  70. feedback_->PrepareReport(&report_);
  71. EXPECT_EQ(1, report_.web_data().product_specific_data_size());
  72. EXPECT_EQ(kOne, report_.web_data().product_specific_data(0).key());
  73. }
  74. TEST_F(FeedbackCommonTest, TestCompression) {
  75. // Add a large and small log, verify that an attachment has been
  76. // added with the right name.
  77. feedback_->AddLog(kOne, kTwo);
  78. feedback_->AddLog(kThree, kLongLog);
  79. CompressLogs();
  80. feedback_->PrepareReport(&report_);
  81. EXPECT_EQ(1, report_.product_specific_binary_data_size());
  82. EXPECT_EQ(kLogsAttachmentName,
  83. report_.product_specific_binary_data(0).name());
  84. }
  85. TEST_F(FeedbackCommonTest, TestAllCrashIdsRemoval) {
  86. feedback_->AddLog(feedback::FeedbackReport::kAllCrashReportIdsKey, kOne);
  87. feedback_->set_user_email("nobody@example.com");
  88. feedback_->PrepareReport(&report_);
  89. EXPECT_EQ(0, report_.web_data().product_specific_data_size());
  90. }
  91. TEST_F(FeedbackCommonTest, TestAllCrashIdsRetention) {
  92. feedback_->AddLog(feedback::FeedbackReport::kAllCrashReportIdsKey, kOne);
  93. feedback_->set_user_email("nobody@google.com");
  94. feedback_->PrepareReport(&report_);
  95. EXPECT_EQ(1, report_.web_data().product_specific_data_size());
  96. }
  97. TEST_F(FeedbackCommonTest, IncludeInSystemLogs) {
  98. bool google_email = true;
  99. EXPECT_TRUE(FeedbackCommon::IncludeInSystemLogs(kOne, google_email));
  100. EXPECT_TRUE(FeedbackCommon::IncludeInSystemLogs(
  101. feedback::FeedbackReport::kAllCrashReportIdsKey, google_email));
  102. google_email = false;
  103. EXPECT_TRUE(FeedbackCommon::IncludeInSystemLogs(kOne, google_email));
  104. EXPECT_FALSE(FeedbackCommon::IncludeInSystemLogs(
  105. feedback::FeedbackReport::kAllCrashReportIdsKey, google_email));
  106. }