snapshot_mac_unittest.mm 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) 2012 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. #include "ui/snapshot/snapshot.h"
  5. #import <Cocoa/Cocoa.h>
  6. #include <memory>
  7. #include "base/mac/scoped_nsobject.h"
  8. #include "testing/platform_test.h"
  9. #import "ui/base/test/cocoa_helper.h"
  10. #include "ui/gfx/geometry/rect.h"
  11. #include "ui/gfx/image/image.h"
  12. namespace ui {
  13. namespace {
  14. typedef CocoaTest GrabWindowSnapshotTest;
  15. // TODO(https://crbug.com/685088): This test fails.
  16. TEST_F(GrabWindowSnapshotTest, DISABLED_TestGrabWindowSnapshot) {
  17. // Launch a test window so we can take a snapshot.
  18. NSRect frame = NSMakeRect(0, 0, 400, 400);
  19. NSWindow* window = test_window();
  20. [window setFrame:frame display:false];
  21. [window setBackgroundColor:[NSColor whiteColor]];
  22. [window makeKeyAndOrderFront:NSApp];
  23. [window display];
  24. gfx::Image image;
  25. gfx::Rect bounds = gfx::Rect(0, 0, frame.size.width, frame.size.height);
  26. EXPECT_TRUE(ui::GrabWindowSnapshot(window, bounds, &image));
  27. NSImage* nsImage = image.ToNSImage();
  28. CGImageRef cgImage =
  29. [nsImage CGImageForProposedRect:nil context:nil hints:nil];
  30. base::scoped_nsobject<NSBitmapImageRep> rep(
  31. [[NSBitmapImageRep alloc] initWithCGImage:cgImage]);
  32. EXPECT_TRUE([rep isKindOfClass:[NSBitmapImageRep class]]);
  33. CGFloat scaleFactor = [window backingScaleFactor];
  34. EXPECT_EQ(400 * scaleFactor, CGImageGetWidth([rep CGImage]));
  35. NSColor* color = [rep colorAtX:200 * scaleFactor y:200 * scaleFactor];
  36. CGFloat red = 0, green = 0, blue = 0, alpha = 0;
  37. [color getRed:&red green:&green blue:&blue alpha:&alpha];
  38. EXPECT_GE(red + green + blue, 3.0);
  39. }
  40. } // namespace
  41. } // namespace ui