Home | History | Annotate | Download | only in avatar
      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 <string>
      8 
      9 #include "base/files/file_util.h"
     10 #include "base/memory/ref_counted.h"
     11 #include "base/message_loop/message_loop_proxy.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"
     15 
     16 namespace chromeos {
     17 namespace test {
     18 
     19 const char kUserAvatarImage1RelativePath[] = "chromeos/avatar1.jpg";
     20 const char kUserAvatarImage2RelativePath[] = "chromeos/avatar2.jpg";
     21 
     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;
     31 
     32   const size_t size = first_bitmap->getSize();
     33   if (second_bitmap->getSize() != size)
     34     return false;
     35 
     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;
     43   }
     44   return true;
     45 }
     46 
     47 ImageLoader::ImageLoader(const base::FilePath& path) : path_(path) {
     48 }
     49 
     50 ImageLoader::~ImageLoader() {
     51 }
     52 
     53 scoped_ptr<gfx::ImageSkia> ImageLoader::Load() {
     54   std::string image_data;
     55   ReadFileToString(path_, &image_data);
     56   scoped_refptr<ImageDecoder> image_decoder = new ImageDecoder(
     57       this,
     58       image_data,
     59       ImageDecoder::ROBUST_JPEG_CODEC);
     60   image_decoder->Start(base::MessageLoopProxy::current());
     61   run_loop_.Run();
     62   return decoded_image_.Pass();
     63 }
     64 
     65 void ImageLoader::OnImageDecoded(const ImageDecoder* decoder,
     66                                  const SkBitmap& decoded_image) {
     67   decoded_image_.reset(
     68       new gfx::ImageSkia(gfx::ImageSkiaRep(decoded_image, 1.0f)));
     69   run_loop_.Quit();
     70 }
     71 
     72 void ImageLoader::OnDecodeImageFailed(const ImageDecoder* decoder) {
     73   run_loop_.Quit();
     74 }
     75 
     76 }  // namespace test
     77 }  // namespace chromeos
     78