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