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( |
| 101 std::make_pair("Style1", FontInformation("Path", "FullName"))); |
| 102 fonts.insert(std::make_pair("Font", face_map)); |
| 103 SetCacheMap(fonts); |
| 104 |
| 105 base::RunLoop run_loop; |
| 106 service_impl_->GetFontList( |
| 107 base::Bind(&FontAccessServiceTest::VerifyFontListCallback, fonts, 1, |
| 108 run_loop.QuitClosure())); |
| 109 run_loop.Run(); |
| 110 EXPECT_EQ(1, counter_->counter); |
| 111 } |
| 112 |
| 113 TEST_F(FontAccessServiceTest, GetFontsRecaches) { |
| 114 FontCacheMap fonts; |
| 115 FontStyleMap face_map; |
| 116 face_map.insert( |
| 117 std::make_pair("Style1", FontInformation("Path", "FullName"))); |
| 118 fonts.insert(std::make_pair("Font", face_map)); |
| 119 SetCacheMap(fonts); |
| 120 |
| 121 { |
| 122 base::RunLoop run_loop; |
| 123 service_impl_->GetFontList( |
| 124 base::Bind(&FontAccessServiceTest::VerifyFontListCallback, fonts, 1, |
| 125 run_loop.QuitClosure())); |
| 126 run_loop.Run(); |
| 127 } |
| 128 |
| 129 fonts.insert(std::make_pair("Font2", face_map)); |
| 130 SetCacheMap(fonts); |
| 131 { |
| 132 base::RunLoop run_loop; |
| 133 service_impl_->GetFontList( |
| 134 base::Bind(&FontAccessServiceTest::VerifyFontListCallback, fonts, 2, |
| 135 run_loop.QuitClosure())); |
| 136 run_loop.Run(); |
| 137 } |
| 138 |
| 139 EXPECT_EQ(2, counter_->counter); |
| 140 } |
| 141 |
| 142 TEST_F(FontAccessServiceTest, GetFontBlob) { |
| 143 base::ScopedTempDir temp_dir; |
| 144 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 145 base::FilePath file_path = temp_dir.path().AppendASCII("test_file.ttf"); |
| 146 base::File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 147 base::File::FLAG_WRITE); |
| 148 ASSERT_TRUE(file.IsValid()); |
| 149 |
| 150 char data_to_write[] = "test"; |
| 151 const int kTestDataSize = 4; |
| 152 int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
| 153 EXPECT_EQ(kTestDataSize, bytes_written); |
| 154 |
| 155 FontCacheMap fonts; |
| 156 FontStyleMap face_map; |
| 157 face_map.insert(std::make_pair( |
| 158 "Style1", FontInformation(file_path.AsUTF8Unsafe(), "FullName"))); |
| 159 fonts.insert(std::make_pair("Font", face_map)); |
| 160 SetCacheMap(fonts); |
| 161 |
| 162 { |
| 163 base::RunLoop run_loop; |
| 164 service_impl_->GetFontData( |
| 165 "Font", "Style1", |
| 166 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 167 kTestDataSize, run_loop.QuitClosure())); |
| 168 run_loop.Run(); |
| 169 } |
| 170 |
| 171 EXPECT_EQ(1, counter_->counter); |
| 172 } |
| 173 |
| 174 TEST_F(FontAccessServiceTest, GetFontBlobIsCached) { |
| 175 base::ScopedTempDir temp_dir; |
| 176 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 177 base::FilePath file_path = temp_dir.path().AppendASCII("test_file.ttf"); |
| 178 base::File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 179 base::File::FLAG_WRITE); |
| 180 ASSERT_TRUE(file.IsValid()); |
| 181 |
| 182 char data_to_write[] = "test"; |
| 183 const int kTestDataSize = 4; |
| 184 int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
| 185 EXPECT_EQ(kTestDataSize, bytes_written); |
| 186 |
| 187 FontCacheMap fonts; |
| 188 FontStyleMap face_map; |
| 189 face_map.insert(std::make_pair( |
| 190 "Style1", FontInformation(file_path.AsUTF8Unsafe(), "FullName"))); |
| 191 fonts.insert(std::make_pair("Font", face_map)); |
| 192 SetCacheMap(fonts); |
| 193 |
| 194 { |
| 195 base::RunLoop run_loop; |
| 196 service_impl_->GetFontData( |
| 197 "Font", "Style1", |
| 198 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 199 kTestDataSize, run_loop.QuitClosure())); |
| 200 run_loop.Run(); |
| 201 } |
| 202 { |
| 203 base::RunLoop run_loop; |
| 204 service_impl_->GetFontData( |
| 205 "Font", "Style1", |
| 206 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 207 kTestDataSize, run_loop.QuitClosure())); |
| 208 run_loop.Run(); |
| 209 } |
| 210 |
| 211 EXPECT_EQ(1, counter_->counter); |
| 212 } |
| 213 |
| 214 TEST_F(FontAccessServiceTest, GetFontBlobRecachesOnMiss) { |
| 215 base::ScopedTempDir temp_dir; |
| 216 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 217 base::FilePath file_path = temp_dir.path().AppendASCII("test_file.ttf"); |
| 218 base::File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 219 base::File::FLAG_WRITE); |
| 220 ASSERT_TRUE(file.IsValid()); |
| 221 |
| 222 char data_to_write[] = "test"; |
| 223 const int kTestDataSize = 4; |
| 224 int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
| 225 EXPECT_EQ(kTestDataSize, bytes_written); |
| 226 |
| 227 FontCacheMap fonts; |
| 228 FontStyleMap face_map; |
| 229 face_map.insert(std::make_pair( |
| 230 "Style1", FontInformation(file_path.AsUTF8Unsafe(), "FullName"))); |
| 231 fonts.insert(std::make_pair("Font", face_map)); |
| 232 SetCacheMap(fonts); |
| 233 |
| 234 // Populates the cache and returns the first font. |
| 235 { |
| 236 base::RunLoop run_loop; |
| 237 service_impl_->GetFontData( |
| 238 "Font", "Style1", |
| 239 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 240 kTestDataSize, run_loop.QuitClosure())); |
| 241 run_loop.Run(); |
| 242 } |
| 243 |
| 244 // Expected cache miss and a second miss after recaching. |
| 245 { |
| 246 base::RunLoop run_loop; |
| 247 service_impl_->GetFontData( |
| 248 "Font2", "Style1", |
| 249 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, 0, |
| 250 run_loop.QuitClosure())); |
| 251 run_loop.Run(); |
| 252 } |
| 253 EXPECT_EQ(2, counter_->counter); |
| 254 |
| 255 fonts.insert(std::make_pair("Font2", face_map)); |
| 256 SetCacheMap(fonts); |
| 257 |
| 258 // Expected cache miss with eventual result on recache. |
| 259 { |
| 260 base::RunLoop run_loop; |
| 261 service_impl_->GetFontData( |
| 262 "Font2", "Style1", |
| 263 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, |
| 264 kTestDataSize, run_loop.QuitClosure())); |
| 265 run_loop.Run(); |
| 266 } |
| 267 |
| 268 EXPECT_EQ(3, counter_->counter); |
| 269 } |
| 270 |
| 271 TEST_F(FontAccessServiceTest, FontBlobPathIsInvalid) { |
| 272 base::ScopedTempDir temp_dir; |
| 273 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 274 base::FilePath file_path = temp_dir.path().AppendASCII("test_file.ttf"); |
| 275 |
| 276 FontCacheMap fonts; |
| 277 FontStyleMap face_map; |
| 278 face_map.insert(std::make_pair( |
| 279 "Style1", FontInformation(file_path.AsUTF8Unsafe(), "FullName"))); |
| 280 fonts.insert(std::make_pair("Font", face_map)); |
| 281 SetCacheMap(fonts); |
| 282 |
| 283 { |
| 284 base::RunLoop run_loop; |
| 285 service_impl_->GetFontData( |
| 286 "Font", "Style1", |
| 287 base::Bind(&FontAccessServiceTest::VerifyFontBlobCallback, 0, |
| 288 run_loop.QuitClosure())); |
| 289 run_loop.Run(); |
| 290 } |
| 291 |
| 292 EXPECT_EQ(1, counter_->counter); |
| 293 } |
| 294 } |
OLD | NEW |