face_detection_impl_win_unittest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2017 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. #include <memory>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include "base/bind.h"
  9. #include "base/callback.h"
  10. #include "base/files/file_util.h"
  11. #include "base/path_service.h"
  12. #include "base/run_loop.h"
  13. #include "base/test/task_environment.h"
  14. #include "base/win/scoped_com_initializer.h"
  15. #include "base/win/windows_version.h"
  16. #include "mojo/public/cpp/bindings/remote.h"
  17. #include "services/shape_detection/face_detection_provider_win.h"
  18. #include "services/shape_detection/public/mojom/facedetection_provider.mojom.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. #include "ui/gfx/codec/jpeg_codec.h"
  21. namespace shape_detection {
  22. namespace {
  23. void DetectCallback(base::OnceClosure quit_closure,
  24. uint32_t* num_faces,
  25. std::vector<mojom::FaceDetectionResultPtr> results) {
  26. *num_faces = results.size();
  27. std::move(quit_closure).Run();
  28. }
  29. } // namespace
  30. class FaceDetectionImplWinTest : public testing::Test {
  31. public:
  32. FaceDetectionImplWinTest(const FaceDetectionImplWinTest&) = delete;
  33. FaceDetectionImplWinTest& operator=(const FaceDetectionImplWinTest&) = delete;
  34. protected:
  35. FaceDetectionImplWinTest() = default;
  36. ~FaceDetectionImplWinTest() override = default;
  37. void SetUp() override {
  38. scoped_com_initializer_ = std::make_unique<base::win::ScopedCOMInitializer>(
  39. base::win::ScopedCOMInitializer::kMTA);
  40. ASSERT_TRUE(scoped_com_initializer_->Succeeded());
  41. }
  42. mojo::Remote<mojom::FaceDetection> ConnectToFaceDetector() {
  43. mojo::Remote<mojom::FaceDetectionProvider> provider;
  44. mojo::Remote<mojom::FaceDetection> face_service;
  45. FaceDetectionProviderWin::Create(provider.BindNewPipeAndPassReceiver());
  46. auto options = shape_detection::mojom::FaceDetectorOptions::New();
  47. provider->CreateFaceDetection(face_service.BindNewPipeAndPassReceiver(),
  48. std::move(options));
  49. return face_service;
  50. }
  51. std::unique_ptr<SkBitmap> LoadTestImage() {
  52. // Load image data from test directory.
  53. base::FilePath image_path;
  54. EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &image_path));
  55. image_path = image_path.Append(FILE_PATH_LITERAL("services"))
  56. .Append(FILE_PATH_LITERAL("test"))
  57. .Append(FILE_PATH_LITERAL("data"))
  58. .Append(FILE_PATH_LITERAL("mona_lisa.jpg"));
  59. EXPECT_TRUE(base::PathExists(image_path));
  60. std::string image_data;
  61. EXPECT_TRUE(base::ReadFileToString(image_path, &image_data));
  62. std::unique_ptr<SkBitmap> image = gfx::JPEGCodec::Decode(
  63. reinterpret_cast<const uint8_t*>(image_data.data()), image_data.size());
  64. if (image) {
  65. const gfx::Size size(image->width(), image->height());
  66. const uint32_t num_bytes = size.GetArea() * 4 /* bytes per pixel */;
  67. EXPECT_EQ(num_bytes, image->computeByteSize());
  68. }
  69. return image;
  70. }
  71. private:
  72. std::unique_ptr<base::win::ScopedCOMInitializer> scoped_com_initializer_;
  73. base::test::TaskEnvironment task_environment_;
  74. };
  75. TEST_F(FaceDetectionImplWinTest, ScanOneFace) {
  76. // FaceDetector not supported before Windows 10
  77. if (base::win::GetVersion() < base::win::Version::WIN10)
  78. return;
  79. mojo::Remote<mojom::FaceDetection> face_detector = ConnectToFaceDetector();
  80. std::unique_ptr<SkBitmap> image = LoadTestImage();
  81. ASSERT_TRUE(image);
  82. base::RunLoop run_loop;
  83. uint32_t num_faces = 0;
  84. face_detector->Detect(
  85. *image,
  86. base::BindOnce(&DetectCallback, run_loop.QuitClosure(), &num_faces));
  87. run_loop.Run();
  88. EXPECT_EQ(1u, num_faces);
  89. }
  90. } // namespace shape_detection