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

Side by Side Diff: chromeos/printing/ppd_provider_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/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/test/scoped_path_override.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "chromeos/chromeos_paths.h"
15 #include "chromeos/printing/ppd_cache.h"
16 #include "chromeos/printing/ppd_provider.h"
17 #include "net/url_request/test_url_request_interceptor.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 using base::FilePath;
22 using std::string;
23 using std::unique_ptr;
24
25 namespace chromeos {
26 namespace printing {
27 namespace {
28 #if 0
skau 2016/10/14 22:10:17 ?
Carlson 2016/10/14 23:05:56 Umm..umm...PAY NO ATTENTION TO THE COMMENTED OUT T
29 const char kTestQuirksServer[] = "bogusserver.bogus.com";
30 const char kTestAPIKey[] = "BOGUSAPIKEY";
31 const char kTestManufacturer[] = "Bogus Printer Corp";
32 const char kTestModel[] = "MegaPrint 9000";
33 const char kQuirksResponse[] =
34 "{\n"
35 " \"compressedPpd\": \"This is the quirks ppd\",\n"
36 " \"lastUpdatedTime\": \"1\"\n"
37 "}\n";
38 const char kQuirksPPD[] = "This is the quirks ppd";
39
40 class PPDProviderTest : public ::testing::Test {
41 public:
42 PPDProviderTest()
43 : ppd_cache_dir_override_(DIR_PRINTER_DRIVERS_CACHE),
44 loop_(base::MessageLoop::TYPE_IO),
45 request_context_getter_(
46 new ::net::TestURLRequestContextGetter(loop_.task_runner().get())) {
47 }
48
49 // Create the ppd_provider_ to be used for this test.
50 void SetUp() override {
51 auto provider_options = PPDProvider::Options();
52 provider_options.quirks_server = kTestQuirksServer;
53 ppd_provider_ = PPDProvider::Create(kTestAPIKey, request_context_getter_,
54 PPDCache::Create(), provider_options);
55 }
56
57 protected:
58 // Overrider for DIR_PRINTER_DRIVERS_CACHE that points it at a temporary
59 // directory for the life of the test.
60 base::ScopedPathOverride ppd_cache_dir_override_;
61
62 // Provider to be used in the test.
63 unique_ptr<PPDProvider> ppd_provider_;
64
65 // Misc extra stuff needed for the test environment to function.
66 base::MessageLoop loop_;
67 scoped_refptr<::net::URLRequestContextGetter> request_context_getter_;
68 };
69
70 // Struct that just captures the callback result for a PPDProvider lookup and
71 // saves it for inspection by the test.
72 struct CapturedResolveResult {
73 bool initialized = false;
74 PPDProvider::ResolveResult result;
75 FilePath file;
76 };
77
78 // Callback for saving a resolve callback.
79 void CaptureResolveResult(CapturedResolveResult* capture,
80 PPDProvider::ResolveResult result,
81 FilePath file) {
82 capture->initialized = true;
83 capture->result = result;
84 capture->file = file;
85 }
86
87 // For a resolve result that should end up successful, check that it is
88 // successful and the contents are expected_contents.
89 void CheckResolveSuccessful(const CapturedResolveResult& captured,
90 const string& expected_contents) {
91 ASSERT_TRUE(captured.initialized);
92 ASSERT_EQ(captured.result, PPDProvider::SUCCESS);
93
94 string contents;
95 ASSERT_TRUE(base::ReadFileToString(captured.file, &contents));
96 EXPECT_EQ(expected_contents, contents);
97 }
98
99 // Resolve a ppd via the quirks server.
100 TEST_F(PPDProviderTest, QuirksServerResolve) {
101 base::ScopedTempDir temp_dir;
102 CHECK(temp_dir.CreateUniqueTempDir());
103
104 {
105 ::net::TestURLRequestInterceptor interceptor(
106 "https", kTestQuirksServer, base::ThreadTaskRunnerHandle::Get(),
107 base::ThreadTaskRunnerHandle::Get());
108
109 GURL expected_url(string("https://") + kTestQuirksServer //
110 + "/v2/printer/manufacturers/" //
111 + kTestManufacturer //
112 + "/models/" //
113 + kTestModel //
114 + "?key=" //
115 + kTestAPIKey);
116
117 FilePath contents_path = temp_dir.path().Append("response");
118 string contents = kQuirksResponse;
119 CHECK_GT(base::WriteFile(contents_path, kQuirksResponse,
120 strlen(kQuirksResponse)),
121 0);
122
123 interceptor.SetResponse(expected_url, contents_path);
124
125 CapturedResolveResult captured;
126 ppd_provider_->Resolve(kTestManufacturer, kTestModel,
127 base::Bind(CaptureResolveResult, &captured));
128 base::RunLoop().RunUntilIdle();
129 CheckResolveSuccessful(captured, kQuirksPPD);
130 }
131
132 // Now that the interceptor is out of scope, re-run the query. We should
133 // hit in the cache, and thus *not* re-run the query.
134 CapturedResolveResult captured;
135 ppd_provider_->Resolve(kTestManufacturer, kTestModel,
136 base::Bind(CaptureResolveResult, &captured));
137 base::RunLoop().RunUntilIdle();
138 CheckResolveSuccessful(captured, kQuirksPPD);
139 }
140
141 // Test storage and retrieval of ppds that are added manually.
142 TEST_F(PPDProviderTest, LocalResolve) {
143 const string kTestPrinterId("My printer");
144 {
145 CapturedResolveResult captured;
146 ppd_provider_->ResolveLocal(kTestPrinterId,
147 base::Bind(CaptureResolveResult, &captured));
148 base::RunLoop().RunUntilIdle();
149 EXPECT_TRUE(captured.initialized);
150 EXPECT_EQ(captured.result, PPDProvider::NOT_FOUND);
151 }
152
153 // Store a local ppd.
154 const string kLocalPPDContents("My local ppd contents");
155 {
156 base::ScopedTempDir temp_dir;
157 CHECK(temp_dir.CreateUniqueTempDir());
158
159 FilePath local_ppd_path = temp_dir.path().Append("local_ppd");
160 ASSERT_EQ(base::WriteFile(local_ppd_path, kLocalPPDContents.data(),
161 kLocalPPDContents.size()),
162 static_cast<int>(kLocalPPDContents.size()));
163 ASSERT_TRUE(ppd_provider_->StoreLocal(kTestPrinterId, local_ppd_path));
164 }
165 // temp_dir should now be deleted, which helps make sure we actually latched a
166 // copy, not a reference.
167
168 // Retry the resove, should get the ppd back now.
169 {
170 CapturedResolveResult captured;
171
172 ppd_provider_->ResolveLocal(kTestPrinterId,
173 base::Bind(CaptureResolveResult, &captured));
174 base::RunLoop().RunUntilIdle();
175 CheckResolveSuccessful(captured, kLocalPPDContents);
176 }
177 }
178 #endif
179 } // namespace
180 } // namespace printing
181 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698