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..86030e061dd7f7e082851c2360f88c2e9bbb305e |
--- /dev/null |
+++ b/chromeos/printing/ppd_provider_unittest.cc |
@@ -0,0 +1,191 @@ |
+// 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/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 { |
+ |
+const char kTestQuirksServer[] = "bogusserver.bogus.com"; |
+const char kTestAPIKey[] = "BOGUSAPIKEY"; |
+const char kLocalPpdUrl[] = "/some/path"; |
+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() |
+ : loop_(base::MessageLoop::TYPE_IO), |
+ request_context_getter_( |
+ new ::net::TestURLRequestContextGetter(loop_.task_runner().get())) { |
+ CHECK(ppd_cache_temp_dir_.CreateUniqueTempDir()); |
+ } |
+ |
+ // 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(ppd_cache_temp_dir_.GetPath()), provider_options); |
+ } |
+ |
+ protected: |
+ base::ScopedTempDir ppd_cache_temp_dir_; |
+ |
+ // 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, |
Lei Zhang
2016/10/19 01:25:10
Can this be CaptureResolveResultCallback? It's a b
Carlson
2016/10/19 16:38:27
Done.
|
+ 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); |
+ EXPECT_EQ(captured.result, PpdProvider::SUCCESS); |
Lei Zhang
2016/10/19 01:25:10
More arguments to reverse.
Carlson
2016/10/19 16:38:27
Done.
|
+ |
+ 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()); |
+ |
+ Printer::PpdReference ppd_reference; |
+ ppd_reference.effective_manufacturer = kTestManufacturer; |
+ ppd_reference.effective_model = kTestModel; |
+ |
+ { |
+ ::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.GetPath().Append("response"); |
Lei Zhang
2016/10/19 01:25:10
BTW, base::FilePath::Append(c_str) is fine here, b
Carlson
2016/10/19 16:38:27
Acknowledged.
|
+ string contents = kQuirksResponse; |
+ CHECK_GT(base::WriteFile(contents_path, kQuirksResponse, |
Lei Zhang
2016/10/19 01:25:10
Maybe:
int bytes_written = base::WriteFile(...);
Carlson
2016/10/19 16:38:27
Done.
|
+ strlen(kQuirksResponse)), |
+ 0); |
+ |
+ interceptor.SetResponse(expected_url, contents_path); |
+ |
+ CapturedResolveResult captured; |
+ ppd_provider_->Resolve(ppd_reference, |
+ 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(ppd_reference, |
+ base::Bind(CaptureResolveResult, &captured)); |
+ base::RunLoop().RunUntilIdle(); |
+ CheckResolveSuccessful(captured, kQuirksPpd); |
+} |
+ |
+// Test storage and retrieval of ppds that are added manually. Even though we |
+// supply a manufacturer and model, we should *not* hit the network for this |
+// resolution since we should find the stored version already cached. |
+TEST_F(PpdProviderTest, LocalResolve) { |
+ Printer::PpdReference ppd_reference; |
+ ppd_reference.user_supplied_ppd_url = kLocalPpdUrl; |
+ ppd_reference.effective_manufacturer = kTestManufacturer; |
+ ppd_reference.effective_model = kTestModel; |
+ |
+ // Initially, should not resolve. |
+ { |
+ CapturedResolveResult captured; |
+ ppd_provider_->Resolve(ppd_reference, |
+ 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.GetPath().Append("local_ppd"); |
+ ASSERT_EQ(base::WriteFile(local_ppd_path, kLocalPpdContents.data(), |
+ kLocalPpdContents.size()), |
+ static_cast<int>(kLocalPpdContents.size())); |
+ ASSERT_TRUE(ppd_provider_->CachePpd(ppd_reference, 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_->Resolve(ppd_reference, |
+ base::Bind(CaptureResolveResult, &captured)); |
+ base::RunLoop().RunUntilIdle(); |
+ CheckResolveSuccessful(captured, kLocalPpdContents); |
+ } |
+} |
+ |
+} // namespace |
+} // namespace printing |
+} // namespace chromeos |