1
1
// make sure you import mocha-config before @angular/core
2
2
import { assert } from "./test-config" ;
3
- import { Component , ElementRef , Renderer2 , NgZone } from "@angular/core" ;
3
+ import { Component , ElementRef , Renderer2 , NgZone , ViewChild } from "@angular/core" ;
4
4
import { ProxyViewContainer } from "ui/proxy-view-container" ;
5
5
import { Red } from "color/known-colors" ;
6
6
import { dumpView } from "./test-utils" ;
@@ -10,6 +10,9 @@ import { StackLayout } from "ui/layouts/stack-layout";
10
10
import { ContentView } from "ui/content-view" ;
11
11
import { Button } from "ui/button" ;
12
12
import { NgView } from "nativescript-angular/element-registry" ;
13
+ import { registerElement } from "nativescript-angular/element-registry" ;
14
+ import * as button from "tns-core-modules/ui/button" ;
15
+ import * as view from "tns-core-modules/ui/core/view" ;
13
16
14
17
@Component ( {
15
18
template : `<StackLayout><Label text="Layout"></Label></StackLayout>`
@@ -136,6 +139,61 @@ export class NgIfThenElseComponent {
136
139
}
137
140
}
138
141
142
+ export class ButtonCounter extends button . Button {
143
+ nativeBackgroundRedraws = 0 ;
144
+ backgroundInternalSetNativeCount = 0 ;
145
+ fontInternalSetNativeCount = 0 ;
146
+
147
+ [ view . backgroundInternalProperty . setNative ] ( value ) {
148
+ this . backgroundInternalSetNativeCount ++ ;
149
+ return super [ view . backgroundInternalProperty . setNative ] ( value ) ;
150
+ }
151
+ [ view . fontInternalProperty . setNative ] ( value ) {
152
+ this . fontInternalSetNativeCount ++ ;
153
+ return super [ view . fontInternalProperty . setNative ] ( value ) ;
154
+ }
155
+ _redrawNativeBackground ( value : any ) : void {
156
+ this . nativeBackgroundRedraws ++ ;
157
+ super . _redrawNativeBackground ( value ) ;
158
+ }
159
+ }
160
+ ButtonCounter . prototype . recycleNativeView = false ;
161
+ registerElement ( "ButtonCounter" , ( ) => ButtonCounter ) ;
162
+
163
+ @Component ( {
164
+ selector : "ng-control-setters-count" ,
165
+ template : `
166
+ <StackLayout>
167
+ <ButtonCounter #btn1 id="btn1" borderWidth="1" borderColor="gray" borderRadius="16" fontWeight="bold" fontSize="16"></ButtonCounter>
168
+ <ButtonCounter #btn2 id="btn2"></ButtonCounter>
169
+ <ButtonCounter #btn3 id="btn3" borderWidth="1" borderColor="gray" borderRadius="16" fontWeight="bold" fontSize="16"></ButtonCounter>
170
+ <ButtonCounter #btn4 id="btn4" borderRadius="3" style="background-image: url('~/logo.png'); background-position: center; background-repeat: no-repeat; background-size: cover;"></ButtonCounter>
171
+ </StackLayout>
172
+ ` ,
173
+ styles : [ `
174
+ #btn2, #btn3, #btn4 {
175
+ border-width: 2;
176
+ border-color: teal;
177
+ border-radius: 20;
178
+ font-weight: 400;
179
+ font-size: 32;
180
+ }` ]
181
+ } )
182
+ export class NgControlSettersCount {
183
+ @ViewChild ( "btn1" ) btn1 : ElementRef ;
184
+ @ViewChild ( "btn2" ) btn2 : ElementRef ;
185
+ @ViewChild ( "btn3" ) btn3 : ElementRef ;
186
+ @ViewChild ( "btn3" ) btn4 : ElementRef ;
187
+
188
+ get buttons ( ) : ElementRef [ ] { return [ this . btn1 , this . btn2 , this . btn3 , this . btn4 ] ; }
189
+
190
+ isAfterViewInit : boolean = false ;
191
+
192
+ ngAfterViewInit ( ) {
193
+ this . isAfterViewInit = true ;
194
+ }
195
+ }
196
+
139
197
@Component ( {
140
198
selector : "ng-for-label" ,
141
199
template : `<Label *ngFor="let item of items" [text]="item"></Label>`
@@ -409,7 +467,6 @@ describe("Renderer createElement", () => {
409
467
} ) ;
410
468
} ) ;
411
469
412
-
413
470
describe ( "Renderer attach/detach" , ( ) => {
414
471
let testApp : TestApp = null ;
415
472
let renderer : Renderer2 = null ;
@@ -480,3 +537,36 @@ describe("Renderer attach/detach", () => {
480
537
} ) ;
481
538
} ) ;
482
539
540
+ describe ( "Renderer lifecycle" , ( ) => {
541
+ let testApp : TestApp = null ;
542
+ let renderer : Renderer2 = null ;
543
+
544
+ before ( ( ) => {
545
+ return TestApp . create ( [ ] , [ NgControlSettersCount ] ) . then ( ( app ) => {
546
+ testApp = app ;
547
+ renderer = testApp . renderer ;
548
+ } ) ;
549
+ } ) ;
550
+
551
+ after ( ( ) => {
552
+ testApp . dispose ( ) ;
553
+ } ) ;
554
+
555
+ afterEach ( ( ) => {
556
+ testApp . disposeComponents ( ) ;
557
+ } ) ;
558
+
559
+ it ( "view native setters are called once on startup" , ( ) => {
560
+ return testApp . loadComponent ( NgControlSettersCount ) . then ( ( componentRef ) => {
561
+ assert . isTrue ( componentRef . instance . isAfterViewInit , "Expected the NgControlSettersCount to have passed its ngAfterViewInit." ) ;
562
+ componentRef . instance . buttons . map ( btn => btn . nativeElement ) . forEach ( btn => {
563
+ assert . isTrue ( btn . isLoaded , `Expected ${ btn . id } to be allready loaded.` ) ;
564
+ assert . isFalse ( btn . isLayoutValid , `Expected ${ btn . id } 's layout to be invalid.` ) ;
565
+
566
+ assert . equal ( btn . backgroundInternalSetNativeCount , 1 , `Expected ${ btn . id } backgroundInternalSetNativeCount to be called just once.` ) ;
567
+ assert . equal ( btn . fontInternalSetNativeCount , 1 , `Expected ${ btn . id } fontInternalSetNativeCount to be called just once.` ) ;
568
+ assert . equal ( btn . nativeBackgroundRedraws , 0 , `Expected ${ btn . id } nativeBackgroundRedraws to be called after its layout pass.` ) ;
569
+ } ) ;
570
+ } ) ;
571
+ } ) ;
572
+ } ) ;
0 commit comments