File: privacy_sandbox_internals_handler_unittest.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (162 lines) | stat: -rw-r--r-- 6,280 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/privacy_sandbox/privacy_sandbox_internals_handler.h"

#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/ui/webui/privacy_sandbox/privacy_sandbox_internals.mojom.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/functions.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"

namespace privacy_sandbox_internals {
namespace {
using ::testing::_;
using ::testing::AllOf;
using ::testing::Each;
using ::testing::Field;
using ::testing::Pointee;
using ::testing::Property;
using ::testing::StartsWith;
using ::testing::StrEq;

using ReadPrefsWithPrefixesResultType = std::vector<
    privacy_sandbox_internals::mojom::PrivacySandboxInternalsPrefPtr>;

static const char kPrefixA[] = "foo.bar";
static const char kPrefixB[] = "bar.foo";
static const char kShortPrefixA[] = "foo";
static std::vector<std::string> kPrefixAPrefNames = {"foo.bar.baz1",
                                                     "foo.bar.baz2"};
static std::vector<std::string> kPrefixBPrefNames = {
    "bar.foo.moo1", "bar.foo.moo2", "bar.foo.moo3"};
static std::vector<std::string> kPrefixCPrefNames = {"moo.foo.baz"};
static const char kPrefAValue[] = "This is a prefix A pref value ";
static const char kPrefBValue[] = "This is a prefix B pref value ";
static const char kPrefCValue[] = "This is a prefix C pref value ";

class PrivacySandboxInternalsHandlerUnitTest : public testing::Test {
 public:
  PrivacySandboxInternalsHandlerUnitTest()
      : browser_task_environment_(
            base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}

  void SetUp() override {
    auto prefs_ =
        std::make_unique<sync_preferences::TestingPrefServiceSyncable>();
    RegisterUserProfilePrefs(prefs_->registry());
    RegisterTestPrefs(prefs_->registry());

    TestingProfile::Builder builder;
    builder.SetPrefService(std::move(prefs_));
    profile_ = builder.Build();

    handler_ = std::make_unique<PrivacySandboxInternalsHandler>(
        profile_.get(), remote_.BindNewPipeAndPassReceiver());
  }

  void RegisterTestPrefs(PrefRegistrySimple* registry) {
    for (auto prefName : kPrefixAPrefNames) {
      registry->RegisterStringPref(prefName, kPrefAValue);
    }
    for (auto prefName : kPrefixBPrefNames) {
      registry->RegisterStringPref(prefName, kPrefBValue);
    }
    for (auto prefName : kPrefixCPrefNames) {
      registry->RegisterStringPref(prefName, kPrefCValue);
    }
  }

 protected:
  content::BrowserTaskEnvironment browser_task_environment_;
  std::unique_ptr<PrivacySandboxInternalsHandler> handler_;
  mojo::Remote<mojom::PageHandler> remote_;

  std::unique_ptr<TestingProfile> profile_;
};

TEST_F(PrivacySandboxInternalsHandlerUnitTest,
       ReadPrefsWithPrefixes_OnlyReturnsPrefsThatStartPrefixes) {
  base::test::TestFuture<ReadPrefsWithPrefixesResultType> future;
  remote_->ReadPrefsWithPrefixes({kPrefixA, kPrefixB}, future.GetCallback());
  auto& cb_data = future.Get();

  EXPECT_THAT(
      cb_data,
      Each(Pointee(Field(
          &privacy_sandbox_internals::mojom::PrivacySandboxInternalsPref::name,
          AnyOf(StartsWith(kPrefixA), StartsWith(kPrefixB))))));
  EXPECT_THAT(cb_data.size(),
              kPrefixAPrefNames.size() + kPrefixBPrefNames.size());
}

TEST_F(PrivacySandboxInternalsHandlerUnitTest,
       ReadPrefsWithPrefixes_HandlesOverlappingPrefixes) {
  base::test::TestFuture<ReadPrefsWithPrefixesResultType> future;
  remote_->ReadPrefsWithPrefixes({kShortPrefixA, kPrefixA},
                                 future.GetCallback());
  auto& cb_data = future.Get();

  EXPECT_THAT(cb_data.size(), kPrefixAPrefNames.size());
}

TEST_F(PrivacySandboxInternalsHandlerUnitTest,
       ReadPrefsWithPrefixes_EmptyStringInPrefixListReportsBadMessage) {
  std::string received_error;
  mojo::SetDefaultProcessErrorHandler(base::BindLambdaForTesting(
      [&](const std::string& error) { received_error = error; }));

  remote_->ReadPrefsWithPrefixes({kPrefixA, ""}, base::DoNothing());
  remote_.FlushForTesting();

  EXPECT_EQ(received_error, "Empty prefixes are invalid.");
}

TEST_F(PrivacySandboxInternalsHandlerUnitTest,
       ReadPrefsWithPrefixes_DuplicatedPrefixInPrefixListReportsBadMessage) {
  std::string received_error;
  mojo::SetDefaultProcessErrorHandler(base::BindLambdaForTesting(
      [&](const std::string& error) { received_error = error; }));

  remote_->ReadPrefsWithPrefixes({kPrefixA, kPrefixB, kPrefixB},
                                 base::DoNothing());
  remote_.FlushForTesting();

  EXPECT_EQ(received_error, "Duplicate prefixes are invalid.");
}

TEST_F(PrivacySandboxInternalsHandlerUnitTest,
       ReadPrefsWithPrefixes_ResultContainsPrefValues) {
  base::test::TestFuture<ReadPrefsWithPrefixesResultType> future;
  remote_->ReadPrefsWithPrefixes({kPrefixA, kPrefixB}, future.GetCallback());
  auto& cb_data = future.Get();

  EXPECT_THAT(
      cb_data,
      Each(Pointee(AnyOf(
          AllOf(Field(&privacy_sandbox_internals::mojom::
                          PrivacySandboxInternalsPref::name,
                      StartsWith(kPrefixA)),
                Field(&privacy_sandbox_internals::mojom::
                          PrivacySandboxInternalsPref::value,
                      Property(&base::Value::GetString, StrEq(kPrefAValue)))),
          AllOf(
              Field(&privacy_sandbox_internals::mojom::
                        PrivacySandboxInternalsPref::name,
                    StartsWith(kPrefixB)),
              Field(&privacy_sandbox_internals::mojom::
                        PrivacySandboxInternalsPref::value,
                    Property(&base::Value::GetString, StrEq(kPrefBValue))))))));
}

}  // namespace
}  // namespace privacy_sandbox_internals