report_queue_factory_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2021 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/reporting/client/report_queue_factory.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "base/logging.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/test/task_environment.h"
  11. #include "components/reporting/client/mock_report_queue.h"
  12. #include "components/reporting/client/mock_report_queue_provider.h"
  13. #include "components/reporting/client/report_queue.h"
  14. #include "components/reporting/client/report_queue_provider_test_helper.h"
  15. #include "components/reporting/util/test_support_callbacks.h"
  16. #include "testing/gmock/include/gmock/gmock.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. using ::testing::_;
  19. using ::testing::HasSubstr;
  20. using ::testing::IsNull;
  21. using ::testing::NiceMock;
  22. using ::testing::NotNull;
  23. using ::testing::Return;
  24. namespace reporting {
  25. class MockReportQueueConsumer {
  26. public:
  27. MockReportQueueConsumer() = default;
  28. void SetReportQueue(test::TestCallbackWaiter* waiter,
  29. std::unique_ptr<ReportQueue> report_queue) {
  30. report_queue_ = std::move(report_queue);
  31. if (waiter) {
  32. waiter->Signal();
  33. }
  34. }
  35. base::OnceCallback<void(std::unique_ptr<ReportQueue>)> GetReportQueueSetter(
  36. test::TestCallbackWaiter* waiter) {
  37. return base::BindOnce(&MockReportQueueConsumer::SetReportQueue,
  38. weak_factory_.GetWeakPtr(), base::Unretained(waiter));
  39. }
  40. ReportQueue* GetReportQueue() const { return report_queue_.get(); }
  41. private:
  42. std::unique_ptr<ReportQueue> report_queue_;
  43. base::WeakPtrFactory<MockReportQueueConsumer> weak_factory_{this};
  44. };
  45. class ReportQueueFactoryTest : public ::testing::Test {
  46. protected:
  47. ReportQueueFactoryTest() = default;
  48. void SetUp() override {
  49. consumer_ = std::make_unique<MockReportQueueConsumer>();
  50. provider_ = std::make_unique<NiceMock<MockReportQueueProvider>>();
  51. report_queue_provider_test_helper::SetForTesting(provider_.get());
  52. }
  53. void TearDown() override {
  54. task_environment_.RunUntilIdle(); // Drain remaining scheduled tasks.
  55. report_queue_provider_test_helper::SetForTesting(nullptr);
  56. }
  57. const Destination destination_ = Destination::UPLOAD_EVENTS;
  58. base::test::TaskEnvironment task_environment_{
  59. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  60. std::unique_ptr<MockReportQueueConsumer> consumer_;
  61. std::unique_ptr<MockReportQueueProvider> provider_;
  62. };
  63. TEST_F(ReportQueueFactoryTest, CreateAndGetQueue) {
  64. // Initially the queue must be an uninitialized unique_ptr
  65. EXPECT_FALSE(consumer_->GetReportQueue());
  66. {
  67. test::TestCallbackAutoWaiter set_waiter;
  68. ReportQueueFactory::Create(EventType::kDevice, destination_,
  69. consumer_->GetReportQueueSetter(&set_waiter));
  70. EXPECT_CALL(*provider_.get(), OnInitCompletedMock()).Times(1);
  71. provider_->ExpectCreateNewQueueAndReturnNewMockQueue(1);
  72. }
  73. // We expect the report queue to be existing in the consumer.
  74. EXPECT_TRUE(consumer_->GetReportQueue());
  75. }
  76. TEST_F(ReportQueueFactoryTest, CreateQueueWithInvalidConfig) {
  77. // Initially the queue must be an uninitialized unique_ptr
  78. EXPECT_FALSE(consumer_->GetReportQueue());
  79. ReportQueueFactory::Create(EventType::kDevice,
  80. Destination::UNDEFINED_DESTINATION,
  81. consumer_->GetReportQueueSetter(nullptr));
  82. // Expect failure before it gets to the report queue provider
  83. EXPECT_CALL(*provider_.get(), OnInitCompletedMock()).Times(0);
  84. // We do not expect the report queue to be existing in the consumer.
  85. EXPECT_FALSE(consumer_->GetReportQueue());
  86. }
  87. TEST_F(ReportQueueFactoryTest, CreateSpeculativeQueue) {
  88. // Mock internal implementation to use a MockReportQueue
  89. provider_->ExpectCreateNewSpeculativeQueueAndReturnNewMockQueue(1);
  90. const auto report_queue = ReportQueueFactory::CreateSpeculativeReportQueue(
  91. EventType::kDevice, destination_);
  92. EXPECT_THAT(report_queue, NotNull());
  93. }
  94. TEST_F(ReportQueueFactoryTest, CreateSpeculativeQueueWithInvalidConfig) {
  95. const auto report_queue = ReportQueueFactory::CreateSpeculativeReportQueue(
  96. EventType::kDevice, Destination::UNDEFINED_DESTINATION);
  97. EXPECT_THAT(report_queue, IsNull());
  98. }
  99. // Tests if two consumers use the same provider and create two queues.
  100. TEST_F(ReportQueueFactoryTest, SameProviderForMultipleThreads) {
  101. auto consumer2 = std::make_unique<MockReportQueueConsumer>();
  102. EXPECT_FALSE(consumer_->GetReportQueue());
  103. EXPECT_FALSE(consumer2->GetReportQueue());
  104. {
  105. test::TestCallbackAutoWaiter set_waiter;
  106. set_waiter.Attach();
  107. ReportQueueFactory::Create(EventType::kDevice, destination_,
  108. consumer_->GetReportQueueSetter(&set_waiter));
  109. ReportQueueFactory::Create(EventType::kUser, destination_,
  110. consumer2->GetReportQueueSetter(&set_waiter));
  111. EXPECT_CALL(*provider_.get(), OnInitCompletedMock()).Times(1);
  112. provider_->ExpectCreateNewQueueAndReturnNewMockQueue(2);
  113. }
  114. // We expect the report queue to be existing in the consumer.
  115. EXPECT_TRUE(consumer_->GetReportQueue());
  116. // And for the 2nd consumer
  117. EXPECT_TRUE(consumer2->GetReportQueue());
  118. }
  119. } // namespace reporting