1// Copyright 2014 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/bind.h"
6#include "base/files/file_path.h"
7#include "base/path_service.h"
8#include "base/run_loop.h"
9#include "chrome/common/chrome_paths.h"
10#include "chrome/utility/media_galleries/image_metadata_extractor.h"
11#include "media/filters/file_data_source.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace metadata {
15
16void QuitLoop(base::RunLoop* loop, bool* output, bool success) {
17  loop->Quit();
18  *output = success;
19}
20
21base::FilePath GetTestDataFilePath(const std::string& filename) {
22  base::FilePath path;
23  EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
24  path = path.AppendASCII("extensions").AppendASCII("api_test")
25      .AppendASCII("wallpaper").AppendASCII(filename);
26  return path;
27}
28
29scoped_ptr<ImageMetadataExtractor> GetExtractor(
30    const std::string& filename,
31    bool expected_result) {
32  EXPECT_TRUE(ImageMetadataExtractor::InitializeLibraryForTesting());
33
34  media::FileDataSource source;
35  base::FilePath test_path;
36
37  EXPECT_TRUE(source.Initialize(GetTestDataFilePath(filename)));
38
39  scoped_ptr<ImageMetadataExtractor> extractor(new ImageMetadataExtractor);
40
41  base::RunLoop loop;
42  bool extracted = false;
43  extractor->Extract(&source, base::Bind(&QuitLoop, &loop, &extracted));
44  EXPECT_EQ(expected_result, extracted);
45
46  return extractor.Pass();
47}
48
49TEST(ImageMetadataExtractorTest, JPGFile) {
50  scoped_ptr<ImageMetadataExtractor> extractor =
51      GetExtractor("test.jpg", true);
52
53  EXPECT_EQ(5616, extractor->width());
54  EXPECT_EQ(3744, extractor->height());
55  EXPECT_EQ(0, extractor->rotation());
56  EXPECT_EQ(300.0, extractor->x_resolution());
57  EXPECT_EQ(300.0, extractor->y_resolution());
58  EXPECT_EQ("2012:03:01 17:06:07", extractor->date());
59  EXPECT_EQ("Canon", extractor->camera_make());
60  EXPECT_EQ("Canon EOS 5D Mark II", extractor->camera_model());
61  EXPECT_EQ(0.01, extractor->exposure_time_sec());
62  EXPECT_FALSE(extractor->flash_fired());
63  EXPECT_EQ(3.2, extractor->f_number());
64  EXPECT_EQ(100, extractor->focal_length_mm());
65  EXPECT_EQ(1600, extractor->iso_equivalent());
66}
67
68TEST(ImageMetadataExtractorTest, PNGFile) {
69  GetExtractor("test.png", false);
70}
71
72TEST(ImageMetadataExtractorTest, NonImageFile) {
73  GetExtractor("test.js", false);
74}
75
76}  // namespace metadata
77