OLD | NEW |
---|---|
(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 namespace chromeos { | |
20 namespace printing { | |
21 namespace { | |
22 | |
23 const char kTestManufacturer[] = "FooPrinters, Inc."; | |
24 const char kTestModel[] = "Laser BarMatic 1000"; | |
25 const char kTestUserUrl[] = "/some/path/to/some/ppd/file"; | |
26 const char kTestPpdContents[] = "This is the ppd for the first printer"; | |
27 // Output of 'gzip -9' on a file with contents 'ppd contents' | |
28 const char kTestGZippedPpdContents[] = { | |
29 0x1f, 0x8b, 0x08, 0x08, 0xe4, 0x2e, 0x00, 0x58, 0x02, 0x03, 0x70, | |
30 0x70, 0x64, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x2b, 0x28, 0x48, 0x51, | |
31 0x48, 0xce, 0xcf, 0x2b, 0x49, 0xcd, 0x2b, 0x29, 0xe6, 0x02, 0x00, | |
32 0x2b, 0x51, 0x91, 0x24, 0x0d, 0x00, 0x00, 0x00}; | |
33 | |
34 // Generate and return a PPDReference that has the fields set from the kTest | |
35 // constants above. | |
36 Printer::PpdReference TestReference() { | |
37 Printer::PpdReference ret; | |
38 ret.effective_manufacturer = kTestManufacturer; | |
39 ret.effective_model = kTestModel; | |
40 ret.user_supplied_ppd_url = kTestUserUrl; | |
41 return ret; | |
42 } | |
43 | |
44 // This fixture just points the cache at a temporary directory for the life of | |
45 // the test. | |
46 class PpdCacheTest : public ::testing::Test { | |
47 public: | |
48 PpdCacheTest() { CHECK(ppd_cache_temp_dir_.CreateUniqueTempDir()); } | |
49 | |
50 // Make and return a cache for the test that uses a temporary directory | |
51 // which is cleaned up at the end of the test. | |
52 std::unique_ptr<PpdCache> CreateTestCache() { | |
53 return PpdCache::Create(ppd_cache_temp_dir_.GetPath()); | |
54 } | |
55 | |
56 protected: | |
57 // Overrider for DIR_CHROMEOS_PPD_CACHE that points it at a temporary | |
58 // directory for the life of the test. | |
59 base::ScopedTempDir ppd_cache_temp_dir_; | |
60 }; | |
61 | |
62 // Check that path has a value, and the contents of the referenced fule | |
63 // are contents. | |
64 void CheckFileContentsAre(base::Optional<base::FilePath> result, | |
65 const std::string& expected_contents) { | |
66 // Make sure we have a value. | |
67 ASSERT_TRUE(result); | |
68 // Make sure we get the ppd back that we stored. | |
69 std::string contents; | |
70 ASSERT_TRUE(base::ReadFileToString(result.value(), &contents)); | |
71 EXPECT_EQ(expected_contents, contents); | |
72 } | |
73 | |
74 // Test that we miss on an empty cache. | |
75 TEST_F(PpdCacheTest, SimpleMiss) { | |
76 auto cache = CreateTestCache(); | |
77 EXPECT_FALSE(cache->Find(TestReference())); | |
78 } | |
79 | |
80 // Test that when we store stuff, we get it back. | |
81 TEST_F(PpdCacheTest, MissThenHit) { | |
82 auto cache = CreateTestCache(); | |
83 auto ref = TestReference(); | |
84 EXPECT_FALSE(cache->Find(ref)); | |
85 | |
86 // Store should give us a reference to the file. | |
87 CheckFileContentsAre(cache->Store(ref, kTestPpdContents), kTestPpdContents); | |
88 | |
89 // We should also get it back in response to a Find. | |
90 CheckFileContentsAre(cache->Find(ref), kTestPpdContents); | |
91 } | |
92 | |
93 // Test that mutating any field in the reference causes us to miss in the cache | |
94 // when we change the highest priority resolve criteria. | |
95 TEST_F(PpdCacheTest, FieldChangeMeansCacheMiss) { | |
96 auto cache = CreateTestCache(); | |
97 auto ref = TestReference(); | |
98 CheckFileContentsAre(cache->Store(ref, kTestPpdContents), kTestPpdContents); | |
99 | |
100 // We have a user url, so should still cache hit on manufacturer change. | |
101 ref.effective_manufacturer = "Something else"; | |
102 EXPECT_TRUE(cache->Find(ref)); | |
103 ref = TestReference(); | |
104 | |
105 // We have a user url, so should still cache hit on model change. | |
106 ref.effective_model = "Something else"; | |
107 EXPECT_TRUE(cache->Find(ref)); | |
108 ref = TestReference(); | |
109 | |
110 // But if we change th user url, that's a cache miss. | |
111 ref.user_supplied_ppd_url = "Something else"; | |
112 EXPECT_FALSE(cache->Find(ref)); | |
113 ref = TestReference(); | |
114 // Should still find the initial Store when ref *is* identical. | |
115 CheckFileContentsAre(cache->Find(ref), kTestPpdContents); | |
116 | |
117 // Now store the reference with no test url. | |
118 ref.user_supplied_ppd_url.clear(); | |
119 CheckFileContentsAre(cache->Store(ref, kTestPpdContents), kTestPpdContents); | |
120 | |
121 // Now changing the model or manufacturer should cause a cache miss. | |
122 ref.effective_manufacturer = "Something else"; | |
123 EXPECT_FALSE(cache->Find(ref)); | |
124 ref = TestReference(); | |
125 ref.user_supplied_ppd_url.clear(); | |
126 ref.effective_model = "Something else"; | |
127 EXPECT_FALSE(cache->Find(ref)); | |
128 } | |
129 | |
130 // Test that gzipped contents get stored as .ppd.gz, and non-gzipped | |
131 // contents get stored as .ppd | |
132 TEST_F(PpdCacheTest, StoredExtensions) { | |
133 auto cache = CreateTestCache(); | |
134 auto ref = TestReference(); | |
135 | |
136 // Not compressed, so should store as .ppd | |
137 auto result = cache->Store(ref, kTestPpdContents); | |
138 EXPECT_EQ(cache->Store(ref, kTestPpdContents).value().Extension(), ".ppd"); | |
Lei Zhang
2016/10/19 01:25:10
Arguments should be flipped. See line 71. As is, g
Carlson
2016/10/19 16:38:26
Done.
| |
139 | |
140 // Compressed, so should identify this and store as .ppd.gz | |
141 std::string gzipped_contents(kTestGZippedPpdContents, | |
142 sizeof(kTestGZippedPpdContents)); | |
143 EXPECT_EQ(cache->Store(ref, gzipped_contents).value().Extension(), ".ppd.gz"); | |
144 } | |
145 | |
146 } // namespace | |
147 } // namespace printing | |
148 } // namespace chromeos | |
OLD | NEW |