text_detection_impl_win_unittest.cc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <utility>
  6. #include <vector>
  7. #include "base/bind.h"
  8. #include "base/files/file_path.h"
  9. #include "base/files/file_util.h"
  10. #include "base/path_service.h"
  11. #include "base/run_loop.h"
  12. #include "base/test/task_environment.h"
  13. #include "base/win/scoped_com_initializer.h"
  14. #include "base/win/windows_version.h"
  15. #include "mojo/public/cpp/bindings/remote.h"
  16. #include "services/shape_detection/public/mojom/textdetection.mojom.h"
  17. #include "services/shape_detection/text_detection_impl.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. #include "ui/gfx/codec/png_codec.h"
  20. namespace shape_detection {
  21. namespace {
  22. void DetectTextCallback(base::OnceClosure quit_closure,
  23. std::vector<mojom::TextDetectionResultPtr>* results_out,
  24. std::vector<mojom::TextDetectionResultPtr> results_in) {
  25. *results_out = std::move(results_in);
  26. std::move(quit_closure).Run();
  27. }
  28. } // namespace
  29. class TextDetectionImplWinTest : public testing::Test {
  30. public:
  31. TextDetectionImplWinTest(const TextDetectionImplWinTest&) = delete;
  32. TextDetectionImplWinTest& operator=(const TextDetectionImplWinTest&) = delete;
  33. protected:
  34. TextDetectionImplWinTest() = default;
  35. ~TextDetectionImplWinTest() override = default;
  36. void SetUp() override {
  37. scoped_com_initializer_ = std::make_unique<base::win::ScopedCOMInitializer>(
  38. base::win::ScopedCOMInitializer::kMTA);
  39. ASSERT_TRUE(scoped_com_initializer_->Succeeded());
  40. }
  41. private:
  42. std::unique_ptr<base::win::ScopedCOMInitializer> scoped_com_initializer_;
  43. base::test::TaskEnvironment task_environment_;
  44. };
  45. TEST_F(TextDetectionImplWinTest, ScanOnce) {
  46. // OCR not supported before Windows 10
  47. if (base::win::GetVersion() < base::win::Version::WIN10)
  48. return;
  49. mojo::Remote<mojom::TextDetection> text_service;
  50. TextDetectionImpl::Create(text_service.BindNewPipeAndPassReceiver());
  51. // Load image data from test directory.
  52. base::FilePath image_path;
  53. ASSERT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &image_path));
  54. image_path = image_path.Append(FILE_PATH_LITERAL("services"))
  55. .Append(FILE_PATH_LITERAL("test"))
  56. .Append(FILE_PATH_LITERAL("data"))
  57. .Append(FILE_PATH_LITERAL("text_detection.png"));
  58. ASSERT_TRUE(base::PathExists(image_path));
  59. std::string image_data;
  60. ASSERT_TRUE(base::ReadFileToString(image_path, &image_data));
  61. SkBitmap bitmap;
  62. gfx::PNGCodec::Decode(reinterpret_cast<const uint8_t*>(image_data.data()),
  63. image_data.size(), &bitmap);
  64. const gfx::Size size(bitmap.width(), bitmap.height());
  65. const uint32_t num_bytes = size.GetArea() * 4 /* bytes per pixel */;
  66. ASSERT_EQ(num_bytes, bitmap.computeByteSize());
  67. base::RunLoop run_loop;
  68. std::vector<mojom::TextDetectionResultPtr> results;
  69. text_service->Detect(
  70. bitmap,
  71. base::BindOnce(&DetectTextCallback, run_loop.QuitClosure(), &results));
  72. run_loop.Run();
  73. ASSERT_EQ(2u, results.size());
  74. EXPECT_EQ("The Chromium Project website is:", results[0]->raw_value);
  75. EXPECT_EQ(gfx::RectF(51, 38, 272, 17), results[0]->bounding_box);
  76. EXPECT_EQ("https://www.chromium.org", results[1]->raw_value);
  77. EXPECT_EQ(gfx::RectF(51, 63, 209, 17), results[1]->bounding_box);
  78. }
  79. } // namespace shape_detection