File: print_job_database_impl_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (255 lines) | stat: -rw-r--r-- 8,600 bytes parent folder | download | duplicates (7)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2019 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/ash/printing/history/print_job_database_impl.h"

#include "base/containers/contains.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "chrome/browser/ash/printing/history/print_job_info.pb.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace {

using printing::proto::PrintJobInfo;

constexpr char kId1[] = "id1";
constexpr char kId2[] = "id2";
constexpr char kTitle1[] = "title1";
constexpr char kTitle2[] = "title2";

PrintJobInfo ConstructPrintJobInfo(const std::string& id,
                                   const std::string& title) {
  PrintJobInfo print_job_info;
  print_job_info.set_id(id);
  print_job_info.set_title(title);
  return print_job_info;
}

}  // namespace

class PrintJobDatabaseImplTest : public ::testing::Test {
 public:
  PrintJobDatabaseImplTest() = default;

  void SetUp() override {
    EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());

    database_provider_ = std::make_unique<leveldb_proto::ProtoDatabaseProvider>(
        temp_dir_.GetPath());

    print_job_database_ = std::make_unique<PrintJobDatabaseImpl>(
        database_provider_.get(), temp_dir_.GetPath());
  }

  void TearDown() override {
    database_provider_.reset();
    print_job_database_.reset();
  }

  void OnInitializedWithClosure(base::RepeatingClosure run_loop_closure,
                                bool success) {
    EXPECT_TRUE(success);
    run_loop_closure.Run();
  }

  void OnPrintJobSaved(base::RepeatingClosure run_loop_closure, bool success) {
    EXPECT_TRUE(success);
    run_loop_closure.Run();
  }

  void OnPrintJobsRetrieved(base::RepeatingClosure run_loop_closure,
                            bool success,
                            std::vector<PrintJobInfo> entries) {
    EXPECT_TRUE(success);
    entries_ = std::move(entries);
    run_loop_closure.Run();
  }

  void OnPrintJobsRetrievedFromDatabase(
      base::RepeatingClosure run_loop_closure,
      bool success,
      std::unique_ptr<std::vector<PrintJobInfo>> entries) {
    EXPECT_TRUE(success);
    entries_ = *entries;
    run_loop_closure.Run();
  }

 protected:
  void Initialize() {
    print_job_database_->Initialize(base::BindOnce(
        &PrintJobDatabaseImplTest::OnInitialized, base::Unretained(this)));
  }

  void OnInitialized(bool success) { EXPECT_TRUE(success); }

  void SavePrintJob(const PrintJobInfo& print_job_info) {
    base::RunLoop run_loop;
    print_job_database_->SavePrintJob(
        print_job_info,
        base::BindOnce(&PrintJobDatabaseImplTest::OnPrintJobSaved,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }

  void DeletePrintJobs(const std::vector<std::string>& ids) {
    base::RunLoop run_loop;
    print_job_database_->DeletePrintJobs(
        ids, base::BindOnce(&PrintJobDatabaseImplTest::OnPrintJobsDeleted,
                            base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }

  void Clear() {
    base::RunLoop run_loop;
    print_job_database_->Clear(
        base::BindOnce(&PrintJobDatabaseImplTest::OnPrintJobsDeleted,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
  }

  void OnPrintJobsDeleted(base::RepeatingClosure run_loop_closure,
                          bool success) {
    EXPECT_TRUE(success);
    run_loop_closure.Run();
  }

  std::vector<PrintJobInfo> GetPrintJobs() {
    base::RunLoop run_loop;
    print_job_database_->GetPrintJobs(
        base::BindOnce(&PrintJobDatabaseImplTest::OnPrintJobsRetrieved,
                       base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
    return entries_;
  }

  std::vector<PrintJobInfo> GetPrintJobsFromProtoDatabase() {
    base::RunLoop run_loop;
    print_job_database_->GetPrintJobsFromProtoDatabase(base::BindOnce(
        &PrintJobDatabaseImplTest::OnPrintJobsRetrievedFromDatabase,
        base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
    return entries_;
  }

  const std::vector<PrintJobInfo>& entries() { return entries_; }

  base::test::TaskEnvironment task_environment_;
  std::unique_ptr<PrintJobDatabaseImpl> print_job_database_;
  base::HistogramTester histogram_tester_;

 private:
  base::ScopedTempDir temp_dir_;
  std::unique_ptr<leveldb_proto::ProtoDatabaseProvider> database_provider_;
  std::vector<PrintJobInfo> entries_;
};

TEST_F(PrintJobDatabaseImplTest, Initialize) {
  base::RunLoop run_loop;
  print_job_database_->Initialize(
      base::BindOnce(&PrintJobDatabaseImplTest::OnInitializedWithClosure,
                     base::Unretained(this), run_loop.QuitClosure()));
  run_loop.Run();
  EXPECT_TRUE(print_job_database_->IsInitialized());
}

TEST_F(PrintJobDatabaseImplTest, SavePrintJob) {
  Initialize();
  PrintJobInfo print_job_info = ConstructPrintJobInfo(kId1, kTitle1);
  SavePrintJob(print_job_info);
  std::vector<PrintJobInfo> entries = GetPrintJobs();
  ASSERT_EQ(1u, entries.size());
  EXPECT_EQ(kId1, entries[0].id());
  EXPECT_EQ(kTitle1, entries[0].title());
}

TEST_F(PrintJobDatabaseImplTest, DeletePrintJobs) {
  Initialize();
  PrintJobInfo print_job_info1 = ConstructPrintJobInfo(kId1, kTitle1);
  SavePrintJob(print_job_info1);
  PrintJobInfo print_job_info2 = ConstructPrintJobInfo(kId2, kTitle2);
  SavePrintJob(print_job_info2);
  DeletePrintJobs({kId1});
  std::vector<PrintJobInfo> entries = GetPrintJobs();
  ASSERT_EQ(1u, entries.size());
  EXPECT_EQ(kId2, entries[0].id());
  EXPECT_EQ(kTitle2, entries[0].title());
}

TEST_F(PrintJobDatabaseImplTest, Clear) {
  Initialize();
  PrintJobInfo print_job_info1 = ConstructPrintJobInfo(kId1, kTitle1);
  SavePrintJob(print_job_info1);
  PrintJobInfo print_job_info2 = ConstructPrintJobInfo(kId2, kTitle2);
  SavePrintJob(print_job_info2);
  std::vector<PrintJobInfo> entries = GetPrintJobs();
  EXPECT_EQ(2u, entries.size());
  Clear();
  entries = GetPrintJobs();
  EXPECT_EQ(0u, entries.size());
}

TEST_F(PrintJobDatabaseImplTest, GetPrintJobsFromDatabase) {
  Initialize();
  PrintJobInfo print_job_info = ConstructPrintJobInfo(kId1, kTitle1);
  SavePrintJob(print_job_info);
  std::vector<PrintJobInfo> entries = GetPrintJobsFromProtoDatabase();
  ASSERT_EQ(1u, entries.size());
  EXPECT_EQ(kId1, entries[0].id());
  EXPECT_EQ(kTitle1, entries[0].title());
}

TEST_F(PrintJobDatabaseImplTest, TwoSimultaneousSavePrintJobRequests) {
  Initialize();
  PrintJobInfo print_job_info = ConstructPrintJobInfo(kId1, kTitle1);
  PrintJobInfo print_job_info2 = ConstructPrintJobInfo(kId2, kTitle2);

  base::RunLoop run_loop;
  base::RunLoop run_loop2;
  print_job_database_->SavePrintJob(
      print_job_info,
      base::BindOnce(&PrintJobDatabaseImplTest::OnPrintJobSaved,
                     base::Unretained(this), run_loop.QuitClosure()));
  print_job_database_->SavePrintJob(
      print_job_info2,
      base::BindOnce(&PrintJobDatabaseImplTest::OnPrintJobSaved,
                     base::Unretained(this), run_loop2.QuitClosure()));
  run_loop.Run();
  run_loop2.Run();

  std::vector<PrintJobInfo> entries = GetPrintJobsFromProtoDatabase();
  ASSERT_EQ(2u, entries.size());
  std::vector<std::string> ids = {entries[0].id(), entries[1].id()};
  EXPECT_TRUE(base::Contains(ids, kId1));
  EXPECT_TRUE(base::Contains(ids, kId2));
}

TEST_F(PrintJobDatabaseImplTest, RequestsBeforeInitialization) {
  PrintJobInfo print_job_info = ConstructPrintJobInfo(kId1, kTitle1);
  base::RunLoop save_print_job_run_loop;
  print_job_database_->SavePrintJob(
      print_job_info, base::BindOnce(&PrintJobDatabaseImplTest::OnPrintJobSaved,
                                     base::Unretained(this),
                                     save_print_job_run_loop.QuitClosure()));
  base::RunLoop get_print_jobs_run_loop;
  print_job_database_->GetPrintJobs(base::BindOnce(
      &PrintJobDatabaseImplTest::OnPrintJobsRetrieved, base::Unretained(this),
      get_print_jobs_run_loop.QuitClosure()));
  Initialize();
  save_print_job_run_loop.Run();
  get_print_jobs_run_loop.Run();

  std::vector<PrintJobInfo> print_job_entries = entries();
  ASSERT_EQ(1u, print_job_entries.size());
  EXPECT_EQ(kId1, print_job_entries[0].id());
  EXPECT_EQ(kTitle1, print_job_entries[0].title());
}

}  // namespace ash