segmentation_platform_profile_observer_unittest.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "chrome/browser/segmentation_platform/segmentation_platform_profile_observer.h"
  5. #include "chrome/browser/profiles/profile_destroyer.h"
  6. #include "chrome/test/base/testing_browser_process.h"
  7. #include "chrome/test/base/testing_profile.h"
  8. #include "chrome/test/base/testing_profile_manager.h"
  9. #include "components/segmentation_platform/public/input_context.h"
  10. #include "components/segmentation_platform/public/segment_selection_result.h"
  11. #include "components/segmentation_platform/public/segmentation_platform_service.h"
  12. #include "content/public/test/browser_task_environment.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. using testing::_;
  16. namespace segmentation_platform {
  17. namespace {
  18. constexpr char kProfile1[] = "profile-1";
  19. constexpr char kProfile2[] = "profile-2";
  20. } // namespace
  21. class MockSegmentationPlatformService : public SegmentationPlatformService {
  22. public:
  23. MockSegmentationPlatformService() = default;
  24. ~MockSegmentationPlatformService() override = default;
  25. MOCK_METHOD(void,
  26. GetSelectedSegment,
  27. (const std::string&, SegmentSelectionCallback));
  28. MOCK_METHOD(SegmentSelectionResult,
  29. GetCachedSegmentResult,
  30. (const std::string&));
  31. MOCK_METHOD(void,
  32. GetSelectedSegmentOnDemand,
  33. (const std::string&,
  34. scoped_refptr<InputContext>,
  35. SegmentSelectionCallback));
  36. MOCK_METHOD(void, EnableMetrics, (bool));
  37. MOCK_METHOD(void, GetServiceStatus, ());
  38. MOCK_METHOD(bool, IsPlatformInitialized, ());
  39. };
  40. class SegmentationPlatformProfileObserverTest : public testing::Test {
  41. public:
  42. SegmentationPlatformProfileObserverTest() = default;
  43. ~SegmentationPlatformProfileObserverTest() override = default;
  44. protected:
  45. void SetUp() override {
  46. testing_profile_manager_ = std::make_unique<TestingProfileManager>(
  47. TestingBrowserProcess::GetGlobal());
  48. ASSERT_TRUE(testing_profile_manager_->SetUp());
  49. }
  50. void TearDown() override {
  51. segmentation_platform_profile_observer_.reset();
  52. testing_profile_manager_.reset();
  53. }
  54. void StartObservingProfiles() {
  55. segmentation_platform_profile_observer_ =
  56. std::make_unique<SegmentationPlatformProfileObserver>(
  57. &segmentation_platform_service_,
  58. testing_profile_manager_->profile_manager());
  59. }
  60. content::BrowserTaskEnvironment task_environment_;
  61. std::unique_ptr<TestingProfileManager> testing_profile_manager_;
  62. MockSegmentationPlatformService segmentation_platform_service_;
  63. std::unique_ptr<SegmentationPlatformProfileObserver>
  64. segmentation_platform_profile_observer_;
  65. };
  66. TEST_F(SegmentationPlatformProfileObserverTest,
  67. StartWithDefaultProfileAndAddOneOrMoreOTRProfiles) {
  68. // Start with a default profile.
  69. TestingProfile* profile1 =
  70. testing_profile_manager_->CreateTestingProfile(kProfile1);
  71. // Start observing profiles. The service should be notified with metrics
  72. // enabled.
  73. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(true)).Times(1);
  74. StartObservingProfiles();
  75. // Create another profile. The signal collection should just continue, so no
  76. // further notification to the service.
  77. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(_)).Times(0);
  78. Profile* profile2 = testing_profile_manager_->CreateTestingProfile(kProfile2);
  79. // Start an OTR profile. The signal collection should stop.
  80. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(false)).Times(1);
  81. Profile* profile1_otr1 =
  82. profile1->GetPrimaryOTRProfile(/*create_if_needed*/ true);
  83. // Kill the OTR profile. The signal collection resumes.
  84. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(true)).Times(1);
  85. ProfileDestroyer::DestroyProfileWhenAppropriate(profile1_otr1);
  86. // Start again another OTR profile. The signal collection should stop.
  87. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(false)).Times(1);
  88. Profile* profile1_otr2 =
  89. profile1->GetPrimaryOTRProfile(/*create_if_needed*/ true);
  90. // Add one more OTR for profile2.
  91. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(_)).Times(0);
  92. Profile* profile2_otr1 =
  93. profile2->GetPrimaryOTRProfile(/*create_if_needed*/ true);
  94. // Add a secondary OTR for profile1.
  95. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(_)).Times(0);
  96. Profile* profile1_otr2_secondary = profile1->GetOffTheRecordProfile(
  97. Profile::OTRProfileID::CreateUniqueForTesting(),
  98. /*create_if_needed=*/true);
  99. // Start killing the OTR profiles one by one. The signal collection resumes
  100. // only after the last one is killed.
  101. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(_)).Times(0);
  102. ProfileDestroyer::DestroyProfileWhenAppropriate(profile1_otr2_secondary);
  103. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(_)).Times(0);
  104. ProfileDestroyer::DestroyProfileWhenAppropriate(profile1_otr2);
  105. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(true)).Times(1);
  106. ProfileDestroyer::DestroyProfileWhenAppropriate(profile2_otr1);
  107. // Cleanup profiles.
  108. testing_profile_manager_->DeleteTestingProfile(kProfile1);
  109. testing_profile_manager_->DeleteTestingProfile(kProfile2);
  110. }
  111. TEST_F(SegmentationPlatformProfileObserverTest, StartWithOTRProfile) {
  112. // Start with a default profile and an OTR profile.
  113. TestingProfile* profile =
  114. testing_profile_manager_->CreateTestingProfile(kProfile1);
  115. Profile* otr = profile->GetPrimaryOTRProfile(/*create_if_needed*/ true);
  116. // Start observing profiles. The service should be notified with metrics
  117. // disabled.
  118. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(false)).Times(1);
  119. StartObservingProfiles();
  120. // Kill the OTR profile. The signal collection resumes.
  121. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(true)).Times(1);
  122. ProfileDestroyer::DestroyProfileWhenAppropriate(otr);
  123. // Cleanup profiles.
  124. EXPECT_CALL(segmentation_platform_service_, EnableMetrics(_)).Times(0);
  125. testing_profile_manager_->DeleteTestingProfile(kProfile1);
  126. }
  127. } // namespace segmentation_platform