183
|
1 package Test::Web::View;
|
188
|
2 use IMPL::Profiler::Memory;
|
183
|
3 use strict;
|
|
4 use warnings;
|
185
|
5 use utf8;
|
183
|
6
|
|
7 use parent qw(IMPL::Test::Unit);
|
|
8 __PACKAGE__->PassThroughArgs;
|
|
9
|
185
|
10 use File::Slurp;
|
188
|
11 use Scalar::Util qw(weaken);
|
185
|
12
|
188
|
13 use IMPL::Test qw(assert test GetCallerSourceLine);
|
183
|
14 use IMPL::Web::View::TTLoader();
|
|
15
|
|
16 use constant {
|
194
|
17 TTLoader => typeof IMPL::Web::View::TTLoader,
|
|
18 MProfiler => 'IMPL::Profiler::Memory'
|
183
|
19 };
|
|
20
|
188
|
21 sub AssertMemoryLeak {
|
194
|
22 my $code = shift;
|
|
23 my $dump = shift;
|
|
24
|
|
25 my $data = MProfiler->Monitor($code);
|
|
26
|
|
27 if ($data->isLeak and $dump) {
|
|
28 write_file("dump.out", { binmode => ':utf8' }, $data->Dump() );
|
|
29 }
|
|
30
|
|
31 assert( not($data->isLeak), "Memory leak detected", GetCallerSourceLine() , @{$data->{objects}} );
|
188
|
32 }
|
|
33
|
185
|
34 sub templatesDir {
|
194
|
35 $_[0]->GetResourceDir('Resources','TTView');
|
185
|
36 }
|
|
37
|
188
|
38 sub CreateLoader {
|
194
|
39 my ($this) = @_;
|
|
40
|
|
41 my $loader = TTLoader->new(
|
|
42 {
|
|
43 INCLUDE_PATH => [
|
|
44 $this->templatesDir
|
|
45 ],
|
|
46 INTERPOLATE => 1,
|
|
47 POST_CHOMP => 1,
|
|
48 ENCODING => 'utf-8'
|
|
49 },
|
|
50 ext => '.tt',
|
|
51 initializer => 'global.tt',
|
|
52 globals => {
|
|
53 site => {
|
|
54 name => 'Test Site'
|
|
55 },
|
|
56 date => {
|
|
57 now => sub { localtime(time); }
|
|
58 },
|
|
59 dynamic => sub { 'this is a dynamic value' }
|
|
60 }
|
|
61 );
|
188
|
62 }
|
|
63
|
|
64 test TTLoaderTests => sub {
|
194
|
65 my ($this) = @_;
|
|
66
|
|
67 my $loader = $this->CreateLoader();
|
|
68
|
|
69 # test the loader to be able to find a desired resource
|
|
70 assert( defined($loader->template('simple') ) );
|
|
71
|
|
72 # loader should be initialized on demand
|
|
73 assert( not $loader->isInitialized );
|
|
74
|
|
75 # loader should be able to load a document
|
|
76 my $doc = $loader->document('simple');
|
|
77 assert(defined $doc);
|
|
78
|
|
79 assert( $loader->isInitialized );
|
|
80 assert( $loader->context->stash->get('user') eq 'test_user');
|
|
81
|
|
82 # document should inherit loader's context
|
|
83 assert( $doc->context->stash->get('user') eq 'test_user');
|
|
84
|
|
85 # document should not have 'this' template variable
|
|
86 assert( not $doc->templateVars('this') );
|
|
87
|
|
88 assert( $doc->context != $loader->context); # document should have an own context
|
183
|
89 };
|
|
90
|
185
|
91 test TTDocumentTests => sub {
|
194
|
92 my ($this) = @_;
|
|
93 my $loader = $this->CreateLoader();
|
|
94
|
|
95 my $doc = $loader->document('simple');
|
|
96
|
|
97 assert(defined $doc);
|
|
98 $doc->title('test document');
|
|
99
|
|
100 assert($doc->nodeName eq 'document');
|
|
101 assert($doc->title eq 'test document');
|
|
102
|
|
103 assert(not $doc->can('notexists')); # autoloaded property should be ignored
|
|
104 assert(not defined $doc->notexists); # nonexisting property
|
|
105 assert($doc->template->version == 10); # static metadata
|
|
106 assert($doc->templateVars('notexists') eq ''); #nonexisting template variable
|
|
107 assert($doc->templateVars('user') eq 'test_user'); # global data
|
|
108 assert($doc->templateVars('templateVar') eq ''); # defined in CTOR block, should be local
|
|
109 assert($doc->templateVars('dynamic') eq 'this is a dynamic value');
|
|
110
|
|
111 my $text = $doc->Render();
|
|
112 my $expected = read_file($this->GetResourceFile('Resources','TTView.Output','simple.txt'), binmode => ':utf8');
|
|
113
|
|
114 assert($text eq $expected, "Bad Render() output","Got: $text", "Expected: $expected");
|
|
115
|
185
|
116 };
|
|
117
|
186
|
118 test TTControlTests => sub {
|
194
|
119 my ($this) = @_;
|
|
120
|
|
121 my $loader = $this->CreateLoader();
|
|
122
|
|
123 my $doc = $loader->document('simple');
|
|
124
|
|
125 assert(defined $doc);
|
|
126
|
|
127 my $factory = $doc->require('My/Org/Panel');
|
|
128
|
|
129 assert(defined $factory);
|
|
130
|
|
131
|
|
132 assert($factory->context->stash != $doc->context->stash);
|
|
133
|
|
134 assert($factory == $doc->require('My/Org/Panel'), "Control should be loaded only once");
|
|
135
|
|
136 my $ctl = $factory->new('information', { visualClass => 'simple', data => ['one','two','hello world'] } );
|
|
137
|
|
138 assert(defined $ctl);
|
|
139
|
|
140 assert($ctl->nodeName eq 'information', "Created control should have a name", "Got: ".$ctl->nodeName, "Expected: information");
|
|
141
|
|
142 assert($ctl->nodeProperty('visualClass') eq 'simple');
|
|
143
|
|
144 assert($factory->instances == 1);
|
|
145
|
|
146 $doc->appendChild($ctl);
|
|
147
|
|
148 assert($doc->templateVars('dojo.require'));
|
|
149 assert(ref $doc->templateVars('dojo.require') eq 'ARRAY');
|
|
150 assert($doc->templateVars('dojo.require')->[0] eq 'dijit.form.Input' );
|
|
151
|
|
152 my $text = $ctl->Render();
|
|
153
|
|
154 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8');
|
|
155 assert($text eq $expected, '$ctl->Render(): Bad output', "Got: $text", "Expected: $expected");
|
|
156
|
|
157
|
|
158
|
191
|
159 };
|
|
160
|
|
161 test TestDocumentLayout => sub {
|
194
|
162 my ($this) = @_;
|
|
163
|
|
164 my $loader = $this->CreateLoader();
|
|
165
|
|
166 my $doc = $loader->document(
|
|
167 'complex',
|
|
168 {
|
|
169 data => [qw(one two three)],
|
|
170 site => {
|
|
171 name => 'Test Site'
|
|
172 }
|
|
173 }
|
|
174 );
|
|
175
|
|
176 assert($doc->layout eq 'Layout/default');
|
|
177
|
|
178 assert($doc->templateVars('dojo.require')->[0]);
|
|
179
|
|
180 my $text = $doc->Render();
|
|
181
|
|
182 my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'complex.default.txt'), binmode => ':utf8' );
|
|
183 assert($text eq $expected, '$doc->Render(): Bad output', "Got: $text", "Expected: $expected");
|
186
|
184 };
|
|
185
|
188
|
186 test TestMemoryLeaks => sub {
|
194
|
187 my ($this) = @_;
|
|
188
|
|
189 my $loader = $this->CreateLoader();
|
|
190 $loader->document('simple'); # force loader initialization
|
|
191
|
|
192 AssertMemoryLeak(sub {
|
|
193 my $doc = $loader->document('simple');
|
|
194 });
|
|
195
|
|
196 AssertMemoryLeak(sub {
|
|
197 my $doc = $loader->document('simple');
|
|
198 $doc->Render( { self => $doc } );
|
|
199 });
|
|
200
|
|
201 $loader->template('Layout/default');
|
|
202 $loader->template('My/Org/Panel');
|
|
203 $loader->template('My/Org/TextPreview');
|
|
204 AssertMemoryLeak(sub {
|
|
205 my $doc = $loader->document('simple');
|
|
206 my $factory = $doc->require('My/Org/Panel');
|
|
207 my $ctl = $doc->AppendChild($factory->new('information', { visualClass => 'complex' }) );
|
|
208 });
|
|
209
|
|
210 $loader->template('complex');
|
|
211 AssertMemoryLeak(sub {
|
|
212 my $doc = $loader->document('complex');
|
|
213 $doc->Render();
|
|
214 },'dump');
|
|
215
|
188
|
216 };
|
|
217
|
183
|
218 1; |