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/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 |
| 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(base::WriteFile(contents_path, kQuirksResponse, |
| 120 strlen(kQuirksResponse)) > 0); |
| 121 |
| 122 interceptor.SetResponse(expected_url, contents_path); |
| 123 |
| 124 CapturedResolveResult captured; |
| 125 ppd_provider_->Resolve(kTestManufacturer, kTestModel, |
| 126 base::Bind(CaptureResolveResult, &captured)); |
| 127 base::RunLoop().RunUntilIdle(); |
| 128 CheckResolveSuccessful(captured, kQuirksPPD); |
| 129 } |
| 130 |
| 131 // Now that the interceptor is out of scope, re-run the query. We should |
| 132 // hit in the cache, and thus *not* re-run the query. |
| 133 CapturedResolveResult captured; |
| 134 ppd_provider_->Resolve(kTestManufacturer, kTestModel, |
| 135 base::Bind(CaptureResolveResult, &captured)); |
| 136 base::RunLoop().RunUntilIdle(); |
| 137 CheckResolveSuccessful(captured, kQuirksPPD); |
| 138 } |
| 139 |
| 140 // Test storage and retrieval of ppds that are added manually. |
| 141 TEST_F(PPDProviderTest, LocalResolve) { |
| 142 const string kTestPrinterId("My printer"); |
| 143 { |
| 144 CapturedResolveResult captured; |
| 145 ppd_provider_->ResolveLocal(kTestPrinterId, |
| 146 base::Bind(CaptureResolveResult, &captured)); |
| 147 base::RunLoop().RunUntilIdle(); |
| 148 EXPECT_TRUE(captured.initialized); |
| 149 EXPECT_EQ(captured.result, PPDProvider::NOT_FOUND); |
| 150 } |
| 151 |
| 152 // Store a local ppd. |
| 153 const string kLocalPPDContents("My local ppd contents"); |
| 154 { |
| 155 base::ScopedTempDir temp_dir; |
| 156 CHECK(temp_dir.CreateUniqueTempDir()); |
| 157 |
| 158 FilePath local_ppd_path = temp_dir.path().Append("local_ppd"); |
| 159 ASSERT_EQ(base::WriteFile(local_ppd_path, kLocalPPDContents.data(), |
| 160 kLocalPPDContents.size()), |
| 161 static_cast<int>(kLocalPPDContents.size())); |
| 162 ASSERT_TRUE(ppd_provider_->StoreLocal(kTestPrinterId, local_ppd_path)); |
| 163 } |
| 164 // temp_dir should now be deleted, which helps make sure we actually latched a |
| 165 // copy, not a reference. |
| 166 |
| 167 // Retry the resove, should get the ppd back now. |
| 168 { |
| 169 CapturedResolveResult captured; |
| 170 |
| 171 ppd_provider_->ResolveLocal(kTestPrinterId, |
| 172 base::Bind(CaptureResolveResult, &captured)); |
| 173 base::RunLoop().RunUntilIdle(); |
| 174 CheckResolveSuccessful(captured, kLocalPPDContents); |
| 175 } |
| 176 } |
| 177 |
| 178 } // namespace |
| 179 } // namespace printing |
| 180 } // namespace chromeos |
OLD | NEW |