Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / login / users / avatar / user_image_manager_test_util.cc
blob2275e464503cb173f28660f45aaf6c93e2c3ed3b
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.
5 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager_test_util.h"
7 #include <string>
9 #include "base/files/file_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/gfx/image/image_skia.h"
14 #include "ui/gfx/image/image_skia_rep.h"
16 namespace chromeos {
17 namespace test {
19 const char kUserAvatarImage1RelativePath[] = "chromeos/avatar1.jpg";
20 const char kUserAvatarImage2RelativePath[] = "chromeos/avatar2.jpg";
22 bool AreImagesEqual(const gfx::ImageSkia& first, const gfx::ImageSkia& second) {
23 if (first.width() != second.width() || first.height() != second.height())
24 return false;
25 const SkBitmap* first_bitmap = first.bitmap();
26 const SkBitmap* second_bitmap = second.bitmap();
27 if (!first_bitmap && !second_bitmap)
28 return true;
29 if (!first_bitmap || !second_bitmap)
30 return false;
32 const size_t size = first_bitmap->getSize();
33 if (second_bitmap->getSize() != size)
34 return false;
36 SkAutoLockPixels first_pixel_lock(*first_bitmap);
37 SkAutoLockPixels second_pixel_lock(*second_bitmap);
38 uint8_t* first_data = reinterpret_cast<uint8_t*>(first_bitmap->getPixels());
39 uint8_t* second_data = reinterpret_cast<uint8_t*>(second_bitmap->getPixels());
40 for (size_t i = 0; i < size; ++i) {
41 if (first_data[i] != second_data[i])
42 return false;
44 return true;
47 ImageLoader::ImageLoader(const base::FilePath& path) : path_(path) {
50 ImageLoader::~ImageLoader() {
53 scoped_ptr<gfx::ImageSkia> ImageLoader::Load() {
54 std::string image_data;
55 ReadFileToString(path_, &image_data);
56 ImageDecoder::StartWithOptions(this, image_data,
57 ImageDecoder::ROBUST_JPEG_CODEC, false);
58 run_loop_.Run();
59 return decoded_image_.Pass();
62 void ImageLoader::OnImageDecoded(const SkBitmap& decoded_image) {
63 decoded_image_.reset(
64 new gfx::ImageSkia(gfx::ImageSkiaRep(decoded_image, 1.0f)));
65 run_loop_.Quit();
68 void ImageLoader::OnDecodeImageFailed() {
69 run_loop_.Quit();
72 } // namespace test
73 } // namespace chromeos