forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_party_sets_util_unittest.cc
82 lines (67 loc) · 2.76 KB
/
first_party_sets_util_unittest.cc
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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/first_party_sets/first_party_sets_util.h"
#include <string>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_reader.h"
#include "base/run_loop.h"
#include "base/sequence_checker.h"
#include "base/test/bind.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
class FirstPartySetsUtilTest : public ::testing::Test {
public:
FirstPartySetsUtilTest() {
CHECK(scoped_dir_.CreateUniqueTempDir());
CHECK(PathExists(scoped_dir_.GetPath()));
persisted_sets_path_ = scoped_dir_.GetPath().Append(
FILE_PATH_LITERAL("persisted_first_party_sets.json"));
}
protected:
base::ScopedTempDir scoped_dir_;
base::FilePath persisted_sets_path_;
// Need to satisfy DCHECK_CURRENTLY_ON(BrowserThread::UI).
content::BrowserTaskEnvironment env_;
};
TEST_F(FirstPartySetsUtilTest, SendAndUpdatePersistedSets_FileNotExist) {
SEQUENCE_CHECKER(sequence_checker);
const std::string expected_updated_sets = "updated first party sets";
FirstPartySetsUtil::GetInstance()->SendAndUpdatePersistedSets(
scoped_dir_.GetPath(),
/*send_sets=*/
base::BindLambdaForTesting(
[&](base::OnceCallback<void(const std::string&)> callback,
const std::string& got) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker);
EXPECT_EQ(got, "");
std::move(callback).Run(expected_updated_sets);
}));
env_.RunUntilIdle();
std::string got;
ASSERT_TRUE(base::ReadFileToString(persisted_sets_path_, &got));
EXPECT_EQ(got, expected_updated_sets);
}
TEST_F(FirstPartySetsUtilTest, SendAndUpdatePersistedSets) {
SEQUENCE_CHECKER(sequence_checker);
const std::string expected_read_sets = "persisted first party sets";
const std::string expected_updated_sets = "updated first party sets";
ASSERT_TRUE(base::WriteFile(persisted_sets_path_, expected_read_sets));
FirstPartySetsUtil::GetInstance()->SendAndUpdatePersistedSets(
scoped_dir_.GetPath(),
/*send_sets=*/
base::BindLambdaForTesting(
[&](base::OnceCallback<void(const std::string&)> callback,
const std::string& got) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker);
EXPECT_EQ(got, expected_read_sets);
std::move(callback).Run(expected_updated_sets);
}));
env_.RunUntilIdle();
std::string got;
ASSERT_TRUE(base::ReadFileToString(persisted_sets_path_, &got));
EXPECT_EQ(got, expected_updated_sets);
}