OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "ui/gfx/compositor/test/test_compositor_host.h" |
| 6 |
| 7 #import <AppKit/NSApplication.h> |
| 8 #import <AppKit/NSOpenGL.h> |
| 9 #import <AppKit/NSView.h> |
| 10 #import <AppKit/NSWindow.h> |
| 11 #import <Foundation/NSAutoreleasePool.h> |
| 12 |
| 13 #include "base/compiler_specific.h" |
| 14 #include "ui/gfx/compositor/compositor.h" |
| 15 #include "ui/gfx/rect.h" |
| 16 |
| 17 // AcceleratedTestView provides an NSView class that delegates drawing to a |
| 18 // ui::Compositor delegate, setting up the NSOpenGLContext as required. |
| 19 @interface AcceleratedTestView : NSView { |
| 20 scoped_refptr<ui::Compositor> compositor_; |
| 21 } |
| 22 // Designated initializer. |
| 23 -(id)init; |
| 24 -(void)setCompositor:(scoped_refptr<ui::Compositor>)compositor; |
| 25 @end |
| 26 |
| 27 @implementation AcceleratedTestView |
| 28 -(id)init { |
| 29 // The frame will be resized when reparented into the window's view hierarchy. |
| 30 self = [super initWithFrame:NSZeroRect]; |
| 31 return self; |
| 32 } |
| 33 |
| 34 -(void)setCompositor:(scoped_refptr<ui::Compositor>)compositor { |
| 35 compositor_ = compositor; |
| 36 } |
| 37 |
| 38 - (void)drawRect:(NSRect)rect { |
| 39 DCHECK(compositor_) << "Drawing with no compositor set."; |
| 40 compositor_->Draw(false); |
| 41 } |
| 42 @end |
| 43 |
| 44 namespace ui { |
| 45 |
| 46 // Tests that use Objective-C memory semantics need to have a top-level |
| 47 // NSAutoreleasePool set up and initialized prior to execution and drained upon |
| 48 // exit. The tests will leak otherwise. |
| 49 class FoundationHost { |
| 50 protected: |
| 51 FoundationHost() { |
| 52 pool_ = [[NSAutoreleasePool alloc] init]; |
| 53 } |
| 54 virtual ~FoundationHost() { |
| 55 [pool_ drain]; |
| 56 } |
| 57 |
| 58 private: |
| 59 NSAutoreleasePool* pool_; |
| 60 DISALLOW_COPY_AND_ASSIGN(FoundationHost); |
| 61 }; |
| 62 |
| 63 // Tests that use the AppKit framework need to have the NSApplication |
| 64 // initialized prior to doing anything with display objects such as windows, |
| 65 // views, or controls. |
| 66 class AppKitHost : public FoundationHost { |
| 67 protected: |
| 68 AppKitHost() { |
| 69 [NSApplication sharedApplication]; |
| 70 } |
| 71 virtual ~AppKitHost() { |
| 72 } |
| 73 private: |
| 74 DISALLOW_COPY_AND_ASSIGN(AppKitHost); |
| 75 }; |
| 76 |
| 77 // TestCompositorHostMac provides a window surface and a coordinated compositor |
| 78 // for use in the compositor unit tests. |
| 79 class TestCompositorHostMac : public TestCompositorHost, |
| 80 public CompositorDelegate, |
| 81 public AppKitHost { |
| 82 public: |
| 83 TestCompositorHostMac(const gfx::Rect& bounds); |
| 84 virtual ~TestCompositorHostMac(); |
| 85 |
| 86 private: |
| 87 // TestCompositorHost: |
| 88 virtual void Show() OVERRIDE; |
| 89 virtual ui::Compositor* GetCompositor() OVERRIDE; |
| 90 |
| 91 // CompositorDelegate: |
| 92 virtual void ScheduleDraw() OVERRIDE; |
| 93 |
| 94 gfx::Rect bounds_; |
| 95 scoped_refptr<ui::Compositor> compositor_; |
| 96 |
| 97 // Owned. Released when window is closed. |
| 98 NSWindow* window_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(TestCompositorHostMac); |
| 101 }; |
| 102 |
| 103 TestCompositorHostMac::TestCompositorHostMac(const gfx::Rect& bounds) |
| 104 : bounds_(bounds), window_(nil) { |
| 105 } |
| 106 |
| 107 TestCompositorHostMac::~TestCompositorHostMac() { |
| 108 [window_ orderOut:nil]; |
| 109 [window_ close]; |
| 110 } |
| 111 |
| 112 void TestCompositorHostMac::Show() { |
| 113 DCHECK(!window_); |
| 114 window_ = [[NSWindow alloc] |
| 115 initWithContentRect:NSMakeRect(bounds_.x(), |
| 116 bounds_.y(), |
| 117 bounds_.width(), |
| 118 bounds_.height()) |
| 119 styleMask:NSBorderlessWindowMask |
| 120 backing:NSBackingStoreBuffered |
| 121 defer:NO]; |
| 122 AcceleratedTestView* view = [[[AcceleratedTestView alloc] init] autorelease]; |
| 123 compositor_ = ui::Compositor::Create(this, view, bounds_.size()); |
| 124 [view setCompositor:compositor_]; |
| 125 [window_ setContentView:view]; |
| 126 [window_ orderFront:nil]; |
| 127 } |
| 128 |
| 129 ui::Compositor* TestCompositorHostMac::GetCompositor() { |
| 130 return compositor_; |
| 131 } |
| 132 |
| 133 void TestCompositorHostMac::ScheduleDraw() { |
| 134 if (!compositor_) |
| 135 return; |
| 136 |
| 137 // Force display now. |
| 138 [window_ display]; |
| 139 } |
| 140 |
| 141 // static |
| 142 TestCompositorHost* TestCompositorHost::Create(const gfx::Rect& bounds) { |
| 143 return new TestCompositorHostMac(bounds); |
| 144 } |
| 145 |
| 146 } // namespace ui |
OLD | NEW |