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/run_loop.h" |
| 6 #include "content/browser/font_access/font_access_service_impl.h" |
| 7 #include "content/browser/font_access/font_getter.h" |
| 8 #include "content/common/font_access/font_access_service.mojom.h" |
| 9 #include "content/public/test/mock_render_process_host.h" |
| 10 #include "content/public/test/test_browser_context.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 13 #include "mojo/public/cpp/bindings/interface_request.h" |
| 14 #include "mojo/public/cpp/system/buffer.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 class CacheClobberCounter { |
| 20 public: |
| 21 CacheClobberCounter() : counter(0) {} |
| 22 |
| 23 void SetCacheMap(const FontCacheMap& cache_map) { cache_map_ = cache_map; } |
| 24 |
| 25 scoped_ptr<FontCacheMap> GetFakeCacheMap() { |
| 26 scoped_ptr<FontCacheMap> clone_map(new FontCacheMap(cache_map_)); |
| 27 counter++; |
| 28 return clone_map; |
| 29 } |
| 30 |
| 31 int counter; |
| 32 FontCacheMap cache_map_; |
| 33 }; |
| 34 |
| 35 class FontAccessServiceTest : public testing::Test { |
| 36 public: |
| 37 FontAccessServiceTest() |
| 38 : context_(new TestBrowserContext()), |
| 39 host_(new MockRenderProcessHost(context_.get())), |
| 40 counter_(new CacheClobberCounter()) {} |
| 41 |
| 42 void SetUp() override { |
| 43 // Create a dummy mojo channel so that the FontAccessServiceImpl can be |
| 44 // instantiated |
| 45 mojo::InterfaceRequest<FontAccessService> request = |
| 46 mojo::GetProxy(&interface_ptr_); |
| 47 // Create a new FontAccessServiceImpl bound to the dummy channel. |
| 48 service_impl_.reset(new FontAccessServiceImpl( |
| 49 base::Bind(&CacheClobberCounter::GetFakeCacheMap, |
| 50 base::Unretained(counter_.get())), |
| 51 std::move(request))); |
| 52 base::RunLoop().RunUntilIdle(); |
| 53 } |
| 54 |
| 55 void SetCacheMap(const FontCacheMap& cache_map) { |
| 56 counter_->SetCacheMap(cache_map); |
| 57 } |
| 58 |
| 59 static void VerifyFontListCallback( |
| 60 const FontCacheMap& real_map, |
| 61 size_t expected_fonts, |
| 62 base::Closure quit, |
| 63 const mojo::Array<FontDescriptionPtr>& values) { |
| 64 EXPECT_EQ(expected_fonts, values.size()); |
| 65 // Verify that all of the fonts were there. |
| 66 for (size_t i = 0; i < values.size(); i++) { |
| 67 FontCacheMap::const_iterator iter; |
| 68 iter = real_map.find(values[i]->family); |
| 69 EXPECT_NE(iter, real_map.end()); |
| 70 FontStyleMap::const_iterator faceIter; |
| 71 faceIter = iter->second.find(values[i]->style); |
| 72 EXPECT_NE(faceIter, iter->second.end()); |
| 73 } |
| 74 |
| 75 quit.Run(); |
| 76 } |
| 77 |
| 78 static void VerifyFontBlobCallback(int64_t expected_length, |
| 79 base::Closure quit, |
| 80 mojo::ScopedSharedBufferHandle buffer, |
| 81 int64_t length) { |
| 82 quit.Run(); |
| 83 EXPECT_EQ(length, expected_length); |
| 84 } |
| 85 |
| 86 mojo::InterfacePtr<FontAccessService> interface_ptr_; |
| 87 content::TestBrowserThreadBundle thread_bundle_; |
| 88 scoped_ptr<TestBrowserContext> context_; |
| 89 scoped_ptr<MockRenderProcessHost> host_; |
| 90 scoped_ptr<FontAccessServiceImpl> service_impl_; |
| 91 scoped_ptr<CacheClobberCounter> counter_; |
| 92 |
| 93 private: |
| 94 DISALLOW_COPY_AND_ASSIGN(FontAccessServiceTest); |
| 95 }; |
| 96 |
| 97 TEST_F(FontAccessServiceTest, GetFonts) { |
| 98 FontCacheMap fonts; |
| 99 FontStyleMap face_map; |
| 100 face_map.insert(std::make_pair("Style1", |
| 101 FontInformation("Path", |
| 102 "FullName"))); |
| 103 fonts.insert(std::make_pair("Font", face_map)); |
| 104 SetCacheMap(fonts); |
| 105 |
| 106 base::RunLoop run_loop; |
| 107 service_impl_->GetFontList( |
| 108 base::Bind(&FontAccessServiceTest::VerifyFontListCallback, fonts, 1, |
| 109 run_loop.QuitClosure())); |
| 110 run_loop.Run(); |
| 111 EXPECT_EQ(1, counter_->counter); |
| 112 } |
| 113 |
| 114 TEST_F(FontAccessServiceTest, GetFontsRecaches) { |
| 115 FontCacheMap fonts; |
| 116 FontStyleMap face_map; |
| 117 face_map.insert(std::make_pair("Style1", |
| 118 FontInformation("Path", |
| 119 "FullName"))); |
| 120 fonts.insert(std::make_pair("Font", face_map)); |
| 121 SetCacheMap(fonts); |
| 122 |
| 123 { |
| 124 base::RunLoop run_loop; |
| 125 service_impl_->GetFontList( |
| 126 base::Bind(&FontAccessServiceTest::VerifyFontListCallback, fonts, 1, |
| 127 run_loop.QuitClosure())); |
| 128 run_loop.Run(); |
| 129 } |
| 130 |
| 131 fonts.insert(std::make_pair("Font2", face_map)); |
| 132 SetCacheMap(fonts); |
| 133 { |
| 134 base::RunLoop run_loop; |
| 135 service_impl_->GetFontList( |
| 136 base::Bind(&FontAccessServiceTest::VerifyFontListCallback, fonts, 2, |
| 137 run_loop.QuitClosure())); |
| 138 run_loop.Run(); |
| 139 } |
| 140 |
| 141 EXPECT_EQ(2, counter_->counter); |
| 142 } |
| 143 |
| 144 TEST_F(FontAccessServiceTest, GetFontBlob) { |
| 145 base::ScopedTempDir temp_dir; |
| 146 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 147 base::FilePath file_path = temp_dir.path().AppendASCII("test_file.ttf"); |
| 148 base::File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 149 base::File::FLAG_WRITE); |
| 150 ASSERT_TRUE(file.IsValid()); |
| 151 |
| 152 char data_to_write[] = "test"; |
| 153 const int kTestDataSize = 4; |
| 154 int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
| 155 EXPECT_EQ(kTestDataSize, bytes_written); |
| 156 |
| 157 FontCacheMap fonts; |
| 158 FontStyleMap face_map; |
| 159 face_map.insert(std::make_pair("Style1", |
| 160 FontInformation(file_path.AsUTF8Unsafe(), |
| 161 "FullName"))); |
| 162 fonts.insert(std::make_pair("Font", face_map)); |
| 163 SetCacheMap(fonts); |
| 164 |
| 165 { |
| 166 base::RunLoop run_loop; |
| 167 service_impl_->GetFontData( |
| 168 "Font", "Style1", |
| 169 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 170 kTestDataSize, |
| 171 run_loop.QuitClosure())); |
| 172 run_loop.Run(); |
| 173 } |
| 174 |
| 175 EXPECT_EQ(1, counter_->counter); |
| 176 } |
| 177 |
| 178 TEST_F(FontAccessServiceTest, GetFontBlobIsCached) { |
| 179 base::ScopedTempDir temp_dir; |
| 180 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 181 base::FilePath file_path = temp_dir.path().AppendASCII("test_file.ttf"); |
| 182 base::File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 183 base::File::FLAG_WRITE); |
| 184 ASSERT_TRUE(file.IsValid()); |
| 185 |
| 186 char data_to_write[] = "test"; |
| 187 const int kTestDataSize = 4; |
| 188 int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
| 189 EXPECT_EQ(kTestDataSize, bytes_written); |
| 190 |
| 191 FontCacheMap fonts; |
| 192 FontStyleMap face_map; |
| 193 face_map.insert(std::make_pair("Style1", |
| 194 FontInformation(file_path.AsUTF8Unsafe(), |
| 195 "FullName"))); |
| 196 fonts.insert(std::make_pair("Font", face_map)); |
| 197 SetCacheMap(fonts); |
| 198 |
| 199 { |
| 200 base::RunLoop run_loop; |
| 201 service_impl_->GetFontData( |
| 202 "Font", "Style1", |
| 203 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 204 kTestDataSize, |
| 205 run_loop.QuitClosure())); |
| 206 run_loop.Run(); |
| 207 } |
| 208 { |
| 209 base::RunLoop run_loop; |
| 210 service_impl_->GetFontData( |
| 211 "Font", "Style1", |
| 212 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 213 kTestDataSize, |
| 214 run_loop.QuitClosure())); |
| 215 run_loop.Run(); |
| 216 } |
| 217 |
| 218 EXPECT_EQ(1, counter_->counter); |
| 219 } |
| 220 |
| 221 TEST_F(FontAccessServiceTest, GetFontBlobRecachesOnMiss) { |
| 222 base::ScopedTempDir temp_dir; |
| 223 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 224 base::FilePath file_path = temp_dir.path().AppendASCII("test_file.ttf"); |
| 225 base::File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 226 base::File::FLAG_WRITE); |
| 227 ASSERT_TRUE(file.IsValid()); |
| 228 |
| 229 char data_to_write[] = "test"; |
| 230 const int kTestDataSize = 4; |
| 231 int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
| 232 EXPECT_EQ(kTestDataSize, bytes_written); |
| 233 |
| 234 FontCacheMap fonts; |
| 235 FontStyleMap face_map; |
| 236 face_map.insert(std::make_pair("Style1", |
| 237 FontInformation(file_path.AsUTF8Unsafe(), |
| 238 "FullName"))); |
| 239 fonts.insert(std::make_pair("Font", face_map)); |
| 240 SetCacheMap(fonts); |
| 241 |
| 242 // Populates the cache and returns the first font. |
| 243 { |
| 244 base::RunLoop run_loop; |
| 245 service_impl_->GetFontData( |
| 246 "Font", "Style1", |
| 247 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 248 kTestDataSize, |
| 249 run_loop.QuitClosure())); |
| 250 run_loop.Run(); |
| 251 } |
| 252 |
| 253 // Expected cache miss and a second miss after recaching. |
| 254 { |
| 255 base::RunLoop run_loop; |
| 256 service_impl_->GetFontData( |
| 257 "Font2", "Style1", |
| 258 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, 0, |
| 259 run_loop.QuitClosure())); |
| 260 run_loop.Run(); |
| 261 } |
| 262 EXPECT_EQ(2, counter_->counter); |
| 263 |
| 264 fonts.insert(std::make_pair("Font2", face_map)); |
| 265 SetCacheMap(fonts); |
| 266 |
| 267 // Expected cache miss with eventual result on recache. |
| 268 { |
| 269 base::RunLoop run_loop; |
| 270 service_impl_->GetFontData( |
| 271 "Font2", "Style1", |
| 272 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 273 kTestDataSize, |
| 274 run_loop.QuitClosure())); |
| 275 run_loop.Run(); |
| 276 } |
| 277 |
| 278 EXPECT_EQ(3, counter_->counter); |
| 279 } |
| 280 |
| 281 TEST_F(FontAccessServiceTest, FontBlobPathIsInvalid) { |
| 282 base::ScopedTempDir temp_dir; |
| 283 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 284 base::FilePath file_path = temp_dir.path().AppendASCII("test_file.ttf"); |
| 285 |
| 286 FontCacheMap fonts; |
| 287 FontStyleMap face_map; |
| 288 face_map.insert(std::make_pair("Style1", |
| 289 FontInformation(file_path.AsUTF8Unsafe(), |
| 290 "FullName"))); |
| 291 fonts.insert(std::make_pair("Font", face_map)); |
| 292 SetCacheMap(fonts); |
| 293 |
| 294 { |
| 295 base::RunLoop run_loop; |
| 296 service_impl_->GetFontData( |
| 297 "Font", "Style1", |
| 298 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 299 0, |
| 300 run_loop.QuitClosure())); |
| 301 run_loop.Run(); |
| 302 } |
| 303 |
| 304 EXPECT_EQ(1, counter_->counter); |
| 305 } |
| 306 } |
OLD | NEW |