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
ash / webui / print_preview_cros / backend / print_preview_pdf_store_unittest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/webui/print_preview_cros/backend/print_preview_pdf_store.h"
#include <memory>
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash::printing::print_preview {
namespace {
scoped_refptr<base::RefCountedMemory> CreateTestData(
const std::string& data = "") {
// Mimics PDF data format + minimum length.
return base::MakeRefCounted<base::RefCountedString>(
"%PDF-7j4iGwz3X9bLoQkEc6u1f5t8y2hVn0pArDmJsKiLg9f4s7r3x2c1v" + data);
}
} // namespace
class PrintPreviewPdfStoreTest : public testing::Test {
public:
PrintPreviewPdfStoreTest() = default;
~PrintPreviewPdfStoreTest() override = default;
};
// Verify requesting data for a non-existent unguessable token returns NULL
// data.
TEST_F(PrintPreviewPdfStoreTest, NonExistentToken) {
scoped_refptr<base::RefCountedMemory> data =
PrintPreviewPdfStore::GetInstance()->GetPdfData(
base::UnguessableToken::Create(), /*page_index=*/0);
EXPECT_EQ(nullptr, data);
}
// Verify requesting data for a non-existent page index returns NULL data.
TEST_F(PrintPreviewPdfStoreTest, NonExistentPageIndex) {
const auto token = base::UnguessableToken::Create();
const int page_index = 0;
// Insert data for specified token and page index.
PrintPreviewPdfStore::GetInstance()->InsertPdfData(token, page_index,
CreateTestData());
// Try to fetch with a valid token but and invalid token and expect null.
const int invalid_page_index = 100;
scoped_refptr<base::RefCountedMemory> data =
PrintPreviewPdfStore::GetInstance()->GetPdfData(token,
invalid_page_index);
EXPECT_EQ(nullptr, data);
}
// Verify data can be stored then received.
TEST_F(PrintPreviewPdfStoreTest, GetExpectedData) {
const auto token = base::UnguessableToken::Create();
const int page_index = 0;
const scoped_refptr<base::RefCountedMemory> expected_data = CreateTestData();
PrintPreviewPdfStore::GetInstance()->InsertPdfData(token, page_index,
expected_data);
scoped_refptr<base::RefCountedMemory> data =
PrintPreviewPdfStore::GetInstance()->GetPdfData(token, page_index);
EXPECT_EQ(expected_data, data);
}
// Verify data can be saved under different page indices for the same
// unguessable token.
TEST_F(PrintPreviewPdfStoreTest, SameTokenDifferentPageIndex) {
const auto token = base::UnguessableToken::Create();
const int first_page_index = 0;
const scoped_refptr<base::RefCountedMemory> first_expected_data =
CreateTestData("first");
const int second_page_index = 1;
const scoped_refptr<base::RefCountedMemory> second_expected_data =
CreateTestData("second");
PrintPreviewPdfStore::GetInstance()->InsertPdfData(token, first_page_index,
first_expected_data);
PrintPreviewPdfStore::GetInstance()->InsertPdfData(token, second_page_index,
second_expected_data);
scoped_refptr<base::RefCountedMemory> first_data =
PrintPreviewPdfStore::GetInstance()->GetPdfData(token, first_page_index);
EXPECT_EQ(first_expected_data, first_data);
scoped_refptr<base::RefCountedMemory> second_data =
PrintPreviewPdfStore::GetInstance()->GetPdfData(token, second_page_index);
EXPECT_EQ(second_expected_data, second_data);
}
// Verify data can be saved under different unguessable tokens.
TEST_F(PrintPreviewPdfStoreTest, DifferentTokenSamePageIndex) {
const auto first_token = base::UnguessableToken::Create();
const auto second_token = base::UnguessableToken::Create();
const int page_index = 0;
const scoped_refptr<base::RefCountedMemory> first_expected_data =
CreateTestData("first");
const scoped_refptr<base::RefCountedMemory> second_expected_data =
CreateTestData("second");
PrintPreviewPdfStore::GetInstance()->InsertPdfData(first_token, page_index,
first_expected_data);
PrintPreviewPdfStore::GetInstance()->InsertPdfData(second_token, page_index,
second_expected_data);
scoped_refptr<base::RefCountedMemory> first_data =
PrintPreviewPdfStore::GetInstance()->GetPdfData(first_token, page_index);
EXPECT_EQ(first_expected_data, first_data);
scoped_refptr<base::RefCountedMemory> second_data =
PrintPreviewPdfStore::GetInstance()->GetPdfData(second_token, page_index);
EXPECT_EQ(second_expected_data, second_data);
}
} // namespace ash::printing::print_preview