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