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/threading/thread_task_runner_handle.h" |
| 13 #include "chromeos/printing/ppd_cache.h" |
| 14 #include "chromeos/printing/ppd_provider.h" |
| 15 #include "net/url_request/test_url_request_interceptor.h" |
| 16 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using ::base::FilePath; |
| 20 using ::base::WaitableEvent; |
| 21 using ::std::string; |
| 22 |
| 23 namespace chromeos { |
| 24 namespace printing { |
| 25 namespace { |
| 26 |
| 27 const char kTestQuirksServer[] = "bogusserver.bogus.com"; |
| 28 const char kTestAPIKey[] = "BOGUSAPIKEY"; |
| 29 const char kTestManufacturer[] = "Bogus Printer Corp"; |
| 30 const char kTestModel[] = "MegaPrint 9000"; |
| 31 const char kQuirksResponse[] = |
| 32 "{\n" |
| 33 " \"compressedPpd\": \"\u000e4manu_a model_1\",\n" |
| 34 " \"lastUpdatedTime\": \"1\"\n" |
| 35 "}\n"; |
| 36 |
| 37 class PPDProviderTest : public ::testing::Test { |
| 38 public: |
| 39 PPDProviderTest() |
| 40 : loop_(base::MessageLoop::TYPE_IO), |
| 41 request_context_getter_( |
| 42 new ::net::TestURLRequestContextGetter(loop_.task_runner().get())) { |
| 43 } |
| 44 |
| 45 protected: |
| 46 ::base::MessageLoop loop_; |
| 47 ::base::RunLoop run_loop_; |
| 48 scoped_refptr<::net::URLRequestContextGetter> request_context_getter_; |
| 49 }; |
| 50 |
| 51 // Struct that just captures the callback result for a PPDProvider lookup |
| 52 struct CapturedLookupResult { |
| 53 bool initialized = false; |
| 54 PPDProvider::LookupResult result; |
| 55 Printer::PPDFile file; |
| 56 }; |
| 57 |
| 58 void CaptureLookupResult(CapturedLookupResult* capture, |
| 59 PPDProvider::LookupResult result, |
| 60 Printer::PPDFile file) { |
| 61 capture->initialized = true; |
| 62 capture->result = result; |
| 63 capture->file = file; |
| 64 } |
| 65 |
| 66 TEST_F(PPDProviderTest, hmm) { |
| 67 ::base::ScopedTempDir temp_dir; |
| 68 CHECK(temp_dir.CreateUniqueTempDir()); |
| 69 auto cache_options = PPDCache::Options(); |
| 70 cache_options.cache_directory = temp_dir.path(); |
| 71 auto cache = PPDCache::Create(cache_options); |
| 72 auto provider_options = PPDProvider::Options(); |
| 73 provider_options.quirks_server = kTestQuirksServer; |
| 74 provider_options.api_key = kTestAPIKey; |
| 75 auto provider = PPDProvider::Create(request_context_getter_, std::move(cache), |
| 76 provider_options); |
| 77 Printer::PPDFile out; |
| 78 |
| 79 ::net::TestURLRequestInterceptor interceptor( |
| 80 "https", kTestQuirksServer, ::base::ThreadTaskRunnerHandle::Get(), |
| 81 ::base::ThreadTaskRunnerHandle::Get()); |
| 82 |
| 83 GURL expected_url(string("https://") + kTestQuirksServer // |
| 84 + "/v2/printer/manufacturers/" // |
| 85 + kTestManufacturer // |
| 86 + "/models/" // |
| 87 + kTestModel // |
| 88 + "?key=" // |
| 89 + kTestAPIKey); |
| 90 |
| 91 FilePath contents_path = temp_dir.path().Append("response"); |
| 92 string contents = kQuirksResponse; |
| 93 CHECK(base::WriteFile(contents_path, kQuirksResponse, |
| 94 strlen(kQuirksResponse)) > 0); |
| 95 |
| 96 interceptor.SetResponse(expected_url, contents_path); |
| 97 |
| 98 CapturedLookupResult captured; |
| 99 provider->Lookup(kTestManufacturer, kTestModel, |
| 100 ::base::Bind(CaptureLookupResult, &captured)); |
| 101 run_loop_.RunUntilIdle(); |
| 102 ASSERT_TRUE(captured.initialized); |
| 103 ASSERT_EQ(captured.result, PPDProvider::SUCCESS); |
| 104 } |
| 105 |
| 106 } // namespace |
| 107 } // namespace printing |
| 108 } // namespace chromeos |
OLD | NEW |