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/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/strings/stringprintf.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 |
| 29 const char kTestQuirksServer[] = "bogusserver.bogus.com"; |
| 30 const char kTestAPIKey[] = "BOGUSAPIKEY"; |
| 31 const char kLocalPpdUrl[] = "/some/path"; |
| 32 const char kTestManufacturer[] = "Bogus Printer Corp"; |
| 33 const char kTestModel[] = "MegaPrint 9000"; |
| 34 const char kQuirksResponse[] = |
| 35 "{\n" |
| 36 " \"compressedPpd\": \"This is the quirks ppd\",\n" |
| 37 " \"lastUpdatedTime\": \"1\"\n" |
| 38 "}\n"; |
| 39 const char kQuirksPpd[] = "This is the quirks ppd"; |
| 40 |
| 41 class PpdProviderTest : public ::testing::Test { |
| 42 public: |
| 43 PpdProviderTest() |
| 44 : loop_(base::MessageLoop::TYPE_IO), |
| 45 request_context_getter_( |
| 46 new net::TestURLRequestContextGetter(loop_.task_runner().get())) {} |
| 47 |
| 48 void SetUp() override { |
| 49 ASSERT_TRUE(ppd_cache_temp_dir_.CreateUniqueTempDir()); |
| 50 auto provider_options = PpdProvider::Options(); |
| 51 provider_options.quirks_server = kTestQuirksServer; |
| 52 ppd_provider_ = PpdProvider::Create( |
| 53 kTestAPIKey, request_context_getter_, |
| 54 PpdCache::Create(ppd_cache_temp_dir_.GetPath()), provider_options); |
| 55 } |
| 56 |
| 57 protected: |
| 58 base::ScopedTempDir ppd_cache_temp_dir_; |
| 59 |
| 60 // Provider to be used in the test. |
| 61 unique_ptr<PpdProvider> ppd_provider_; |
| 62 |
| 63 // Misc extra stuff needed for the test environment to function. |
| 64 base::MessageLoop loop_; |
| 65 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 66 }; |
| 67 |
| 68 // Struct that just captures the callback result for a PpdProvider lookup and |
| 69 // saves it for inspection by the test. |
| 70 struct CapturedResolveResult { |
| 71 bool initialized = false; |
| 72 PpdProvider::ResolveResult result; |
| 73 FilePath file; |
| 74 }; |
| 75 |
| 76 // Callback for saving a resolve callback. |
| 77 void CaptureResolveResultCallback(CapturedResolveResult* capture, |
| 78 PpdProvider::ResolveResult result, |
| 79 FilePath file) { |
| 80 capture->initialized = true; |
| 81 capture->result = result; |
| 82 capture->file = file; |
| 83 } |
| 84 |
| 85 // For a resolve result that should end up successful, check that it is |
| 86 // successful and the contents are expected_contents. |
| 87 void CheckResolveSuccessful(const CapturedResolveResult& captured, |
| 88 const string& expected_contents) { |
| 89 ASSERT_TRUE(captured.initialized); |
| 90 EXPECT_EQ(PpdProvider::SUCCESS, captured.result); |
| 91 |
| 92 string contents; |
| 93 ASSERT_TRUE(base::ReadFileToString(captured.file, &contents)); |
| 94 EXPECT_EQ(expected_contents, contents); |
| 95 } |
| 96 |
| 97 // Resolve a PPD via the quirks server. |
| 98 TEST_F(PpdProviderTest, QuirksServerResolve) { |
| 99 base::ScopedTempDir temp_dir; |
| 100 CHECK(temp_dir.CreateUniqueTempDir()); |
| 101 |
| 102 Printer::PpdReference ppd_reference; |
| 103 ppd_reference.effective_manufacturer = kTestManufacturer; |
| 104 ppd_reference.effective_model = kTestModel; |
| 105 |
| 106 { |
| 107 net::TestURLRequestInterceptor interceptor( |
| 108 "https", kTestQuirksServer, base::ThreadTaskRunnerHandle::Get(), |
| 109 base::ThreadTaskRunnerHandle::Get()); |
| 110 |
| 111 GURL expected_url(base::StringPrintf( |
| 112 "https://%s/v2/printer/manufacturers/%s/models/%s?key=%s", |
| 113 kTestQuirksServer, kTestManufacturer, kTestModel, kTestAPIKey)); |
| 114 |
| 115 FilePath contents_path = temp_dir.GetPath().Append("response"); |
| 116 string contents = kQuirksResponse; |
| 117 int bytes_written = base::WriteFile(contents_path, kQuirksResponse, |
| 118 strlen(kQuirksResponse)); |
| 119 ASSERT_EQ(bytes_written, static_cast<int>(strlen(kQuirksResponse))); |
| 120 |
| 121 interceptor.SetResponse(expected_url, contents_path); |
| 122 |
| 123 CapturedResolveResult captured; |
| 124 ppd_provider_->Resolve(ppd_reference, |
| 125 base::Bind(CaptureResolveResultCallback, &captured)); |
| 126 base::RunLoop().RunUntilIdle(); |
| 127 CheckResolveSuccessful(captured, kQuirksPpd); |
| 128 } |
| 129 |
| 130 // Now that the interceptor is out of scope, re-run the query. We should |
| 131 // hit in the cache, and thus *not* re-run the query. |
| 132 CapturedResolveResult captured; |
| 133 ppd_provider_->Resolve(ppd_reference, |
| 134 base::Bind(CaptureResolveResultCallback, &captured)); |
| 135 base::RunLoop().RunUntilIdle(); |
| 136 CheckResolveSuccessful(captured, kQuirksPpd); |
| 137 } |
| 138 |
| 139 // Test storage and retrieval of PPDs that are added manually. Even though we |
| 140 // supply a manufacturer and model, we should *not* hit the network for this |
| 141 // resolution since we should find the stored version already cached. |
| 142 TEST_F(PpdProviderTest, LocalResolve) { |
| 143 Printer::PpdReference ppd_reference; |
| 144 ppd_reference.user_supplied_ppd_url = kLocalPpdUrl; |
| 145 ppd_reference.effective_manufacturer = kTestManufacturer; |
| 146 ppd_reference.effective_model = kTestModel; |
| 147 |
| 148 // Initially, should not resolve. |
| 149 { |
| 150 CapturedResolveResult captured; |
| 151 ppd_provider_->Resolve(ppd_reference, |
| 152 base::Bind(CaptureResolveResultCallback, &captured)); |
| 153 base::RunLoop().RunUntilIdle(); |
| 154 EXPECT_TRUE(captured.initialized); |
| 155 EXPECT_EQ(PpdProvider::NOT_FOUND, captured.result); |
| 156 } |
| 157 |
| 158 // Store a local ppd. |
| 159 const string kLocalPpdContents("My local ppd contents"); |
| 160 { |
| 161 base::ScopedTempDir temp_dir; |
| 162 CHECK(temp_dir.CreateUniqueTempDir()); |
| 163 |
| 164 FilePath local_ppd_path = temp_dir.GetPath().Append("local_ppd"); |
| 165 ASSERT_EQ(base::WriteFile(local_ppd_path, kLocalPpdContents.data(), |
| 166 kLocalPpdContents.size()), |
| 167 static_cast<int>(kLocalPpdContents.size())); |
| 168 ASSERT_TRUE(ppd_provider_->CachePpd(ppd_reference, local_ppd_path)); |
| 169 } |
| 170 // temp_dir should now be deleted, which helps make sure we actually latched a |
| 171 // copy, not a reference. |
| 172 |
| 173 // Retry the resove, should get the PPD back now. |
| 174 { |
| 175 CapturedResolveResult captured; |
| 176 |
| 177 ppd_provider_->Resolve(ppd_reference, |
| 178 base::Bind(CaptureResolveResultCallback, &captured)); |
| 179 base::RunLoop().RunUntilIdle(); |
| 180 CheckResolveSuccessful(captured, kLocalPpdContents); |
| 181 } |
| 182 } |
| 183 |
| 184 } // namespace |
| 185 } // namespace printing |
| 186 } // namespace chromeos |
OLD | NEW |