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 "chrome/browser/chromeos/login/users/avatar/user_image_manager_test_util.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <string>
10 #include <utility>
11 
12 #include "base/files/file_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/gfx/image/image_skia_rep.h"
18 
19 namespace chromeos {
20 namespace test {
21 
22 const char kUserAvatarImage1RelativePath[] = "chromeos/avatar1.jpg";
23 const char kUserAvatarImage2RelativePath[] = "chromeos/avatar2.jpg";
24 const char kUserAvatarImage3RelativePath[] = "chromeos/avatar3.png";
25 
AreImagesEqual(const gfx::ImageSkia & first,const gfx::ImageSkia & second)26 bool AreImagesEqual(const gfx::ImageSkia& first, const gfx::ImageSkia& second) {
27   if (first.width() != second.width() || first.height() != second.height())
28     return false;
29   const SkBitmap* first_bitmap = first.bitmap();
30   const SkBitmap* second_bitmap = second.bitmap();
31   if (!first_bitmap && !second_bitmap)
32     return true;
33   if (!first_bitmap || !second_bitmap)
34     return false;
35 
36   const size_t size = first_bitmap->computeByteSize();
37   if (second_bitmap->computeByteSize() != size)
38     return false;
39 
40   uint8_t* first_data = reinterpret_cast<uint8_t*>(first_bitmap->getPixels());
41   uint8_t* second_data = reinterpret_cast<uint8_t*>(second_bitmap->getPixels());
42   for (size_t i = 0; i < size; ++i) {
43     if (first_data[i] != second_data[i])
44       return false;
45   }
46   return true;
47 }
48 
ImageLoader(const base::FilePath & path)49 ImageLoader::ImageLoader(const base::FilePath& path) : path_(path) {}
50 
~ImageLoader()51 ImageLoader::~ImageLoader() {}
52 
Load()53 gfx::ImageSkia ImageLoader::Load() {
54   std::string image_data;
55   {
56     base::ScopedAllowBlockingForTesting allow_io;
57     ReadFileToString(path_, &image_data);
58   }
59   const ImageDecoder::ImageCodec codec =
60       (path_.Extension() == FILE_PATH_LITERAL(".jpg")
61        ? ImageDecoder::DEFAULT_CODEC
62        : ImageDecoder::ROBUST_PNG_CODEC);
63   ImageDecoder::StartWithOptions(this, image_data, codec, false);
64   run_loop_.Run();
65   return decoded_image_;
66 }
67 
OnImageDecoded(const SkBitmap & decoded_image)68 void ImageLoader::OnImageDecoded(const SkBitmap& decoded_image) {
69   decoded_image_ = gfx::ImageSkia(gfx::ImageSkiaRep(decoded_image, 1.0f));
70   run_loop_.Quit();
71 }
72 
OnDecodeImageFailed()73 void ImageLoader::OnDecodeImageFailed() {
74   decoded_image_ = gfx::ImageSkia();
75   run_loop_.Quit();
76 }
77 
78 }  // namespace test
79 }  // namespace chromeos
80