Home | History | Annotate | Download | only in login
      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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SCREENSHOT_TESTER_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SCREENSHOT_TESTER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/base_export.h"
     11 #include "base/bind_internal.h"
     12 #include "base/files/file_path.h"
     13 #include "base/macros.h"
     14 #include "base/memory/ref_counted_memory.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "base/run_loop.h"
     17 
     18 namespace chromeos {
     19 
     20 // A class that allows taking, saving and comparing screnshots while
     21 // running tests.
     22 class ScreenshotTester {
     23  public:
     24   ScreenshotTester();
     25   virtual ~ScreenshotTester();
     26 
     27   // Returns true if the screenshots should be taken and will be taken,
     28   // false otherwise. Also gets all the information from the command line
     29   // swithes.
     30   bool TryInitialize();
     31 
     32   // Does all the work that has been stated through switches:
     33   // updates golden screenshot or takes a new screenshot and compares it
     34   // with the golden one. |test_name| is the name of the test from which
     35   // we run this method.
     36   void Run(const std::string& test_name);
     37 
     38  private:
     39   typedef scoped_refptr<base::RefCountedBytes> PNGFile;
     40 
     41   // Takes a screenshot and returns it.
     42   PNGFile TakeScreenshot();
     43 
     44   // Saves |png_data| as a new golden screenshot for test |test_name_|.
     45   void UpdateGoldenScreenshot(PNGFile png_data);
     46 
     47   // Saves an image |png_data|, assuming it is a .png file.
     48   // Returns true if image was saved successfully.
     49   bool SaveImage(const std::string& file_name,
     50                  const base::FilePath& screenshot_dir,
     51                  PNGFile png_data);
     52 
     53   // Saves |png_data| as a current screenshot.
     54   void ReturnScreenshot(const PNGFile& screenshot, PNGFile png_data);
     55 
     56   // Loads golden screenshot from the disk. Fails if there is no
     57   // golden screenshot for test |test_name_|.
     58   PNGFile LoadGoldenScreenshot();
     59 
     60   // Compares two given screenshots and saves |sample|
     61   // and difference between |sample| and |model|, if they differ in any pixel.
     62   void CompareScreenshots(PNGFile model, PNGFile sample);
     63 
     64   // Name of the test from which Run() method has been called.
     65   // Used for generating names for screenshot files.
     66   std::string test_name_;
     67 
     68   // Path to the directory for golden screenshots.
     69   base::FilePath golden_screenshots_dir_;
     70 
     71   // Path to the directory where screenshots that failed comparing
     72   // and difference between them and golden ones will be stored.
     73   base::FilePath artifacts_dir_;
     74 
     75   // |run_loop_| and |run_loop_quitter_| are used to synchronize
     76   // with ui::GrabWindowSnapshotAsync.
     77   base::RunLoop run_loop_;
     78   base::Closure run_loop_quitter_;
     79 
     80   // Is true when we're in test mode:
     81   // comparing golden screenshots and current ones.
     82   bool test_mode_;
     83 
     84   base::WeakPtrFactory<ScreenshotTester> weak_factory_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(ScreenshotTester);
     87 };
     88 
     89 }  // namespace chromeos
     90 
     91 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_SCREENSHOT_TESTER_H_
     92