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