Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / compositor / test / test_compositor_host_mac.mm
blob5c212cd25b891f6681db5c38f5407d462422df23
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.
5 #include "ui/compositor/test/test_compositor_host.h"
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>
13 #include "base/compiler_specific.h"
14 #include "base/mac/scoped_nsobject.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "ui/compositor/compositor.h"
18 #include "ui/gfx/geometry/rect.h"
20 // AcceleratedTestView provides an NSView class that delegates drawing to a
21 // ui::Compositor delegate, setting up the NSOpenGLContext as required.
22 @interface AcceleratedTestView : NSView {
23   ui::Compositor* compositor_;
25 // Designated initializer.
26 -(id)init;
27 -(void)setCompositor:(ui::Compositor*)compositor;
28 @end
30 @implementation AcceleratedTestView
31 -(id)init {
32   // The frame will be resized when reparented into the window's view hierarchy.
33   self = [super initWithFrame:NSZeroRect];
34   return self;
37 -(void)setCompositor:(ui::Compositor*)compositor {
38   compositor_ = compositor;
41 - (void)drawRect:(NSRect)rect {
42   DCHECK(compositor_) << "Drawing with no compositor set.";
43   compositor_->ScheduleFullRedraw();
45 @end
47 namespace ui {
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   }
61  private:
62   NSAutoreleasePool* pool_;
63   DISALLOW_COPY_AND_ASSIGN(FoundationHost);
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   ~AppKitHost() override {}
75  private:
76   DISALLOW_COPY_AND_ASSIGN(AppKitHost);
79 // TestCompositorHostMac provides a window surface and a coordinated compositor
80 // for use in the compositor unit tests.
81 class TestCompositorHostMac : public TestCompositorHost,
82                               public AppKitHost {
83  public:
84   TestCompositorHostMac(const gfx::Rect& bounds,
85                         ui::ContextFactory* context_factory);
86   ~TestCompositorHostMac() override;
88  private:
89   // TestCompositorHost:
90   void Show() override;
91   ui::Compositor* GetCompositor() override;
93   gfx::Rect bounds_;
95   ui::ContextFactory* context_factory_;
97   ui::Compositor compositor_;
99   // Owned.  Released when window is closed.
100   NSWindow* window_;
102   DISALLOW_COPY_AND_ASSIGN(TestCompositorHostMac);
105 TestCompositorHostMac::TestCompositorHostMac(
106     const gfx::Rect& bounds,
107     ui::ContextFactory* context_factory)
108     : bounds_(bounds),
109       context_factory_(context_factory),
110       compositor_(context_factory, base::ThreadTaskRunnerHandle::Get()),
111       window_(nil) {
114 TestCompositorHostMac::~TestCompositorHostMac() {
115   // Release reference to |compositor_|.  Important because the |compositor_|
116   // holds |this| as its delegate, so that reference must be removed here.
117   [[window_ contentView] setCompositor:NULL];
118   [window_ setContentView:nil];
120   [window_ orderOut:nil];
121   [window_ close];
124 void TestCompositorHostMac::Show() {
125   DCHECK(!window_);
126   window_ = [[NSWindow alloc]
127                 initWithContentRect:NSMakeRect(bounds_.x(),
128                                                bounds_.y(),
129                                                bounds_.width(),
130                                                bounds_.height())
131                           styleMask:NSBorderlessWindowMask
132                             backing:NSBackingStoreBuffered
133                               defer:NO];
134   base::scoped_nsobject<AcceleratedTestView> view(
135       [[AcceleratedTestView alloc] init]);
136   compositor_.SetAcceleratedWidgetAndStartCompositor(view);
137   compositor_.SetScaleAndSize(1.0f, bounds_.size());
138   [view setCompositor:&compositor_];
139   [window_ setContentView:view];
140   [window_ orderFront:nil];
143 ui::Compositor* TestCompositorHostMac::GetCompositor() {
144   return &compositor_;
147 // static
148 TestCompositorHost* TestCompositorHost::Create(
149     const gfx::Rect& bounds,
150     ui::ContextFactory* context_factory) {
151   return new TestCompositorHostMac(bounds, context_factory);
154 }  // namespace ui