Chromium Code Reviews| Index: chromeos/printing/ppd_provider_unittest.cc |
| diff --git a/chromeos/printing/ppd_provider_unittest.cc b/chromeos/printing/ppd_provider_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..55722544f9cc5f521cc8d8e91445faa2db5456d4 |
| --- /dev/null |
| +++ b/chromeos/printing/ppd_provider_unittest.cc |
| @@ -0,0 +1,181 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/test/scoped_path_override.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "chromeos/chromeos_paths.h" |
| +#include "chromeos/printing/ppd_cache.h" |
| +#include "chromeos/printing/ppd_provider.h" |
| +#include "net/url_request/test_url_request_interceptor.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using base::FilePath; |
| +using std::string; |
| +using std::unique_ptr; |
| + |
| +namespace chromeos { |
| +namespace printing { |
| +namespace { |
| +#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
|
| +const char kTestQuirksServer[] = "bogusserver.bogus.com"; |
| +const char kTestAPIKey[] = "BOGUSAPIKEY"; |
| +const char kTestManufacturer[] = "Bogus Printer Corp"; |
| +const char kTestModel[] = "MegaPrint 9000"; |
| +const char kQuirksResponse[] = |
| + "{\n" |
| + " \"compressedPpd\": \"This is the quirks ppd\",\n" |
| + " \"lastUpdatedTime\": \"1\"\n" |
| + "}\n"; |
| +const char kQuirksPPD[] = "This is the quirks ppd"; |
| + |
| +class PPDProviderTest : public ::testing::Test { |
| + public: |
| + PPDProviderTest() |
| + : ppd_cache_dir_override_(DIR_PRINTER_DRIVERS_CACHE), |
| + loop_(base::MessageLoop::TYPE_IO), |
| + request_context_getter_( |
| + new ::net::TestURLRequestContextGetter(loop_.task_runner().get())) { |
| + } |
| + |
| + // Create the ppd_provider_ to be used for this test. |
| + void SetUp() override { |
| + auto provider_options = PPDProvider::Options(); |
| + provider_options.quirks_server = kTestQuirksServer; |
| + ppd_provider_ = PPDProvider::Create(kTestAPIKey, request_context_getter_, |
| + PPDCache::Create(), provider_options); |
| + } |
| + |
| + protected: |
| + // Overrider for DIR_PRINTER_DRIVERS_CACHE that points it at a temporary |
| + // directory for the life of the test. |
| + base::ScopedPathOverride ppd_cache_dir_override_; |
| + |
| + // Provider to be used in the test. |
| + unique_ptr<PPDProvider> ppd_provider_; |
| + |
| + // Misc extra stuff needed for the test environment to function. |
| + base::MessageLoop loop_; |
| + scoped_refptr<::net::URLRequestContextGetter> request_context_getter_; |
| +}; |
| + |
| +// Struct that just captures the callback result for a PPDProvider lookup and |
| +// saves it for inspection by the test. |
| +struct CapturedResolveResult { |
| + bool initialized = false; |
| + PPDProvider::ResolveResult result; |
| + FilePath file; |
| +}; |
| + |
| +// Callback for saving a resolve callback. |
| +void CaptureResolveResult(CapturedResolveResult* capture, |
| + PPDProvider::ResolveResult result, |
| + FilePath file) { |
| + capture->initialized = true; |
| + capture->result = result; |
| + capture->file = file; |
| +} |
| + |
| +// For a resolve result that should end up successful, check that it is |
| +// successful and the contents are expected_contents. |
| +void CheckResolveSuccessful(const CapturedResolveResult& captured, |
| + const string& expected_contents) { |
| + ASSERT_TRUE(captured.initialized); |
| + ASSERT_EQ(captured.result, PPDProvider::SUCCESS); |
| + |
| + string contents; |
| + ASSERT_TRUE(base::ReadFileToString(captured.file, &contents)); |
| + EXPECT_EQ(expected_contents, contents); |
| +} |
| + |
| +// Resolve a ppd via the quirks server. |
| +TEST_F(PPDProviderTest, QuirksServerResolve) { |
| + base::ScopedTempDir temp_dir; |
| + CHECK(temp_dir.CreateUniqueTempDir()); |
| + |
| + { |
| + ::net::TestURLRequestInterceptor interceptor( |
| + "https", kTestQuirksServer, base::ThreadTaskRunnerHandle::Get(), |
| + base::ThreadTaskRunnerHandle::Get()); |
| + |
| + GURL expected_url(string("https://") + kTestQuirksServer // |
| + + "/v2/printer/manufacturers/" // |
| + + kTestManufacturer // |
| + + "/models/" // |
| + + kTestModel // |
| + + "?key=" // |
| + + kTestAPIKey); |
| + |
| + FilePath contents_path = temp_dir.path().Append("response"); |
| + string contents = kQuirksResponse; |
| + CHECK_GT(base::WriteFile(contents_path, kQuirksResponse, |
| + strlen(kQuirksResponse)), |
| + 0); |
| + |
| + interceptor.SetResponse(expected_url, contents_path); |
| + |
| + CapturedResolveResult captured; |
| + ppd_provider_->Resolve(kTestManufacturer, kTestModel, |
| + base::Bind(CaptureResolveResult, &captured)); |
| + base::RunLoop().RunUntilIdle(); |
| + CheckResolveSuccessful(captured, kQuirksPPD); |
| + } |
| + |
| + // Now that the interceptor is out of scope, re-run the query. We should |
| + // hit in the cache, and thus *not* re-run the query. |
| + CapturedResolveResult captured; |
| + ppd_provider_->Resolve(kTestManufacturer, kTestModel, |
| + base::Bind(CaptureResolveResult, &captured)); |
| + base::RunLoop().RunUntilIdle(); |
| + CheckResolveSuccessful(captured, kQuirksPPD); |
| +} |
| + |
| +// Test storage and retrieval of ppds that are added manually. |
| +TEST_F(PPDProviderTest, LocalResolve) { |
| + const string kTestPrinterId("My printer"); |
| + { |
| + CapturedResolveResult captured; |
| + ppd_provider_->ResolveLocal(kTestPrinterId, |
| + base::Bind(CaptureResolveResult, &captured)); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_TRUE(captured.initialized); |
| + EXPECT_EQ(captured.result, PPDProvider::NOT_FOUND); |
| + } |
| + |
| + // Store a local ppd. |
| + const string kLocalPPDContents("My local ppd contents"); |
| + { |
| + base::ScopedTempDir temp_dir; |
| + CHECK(temp_dir.CreateUniqueTempDir()); |
| + |
| + FilePath local_ppd_path = temp_dir.path().Append("local_ppd"); |
| + ASSERT_EQ(base::WriteFile(local_ppd_path, kLocalPPDContents.data(), |
| + kLocalPPDContents.size()), |
| + static_cast<int>(kLocalPPDContents.size())); |
| + ASSERT_TRUE(ppd_provider_->StoreLocal(kTestPrinterId, local_ppd_path)); |
| + } |
| + // temp_dir should now be deleted, which helps make sure we actually latched a |
| + // copy, not a reference. |
| + |
| + // Retry the resove, should get the ppd back now. |
| + { |
| + CapturedResolveResult captured; |
| + |
| + ppd_provider_->ResolveLocal(kTestPrinterId, |
| + base::Bind(CaptureResolveResult, &captured)); |
| + base::RunLoop().RunUntilIdle(); |
| + CheckResolveSuccessful(captured, kLocalPPDContents); |
| + } |
| +} |
| +#endif |
| +} // namespace |
| +} // namespace printing |
| +} // namespace chromeos |