OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 <string> |
| 6 #include <vector> |
| 7 |
| 8 #include "app/app_switches.h" |
| 9 #include "app/gfx/gl/gl_implementation.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/file_path.h" |
| 12 #include "base/file_util.h" |
| 13 #include "base/path_service.h" |
| 14 #include "base/string_util.h" |
| 15 #include "base/stringprintf.h" |
| 16 #include "chrome/browser/browser.h" |
| 17 #include "chrome/browser/browser_window.h" |
| 18 #include "chrome/browser/gpu_process_host.h" |
| 19 #include "chrome/browser/renderer_host/render_view_host.h" |
| 20 #include "chrome/browser/tab_contents/tab_contents.h" |
| 21 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/common/gpu_info.h" |
| 23 #include "chrome/test/in_process_browser_test.h" |
| 24 #include "chrome/test/ui_test_utils.h" |
| 25 #include "gfx/codec/png_codec.h" |
| 26 #include "gfx/size.h" |
| 27 #include "googleurl/src/gurl.h" |
| 28 #include "net/base/net_util.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #include "third_party/skia/include/core/SkBitmap.h" |
| 31 #include "third_party/skia/include/core/SkColor.h" |
| 32 |
| 33 namespace { |
| 34 |
| 35 // Command line flag for forcing the machine's GPU to be used instead of OSMesa. |
| 36 const char kUseGpuInTests[] = "use-gpu-in-tests"; |
| 37 |
| 38 // Reads and decodes a PNG image to a bitmap. Returns true on success. The PNG |
| 39 // should have been encoded using |gfx::PNGCodec::Encode|. |
| 40 bool ReadPNGFile(const FilePath& file_path, SkBitmap* bitmap) { |
| 41 DCHECK(bitmap); |
| 42 std::string png_data; |
| 43 return file_util::ReadFileToString(file_path, &png_data) && |
| 44 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), |
| 45 png_data.length(), |
| 46 bitmap); |
| 47 } |
| 48 |
| 49 // Encodes a bitmap into a PNG and write to disk. Returns true on success. The |
| 50 // parent directory does not have to exist. |
| 51 bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path) { |
| 52 std::vector<unsigned char> png_data; |
| 53 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data) && |
| 54 file_util::CreateDirectory(file_path.DirName())) { |
| 55 int bytes_written = file_util::WriteFile( |
| 56 file_path, reinterpret_cast<char*>(&png_data[0]), png_data.size()); |
| 57 if (bytes_written == static_cast<int>(png_data.size())) |
| 58 return true; |
| 59 } |
| 60 return false; |
| 61 } |
| 62 |
| 63 // Resizes the browser window so that the tab's contents are at a given size. |
| 64 void ResizeTabContainer(Browser* browser, const gfx::Size& desired_size) { |
| 65 gfx::Rect container_rect; |
| 66 browser->GetSelectedTabContents()->GetContainerBounds(&container_rect); |
| 67 // Size cannot be negative, so use a point. |
| 68 gfx::Point correction(desired_size.width() - container_rect.size().width(), |
| 69 desired_size.height() - container_rect.size().height()); |
| 70 |
| 71 gfx::Rect window_rect = browser->window()->GetRestoredBounds(); |
| 72 gfx::Size new_size = window_rect.size(); |
| 73 new_size.Enlarge(correction.x(), correction.y()); |
| 74 window_rect.set_size(new_size); |
| 75 browser->window()->SetBounds(window_rect); |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 |
| 80 // Test fixture for GPU image comparison tests. |
| 81 // TODO(kkania): Document how to add to/modify these tests. |
| 82 class GpuPixelBrowserTest : public InProcessBrowserTest { |
| 83 public: |
| 84 GpuPixelBrowserTest() {} |
| 85 |
| 86 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 87 // This enables DOM automation for tab contentses. |
| 88 EnableDOMAutomation(); |
| 89 |
| 90 // All tests by default use OSMesa. This can be changed if the |kUseGL| |
| 91 // switch is explicitly set to something else or if |kUseGpuInTests| is |
| 92 // present. |
| 93 if (command_line->HasSwitch(switches::kUseGL)) { |
| 94 using_gpu_ = command_line->GetSwitchValueASCII(switches::kUseGL) != |
| 95 gfx::kGLImplementationOSMesaName; |
| 96 } else if (command_line->HasSwitch(kUseGpuInTests)) { |
| 97 #if defined(OS_WIN) |
| 98 command_line->AppendSwitchASCII( |
| 99 switches::kUseGL, gfx::kGLImplementationEGLName); |
| 100 #else |
| 101 command_line->AppendSwitchASCII( |
| 102 switches::kUseGL, gfx::kGLImplementationDesktopName); |
| 103 #endif |
| 104 using_gpu_ = true; |
| 105 } else { |
| 106 // OSMesa will be used by default. |
| 107 using_gpu_ = false; |
| 108 } |
| 109 } |
| 110 |
| 111 virtual void SetUpInProcessBrowserTestFixture() { |
| 112 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); |
| 113 test_data_dir_ = test_data_dir_.AppendASCII("gpu"); |
| 114 generated_img_dir_ = test_data_dir_.AppendASCII("generated"); |
| 115 if (using_gpu_) |
| 116 reference_img_dir_ = test_data_dir_.AppendASCII("gpu_reference"); |
| 117 else |
| 118 reference_img_dir_ = test_data_dir_.AppendASCII("sw_reference"); |
| 119 |
| 120 test_name_ = testing::UnitTest::GetInstance()->current_test_info()->name(); |
| 121 const char* test_status_prefixes[] = {"DISABLED_", "FLAKY_", "FAILS_"}; |
| 122 for (size_t i = 0; i < arraysize(test_status_prefixes); ++i) { |
| 123 ReplaceFirstSubstringAfterOffset( |
| 124 &test_name_, 0, test_status_prefixes[i], ""); |
| 125 } |
| 126 } |
| 127 |
| 128 // Compares the generated bitmap with the appropriate reference image on disk. |
| 129 // The reference image is determined by the name of the test, the |postfix|, |
| 130 // and the vendor and device ID. Returns true iff the images were the same. |
| 131 // |
| 132 // |postfix|, is an optional name that can be appended onto the images name to |
| 133 // distinguish between images from a single test. |
| 134 // |
| 135 // On failure, the image will be written to disk, and can be collected as a |
| 136 // baseline. The image format is: |
| 137 // <test_name>_<postfix>_<os>_<vendor_id>-<device_id>.png |
| 138 // E.g., |
| 139 // WebGLFishTank_FirstTab_120981-1201.png |
| 140 bool CompareImages(const SkBitmap& gen_bmp, const std::string& postfix) { |
| 141 // Determine the name of the image. |
| 142 std::string img_name = test_name_; |
| 143 if (postfix.length()) |
| 144 img_name += "_" + postfix; |
| 145 #if defined(OS_WIN) |
| 146 const char* os_label = "win"; |
| 147 #elif defined(OS_MACOSX) |
| 148 const char* os_label = "mac"; |
| 149 #elif defined(OS_LINUX) |
| 150 const char* os_label = "linux"; |
| 151 #else |
| 152 #error "Not implemented for this platform" |
| 153 #endif |
| 154 if (using_gpu_) { |
| 155 GPUInfo info = GpuProcessHost::Get()->gpu_info(); |
| 156 if (!info.initialized()) { |
| 157 LOG(ERROR) << "Could not get gpu info"; |
| 158 return false; |
| 159 } |
| 160 img_name = StringPrintf("%s_%s_%d-%d.png", |
| 161 img_name.c_str(), os_label, info.vendor_id(), info.device_id()); |
| 162 } else { |
| 163 img_name = StringPrintf("%s_%s_mesa.png", img_name.c_str(), os_label); |
| 164 } |
| 165 |
| 166 // Read the reference image and verify the images' dimensions are equal. |
| 167 FilePath ref_img_path = reference_img_dir_.AppendASCII(img_name); |
| 168 SkBitmap ref_bmp; |
| 169 bool should_compare = true; |
| 170 if (!ReadPNGFile(ref_img_path, &ref_bmp)) { |
| 171 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value(); |
| 172 should_compare = false; |
| 173 } |
| 174 if (should_compare && |
| 175 (ref_bmp.width() != gen_bmp.width() || |
| 176 ref_bmp.height() != gen_bmp.height())) { |
| 177 LOG(ERROR) |
| 178 << "Dimensions do not match (Expected) vs (Actual):" |
| 179 << "(" << ref_bmp.width() << "x" << ref_bmp.height() |
| 180 << ") vs. " |
| 181 << "(" << gen_bmp.width() << "x" << gen_bmp.height() << ")"; |
| 182 should_compare = false; |
| 183 } |
| 184 |
| 185 // Compare pixels and create a simple diff image. |
| 186 int diff_pixels_count = 0; |
| 187 SkBitmap diff_bmp; |
| 188 if (should_compare) { |
| 189 diff_bmp.setConfig(SkBitmap::kARGB_8888_Config, |
| 190 gen_bmp.width(), gen_bmp.height()); |
| 191 diff_bmp.allocPixels(); |
| 192 diff_bmp.eraseColor(SK_ColorWHITE); |
| 193 SkAutoLockPixels lock_bmp(gen_bmp); |
| 194 SkAutoLockPixels lock_ref_bmp(ref_bmp); |
| 195 SkAutoLockPixels lock_diff_bmp(diff_bmp); |
| 196 // The reference images were saved with no alpha channel. Use the mask to |
| 197 // set alpha to 0. |
| 198 uint32_t kAlphaMask = 0x00FFFFFF; |
| 199 for (int x = 0; x < gen_bmp.width(); ++x) { |
| 200 for (int y = 0; y < gen_bmp.height(); ++y) { |
| 201 if ((*gen_bmp.getAddr32(x, y) & kAlphaMask) != |
| 202 (*ref_bmp.getAddr32(x, y) & kAlphaMask)) { |
| 203 ++diff_pixels_count; |
| 204 *diff_bmp.getAddr32(x, y) = 192 << 16; // red |
| 205 } |
| 206 } |
| 207 } |
| 208 } |
| 209 |
| 210 // Write the generated and diff images if the comparison failed. |
| 211 if (!should_compare || diff_pixels_count != 0) { |
| 212 FilePath gen_img_path = generated_img_dir_.AppendASCII(img_name); |
| 213 if (WritePNGFile(gen_bmp, gen_img_path)) { |
| 214 // Output the generated image path to the log for easy parsing later. |
| 215 std::cout << "*GEN_IMG=" << gen_img_path.value() << std::endl; |
| 216 } else { |
| 217 LOG(WARNING) << "Could not write generated image: " |
| 218 << gen_img_path.value(); |
| 219 } |
| 220 if (should_compare) { |
| 221 WritePNGFile(diff_bmp, |
| 222 generated_img_dir_.AppendASCII("diff-" + img_name)); |
| 223 LOG(ERROR) << "Images differ by pixel count: " << diff_pixels_count; |
| 224 } |
| 225 return false; |
| 226 } |
| 227 return true; |
| 228 } |
| 229 |
| 230 protected: |
| 231 FilePath test_data_dir_; |
| 232 |
| 233 private: |
| 234 FilePath reference_img_dir_; |
| 235 FilePath generated_img_dir_; |
| 236 // The name of the test, with any special prefixes dropped. |
| 237 std::string test_name_; |
| 238 // Whether the gpu, or OSMesa is being used for rendering. |
| 239 bool using_gpu_; |
| 240 |
| 241 DISALLOW_COPY_AND_ASSIGN(GpuPixelBrowserTest); |
| 242 }; |
| 243 |
| 244 // This test does not run on Linux because the information about the gfx card is |
| 245 // not available yet. It does not run on Windows because the thumbnail generator |
| 246 // is currently broken on that platform. |
| 247 // TODO(kkania): Fix thumbnail generator. |
| 248 #if defined(OS_MACOSX) |
| 249 // Mark this as failing because we don't have the reference images yet. Once the |
| 250 // images are generated on the bots, they will be uploaded and this status will |
| 251 // be changed. |
| 252 #define MAYBE_WebGLTeapot FAILS_WebGLTeapot |
| 253 #else |
| 254 #define MAYBE_WebGLTeapot DISABLED_WebGLTeapot |
| 255 #endif |
| 256 IN_PROC_BROWSER_TEST_F(GpuPixelBrowserTest, MAYBE_WebGLTeapot) { |
| 257 ui_test_utils::DOMMessageQueue message_queue; |
| 258 ui_test_utils::NavigateToURL( |
| 259 browser(), |
| 260 net::FilePathToFileURL(test_data_dir_.AppendASCII("webgl_teapot"). |
| 261 AppendASCII("teapot.html"))); |
| 262 |
| 263 // Wait for message from teapot page indicating the GL calls have been issued. |
| 264 ASSERT_TRUE(message_queue.WaitForMessage(NULL)); |
| 265 |
| 266 SkBitmap bitmap; |
| 267 gfx::Size container_size(500, 500); |
| 268 ResizeTabContainer(browser(), container_size); |
| 269 ASSERT_TRUE(ui_test_utils::TakeSnapshot( |
| 270 browser()->GetSelectedTabContents()->render_view_host(), |
| 271 container_size, &bitmap)); |
| 272 ASSERT_TRUE(CompareImages(bitmap, "")); |
| 273 } |
OLD | NEW |