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_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 |
| 41 NSOpenGLContext* context = [NSOpenGLContext currentContext]; |
| 42 [context setView:self]; |
| 43 compositor_->Draw(false); |
| 44 } |
| 45 @end |
| 46 |
| 47 namespace ui { |
| 48 |
| 49 // Tests that use Objective-C memory semantics need to have a top-level |
| 50 // NSAutoreleasePool set up and initialized prior to execution and drained upon |
| 51 // exit. The tests will leak otherwise. |
| 52 class FoundationHost { |
| 53 protected: |
| 54 FoundationHost() { |
| 55 pool_ = [[NSAutoreleasePool alloc] init]; |
| 56 } |
| 57 virtual ~FoundationHost() { |
| 58 [pool_ drain]; |
| 59 } |
| 60 |
| 61 private: |
| 62 NSAutoreleasePool* pool_; |
| 63 DISALLOW_COPY_AND_ASSIGN(FoundationHost); |
| 64 }; |
| 65 |
| 66 // Tests that use the AppKit framework need to have the NSApplication |
| 67 // initialized prior to doing anything with display objects such as windows, |
| 68 // views, or controls. |
| 69 class AppKitHost : public FoundationHost { |
| 70 protected: |
| 71 AppKitHost() { |
| 72 [NSApplication sharedApplication]; |
| 73 } |
| 74 virtual ~AppKitHost() { |
| 75 } |
| 76 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(AppKitHost); |
| 78 }; |
| 79 |
| 80 // TestCompositorHostMac provides a window surface and a coordinated compositor |
| 81 // for use in the compositor unit tests. |
| 82 class TestCompositorHostMac : public TestCompositorHost, |
| 83 public CompositorDelegate, |
| 84 public AppKitHost { |
| 85 public: |
| 86 TestCompositorHostMac(const gfx::Rect& bounds); |
| 87 virtual ~TestCompositorHostMac(); |
| 88 |
| 89 private: |
| 90 // TestCompositorHost: |
| 91 virtual void Show() OVERRIDE; |
| 92 virtual ui::Compositor* GetCompositor() OVERRIDE; |
| 93 |
| 94 // CompositorDelegate: |
| 95 virtual void ScheduleDraw() OVERRIDE; |
| 96 |
| 97 gfx::Rect bounds_; |
| 98 scoped_refptr<ui::Compositor> compositor_; |
| 99 |
| 100 // Owned. Released when window is closed. |
| 101 NSWindow* window_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(TestCompositorHostMac); |
| 104 }; |
| 105 |
| 106 TestCompositorHostMac::TestCompositorHostMac(const gfx::Rect& bounds) |
| 107 : bounds_(bounds), window_(nil) { |
| 108 } |
| 109 |
| 110 TestCompositorHostMac::~TestCompositorHostMac() { |
| 111 [window_ orderOut:nil]; |
| 112 [window_ close]; |
| 113 } |
| 114 |
| 115 void TestCompositorHostMac::Show() { |
| 116 DCHECK(!window_); |
| 117 window_ = [[NSWindow alloc] |
| 118 initWithContentRect:NSMakeRect(bounds_.x(), |
| 119 bounds_.y(), |
| 120 bounds_.width(), |
| 121 bounds_.height()) |
| 122 styleMask:NSBorderlessWindowMask |
| 123 backing:NSBackingStoreBuffered |
| 124 defer:NO]; |
| 125 AcceleratedTestView* view = [[[AcceleratedTestView alloc] init] autorelease]; |
| 126 compositor_ = ui::Compositor::Create(this, view, bounds_.size()); |
| 127 [view setCompositor:compositor_]; |
| 128 [window_ setContentView:view]; |
| 129 [window_ orderFront:nil]; |
| 130 } |
| 131 |
| 132 ui::Compositor* TestCompositorHostMac::GetCompositor() { |
| 133 return compositor_; |
| 134 } |
| 135 |
| 136 void TestCompositorHostMac::ScheduleDraw() { |
| 137 if (!compositor_) |
| 138 return; |
| 139 |
| 140 // Force display now. |
| 141 [window_ display]; |
| 142 } |
| 143 |
| 144 // static |
| 145 TestCompositorHost* TestCompositorHost::Create(const gfx::Rect& bounds) { |
| 146 return new TestCompositorHostMac(bounds); |
| 147 } |
| 148 |
| 149 } // namespace ui |
OLD | NEW |