Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: chromeos/printing/ppd_cache_unittest.cc

Issue 2343983004: Add PPDProvider barebones implementation and associated cache skeleton. (Closed)
Patch Set: Initial PPDProvider/PPDCache implementation. Also, add associated unittests. This doesn't plumb th… Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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
5 #include "base/bind.h"
6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/hash.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "chromeos/printing/ppd_cache.h"
15 #include "net/url_request/test_url_request_interceptor.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using ::base::FilePath;
Lei Zhang 2016/10/17 17:54:07 What's the motivation for these? It's not like wri
Carlson 2016/10/18 19:05:01 Subjective readability?
Lei Zhang 2016/10/18 19:37:00 In general, I would suggest writing code to fit in
Carlson 2016/10/18 20:06:28 See earlier comment about "acceptable" vs "how you
Lei Zhang 2016/10/18 22:25:34 What I'm saying is "this is how 99% of the code is
Carlson 2016/10/18 23:54:31 Sometimes what I really want is a "Disagree, but d
20 using ::base::Optional;
21 using ::base::WaitableEvent;
22 using ::std::string;
23
24 namespace chromeos {
25 namespace printing {
26 namespace {
27
28 const char kTestManufacturer[] = "FooPrinters, Inc.";
29 const char kTestModel[] = "Laser BarMatic 1000";
30 const char kTestUserUrl[] = "/some/path/to/some/ppd/file";
31 const char kTestPpdContents[] = "This is the ppd for the first printer";
32 // Output of 'gzip -9' on a file with contents 'ppd contents'
33 const char kTestGZippedPpdContents[] = {
34 0x1f, 0x8b, 0x08, 0x08, 0xe4, 0x2e, 0x00, 0x58, 0x02, 0x03, 0x70,
35 0x70, 0x64, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x2b, 0x28, 0x48, 0x51,
36 0x48, 0xce, 0xcf, 0x2b, 0x49, 0xcd, 0x2b, 0x29, 0xe6, 0x02, 0x00,
37 0x2b, 0x51, 0x91, 0x24, 0x0d, 0x00, 0x00, 0x00};
38
39 // Generate and return a PPDReference that has the fields set from the kTest
40 // constants above.
41 Printer::PpdReference TestReference() {
42 Printer::PpdReference ret;
43 ret.effective_manufacturer = kTestManufacturer;
44 ret.effective_model = kTestModel;
45 ret.user_supplied_ppd_url = kTestUserUrl;
46 return ret;
47 }
48
49 // This fixture just points the cache at a temporary directory for the life of
50 // the test.
51 class PpdCacheTest : public ::testing::Test {
52 public:
53 PpdCacheTest() { CHECK(ppd_cache_temp_dir_.CreateUniqueTempDir()); }
54
55 // Make and return a cache for the test that uses a temporary directory
56 // which is cleaned up at the end of the test.
57 std::unique_ptr<PpdCache> CreateTestCache() {
58 return PpdCache::Create(ppd_cache_temp_dir_.GetPath());
59 }
60
61 protected:
62 // Overrider for DIR_CHROMEOS_PPD_CACHE that points it at a temporary
63 // directory for the life of the test.
64 base::ScopedTempDir ppd_cache_temp_dir_;
65 };
66
67 // Check that path has a value, and the contents of the referenced fule
68 // are contents.
69 void CheckFileContentsAre(Optional<FilePath> result,
70 const string& expected_contents) {
71 // Make sure we have a value.
72 ASSERT_TRUE(result);
73 // Make sure we get the ppd back that we stored.
74 string contents;
75 ASSERT_TRUE(base::ReadFileToString(result.value(), &contents));
76 EXPECT_EQ(expected_contents, contents);
77 }
78
79 // Test that we miss on an empty cache.
80 TEST_F(PpdCacheTest, SimpleMisses) {
81 auto cache = CreateTestCache();
82 EXPECT_FALSE(cache->Find(Printer::PpdReference()));
83 EXPECT_FALSE(cache->Find(TestReference()));
84 }
85
86 // Test that when we store stuff, we get it back.
87 TEST_F(PpdCacheTest, MissThenHit) {
88 auto cache = CreateTestCache();
89 auto ref = TestReference();
90 EXPECT_FALSE(cache->Find(ref));
91
92 // Store should give us a reference to the file.
93 CheckFileContentsAre(cache->Store(ref, kTestPpdContents), kTestPpdContents);
94
95 // We should also get it back in response to a Find.
96 CheckFileContentsAre(cache->Find(ref), kTestPpdContents);
97 }
98
99 // Test that mutating any field in the reference causes us to
100 // miss in the cache.
101 TEST_F(PpdCacheTest, FieldChangeMeansCacheMiss) {
102 auto cache = CreateTestCache();
103 auto ref = TestReference();
104 CheckFileContentsAre(cache->Store(ref, kTestPpdContents), kTestPpdContents);
105
106 ref.effective_manufacturer = "Something else";
107 EXPECT_FALSE(cache->Find(ref));
108 ref = TestReference();
109
110 ref.effective_model = "Something else";
111 EXPECT_FALSE(cache->Find(ref));
112 ref = TestReference();
113
114 ref.user_supplied_ppd_url = "Something else";
115 EXPECT_FALSE(cache->Find(ref));
116 ref = TestReference();
117
118 // Should still find the initial Store when ref *is* identical.
119 CheckFileContentsAre(cache->Find(ref), kTestPpdContents);
120 }
121
122 // Test that gzipped contents get stored as .ppd.gz, and non-gzipped
123 // contents get stored as .ppd
124 TEST_F(PpdCacheTest, StoredExtensions) {
125 auto cache = CreateTestCache();
126 auto ref = TestReference();
127
128 // Not compressed, so should store as .ppd
129 auto result = cache->Store(ref, kTestPpdContents);
130 EXPECT_EQ(cache->Store(ref, kTestPpdContents).value().Extension(), ".ppd");
131
132 // Compressed, so should identify this and store as .ppd.gz
133 string gzipped_contents(kTestGZippedPpdContents,
134 sizeof(kTestGZippedPpdContents));
135 EXPECT_EQ(cache->Store(ref, gzipped_contents).value().Extension(), ".ppd.gz");
136 }
137
138 } // anonymous namespace
Lei Zhang 2016/10/17 17:54:07 s/annoymous // (Run git cl lint)
Carlson 2016/10/18 19:05:01 I did run git cl lint and it doesn't pick this up.
139 } // namespace printing
140 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698