Skip to content

Commit 0ddbb2c

Browse files
bouraqadidvmason
andcommitted
Counter app example PjPharoCounterBrowserAppTest are green
Co-authored-by: Dave Mason <[email protected]> Co-authored-by: Noury Bouraqadi <[email protected]>
1 parent a256e92 commit 0ddbb2c

15 files changed

+437
-1
lines changed

.project

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
'srcDirectory' : 'Source'
2+
'srcDirectory' : 'Pharo'
33
}

Pharo/.properties

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
#format : #tonel
3+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Class {
2+
#name : #PjCounter,
3+
#superclass : #Object,
4+
#instVars : [
5+
'count'
6+
],
7+
#category : #'PharoJsExamples-Counter'
8+
}
9+
10+
{ #category : #accessing }
11+
PjCounter >> count [
12+
^ count
13+
]
14+
15+
{ #category : #accessing }
16+
PjCounter >> count: anObject [
17+
count := anObject
18+
]
19+
20+
{ #category : #'initialize - release' }
21+
PjCounter >> increment [
22+
self count: self count + 1
23+
]
24+
25+
{ #category : #'initialize - release' }
26+
PjCounter >> initialize [
27+
super initialize.
28+
self reset
29+
]
30+
31+
{ #category : #'initialize - release' }
32+
PjCounter >> reset [
33+
self count: 0
34+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Class {
2+
#name : #PjCounterBrowserApp,
3+
#superclass : #PjFileBasedBrowserApp,
4+
#instVars : [
5+
'counter',
6+
'controller'
7+
],
8+
#category : #'PharoJsExamples-Counter'
9+
}
10+
11+
{ #category : #description }
12+
PjCounterBrowserApp class >> appClasses [
13+
<pharoJsSkip>
14+
^super appClasses, {PjCounter. PjCounterController}
15+
]
16+
17+
{ #category : #description }
18+
PjCounterBrowserApp class >> appJsSubFolder [
19+
^'js'
20+
]
21+
22+
{ #category : #accessing }
23+
PjCounterBrowserApp >> countDisplay [
24+
^ self domElementAt: 'countDisplay'
25+
]
26+
27+
{ #category : #accessing }
28+
PjCounterBrowserApp >> incrementButton [
29+
^ self domElementAt: 'incrementButton'
30+
]
31+
32+
{ #category : #'initialize-release' }
33+
PjCounterBrowserApp >> initialize [
34+
| countDisplay resetButton incrementButton |
35+
super initialize.
36+
counter := PjCounter new.
37+
controller := PjCounterController new.
38+
countDisplay := self countDisplay.
39+
resetButton := self resetButton.
40+
incrementButton := self incrementButton.
41+
42+
controller counter: counter.
43+
controller updateDisplayBlock: [ :newCount | countDisplay innerHTML: newCount ].
44+
resetButton addEventListener: #click block: [ controller reset ].
45+
incrementButton addEventListener: #click block: [ controller increment ]
46+
]
47+
48+
{ #category : #accessing }
49+
PjCounterBrowserApp >> resetButton [
50+
^ self domElementAt: 'resetButton'
51+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Class {
2+
#name : #PjCounterBrowserAppTest,
3+
#superclass : #PjAppTestCase,
4+
#instVars : [
5+
'application',
6+
'countDisplay',
7+
'incrementButton',
8+
'resetButton'
9+
],
10+
#category : #'PharoJsExamples-Counter'
11+
}
12+
13+
{ #category : #'suite parameters' }
14+
PjCounterBrowserAppTest class >> appClass [
15+
^PjCounterBrowserApp
16+
]
17+
18+
{ #category : #testing }
19+
PjCounterBrowserAppTest class >> isAbstract [
20+
^self == PjCounterBrowserAppTest
21+
]
22+
23+
{ #category : #testing }
24+
PjCounterBrowserAppTest >> app [
25+
self subclassResponsibility
26+
]
27+
28+
{ #category : #testing }
29+
PjCounterBrowserAppTest >> setUp [
30+
super setUp.
31+
application := self app.
32+
countDisplay := application countDisplay.
33+
resetButton := application resetButton.
34+
incrementButton := application incrementButton.
35+
36+
]
37+
38+
{ #category : #testing }
39+
PjCounterBrowserAppTest >> testClickOnIncrementButtonIncreasesCountAndUpdatesDisplay [
40+
1 to: 10 do: [ : expectedCount |
41+
incrementButton click.
42+
self assert: countDisplay innerHTML equals: expectedCount asString
43+
]
44+
45+
]
46+
47+
{ #category : #testing }
48+
PjCounterBrowserAppTest >> testClickOnResetButtonSetsCountTo0 [
49+
3 timesRepeat: [incrementButton click.].
50+
resetButton click.
51+
self assert: countDisplay innerHTML equals: '0'
52+
53+
]
54+
55+
{ #category : #testing }
56+
PjCounterBrowserAppTest >> testInitialCountDisplayIs0 [
57+
self assert: countDisplay innerHTML equals: '0'
58+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Class {
2+
#name : #PjCounterController,
3+
#superclass : #Object,
4+
#instVars : [
5+
'counter',
6+
'updateDisplayBlock'
7+
],
8+
#category : #'PharoJsExamples-Counter'
9+
}
10+
11+
{ #category : #accessing }
12+
PjCounterController >> counter [
13+
^ counter
14+
]
15+
16+
{ #category : #accessing }
17+
PjCounterController >> counter: anObject [
18+
counter := anObject
19+
]
20+
21+
{ #category : #actions }
22+
PjCounterController >> increment [
23+
self counter increment.
24+
self updateDisplay
25+
]
26+
27+
{ #category : #actions }
28+
PjCounterController >> reset [
29+
self counter reset.
30+
self updateDisplay
31+
]
32+
33+
{ #category : #displaying }
34+
PjCounterController >> updateDisplay [
35+
self updateDisplayBlock value: self counter count
36+
]
37+
38+
{ #category : #accessing }
39+
PjCounterController >> updateDisplayBlock [
40+
^ updateDisplayBlock
41+
]
42+
43+
{ #category : #accessing }
44+
PjCounterController >> updateDisplayBlock: anObject [
45+
updateDisplayBlock := anObject
46+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Class {
2+
#name : #PjCounterTest,
3+
#superclass : #TestCase,
4+
#instVars : [
5+
'counter'
6+
],
7+
#category : #'PharoJsExamples-Counter'
8+
}
9+
10+
{ #category : #testing }
11+
PjCounterTest >> setUp [
12+
counter := PjCounter new
13+
]
14+
15+
{ #category : #testing }
16+
PjCounterTest >> testIncrementIncreasesCountBy1 [
17+
1 to: 10 do: [ : expectedCount |
18+
counter increment.
19+
self assert: counter count equals: expectedCount
20+
]
21+
22+
]
23+
24+
{ #category : #testing }
25+
PjCounterTest >> testInitialCountIs0 [
26+
self assert: counter count equals: 0
27+
]
28+
29+
{ #category : #testing }
30+
PjCounterTest >> testResetSetsCountTo0 [
31+
3 timesRepeat: [counter increment].
32+
counter reset.
33+
self assert: counter count equals: 0
34+
35+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"
2+
I am just the shared methods of a minimal browser application.
3+
4+
My subclasses are the actual implementions.
5+
"
6+
Class {
7+
#name : #PjDrawRectangleApp,
8+
#superclass : #PjBrowserApplication,
9+
#instVars : [
10+
'rectangle',
11+
'colour'
12+
],
13+
#category : #'PharoJsExamples-DrawRectangle'
14+
}
15+
16+
{ #category : #'event handling' }
17+
PjDrawRectangleApp >> onLoad [
18+
]
19+
20+
{ #category : #'event handling' }
21+
PjDrawRectangleApp >> start [
22+
super start.
23+
self setupDOM
24+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"
2+
I implement a minimal browser application.
3+
4+
I am a version that doesn't need a DNU infrastructure.
5+
"
6+
Class {
7+
#name : #PjDrawRectangleNoDnuApp,
8+
#superclass : #PjDrawRectangleApp,
9+
#category : #'PharoJsExamples-DrawRectangle'
10+
}
11+
12+
{ #category : #'initialize - release' }
13+
PjDrawRectangleNoDnuApp >> setupDOM [
14+
rectangle := document js_createElement: 'div'.
15+
rectangle js_at:#id put: 'grn'.
16+
colour := 'green'.
17+
(rectangle js_at:#style)
18+
js_at:#backgroundColor put: colour;
19+
js_at:#height put: '3cm';
20+
js_at:#width put: '3cm';
21+
js_at:#margin put: '-1.5cm';
22+
js_at:#position put: #absolute;
23+
js_at:#left put: '100px';
24+
js_at:#top put: '150px'.
25+
rectangle js_at:#innerHTML put: 'Clck to ','' capitalized,'change' capitalized,' colour'.
26+
rectangle js_addEventListener: #click block: [ : ev |
27+
colour = 'green' ifTrue: [
28+
colour := 'pink'.
29+
(rectangle js_at:#style) js_at:#backgroundColor put: colour
30+
] ifFalse: [
31+
colour = 'pink' ifTrue: [
32+
colour := 'yellow'.
33+
(rectangle js_at:#style) js_at:#backgroundColor put: colour
34+
] ifFalse: [
35+
" self flag: 'TODO: firefox requires the listener in removeEventListener'.
36+
" (document js_at:#body) js_removeChild: rectangle.
37+
self stop
38+
]]
39+
].
40+
41+
(document js_at:#body)
42+
js_appendChild: rectangle;
43+
js_addEventListener: #click block: [ : ev | | x y |
44+
x := ev js_at:#pageX.
45+
y := ev js_at:#pageY.
46+
(rectangle js_at:#style)
47+
js_at:#left put: x asString,'px';
48+
js_at:#top put: y asString,'px'
49+
]
50+
]
51+
52+
{ #category : #'initialize - release' }
53+
PjDrawRectangleNoDnuApp >> subscribeToDeviceEvents [
54+
" Bind any events that are required on startup. Common events are:
55+
'load', 'deviceready', 'offline', and 'online'."
56+
super subscribeToDeviceEvents.
57+
document js_addEventListener: 'load' block: [: ev | self onLoad].
58+
59+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"
2+
I implement a minimal browser application.
3+
4+
I am a version that needs a working DNU infrastructure.
5+
"
6+
Class {
7+
#name : #PjDrawRectangleUseDnuApp,
8+
#superclass : #PjDrawRectangleApp,
9+
#category : #'PharoJsExamples-DrawRectangle'
10+
}
11+
12+
{ #category : #'initialize - release' }
13+
PjDrawRectangleUseDnuApp >> setupDOM [
14+
rectangle := document createElement: 'div'.
15+
rectangle id: 'grn'.
16+
colour := 'green'.
17+
rectangle style
18+
backgroundColor: colour;
19+
height: '3cm';
20+
width: '3cm';
21+
margin: '-1.5cm';
22+
position: #absolute;
23+
left: '100px';
24+
top: '150px'.
25+
rectangle innerHTML: 'Clck to change colour'.
26+
rectangle addEventListener: #click block: [ : ev |
27+
colour = 'green' ifTrue: [
28+
colour := 'pink'.
29+
rectangle style backgroundColor: colour
30+
] ifFalse: [
31+
colour = 'pink' ifTrue: [
32+
colour := 'yellow'.
33+
rectangle style backgroundColor: colour
34+
] ifFalse: [
35+
self flag: 'TODO: firefox requires the listener in removeEventListener'.
36+
document body removeChild: rectangle.
37+
self stop
38+
]]
39+
].
40+
41+
document body
42+
appendChild: rectangle;
43+
addEventListener: #click block: [ : ev | | x y |
44+
x := ev pageX.
45+
y := ev pageY.
46+
rectangle style
47+
left: x asString,'px';
48+
top: y asString,'px'
49+
]
50+
]
51+
52+
{ #category : #'initialize - release' }
53+
PjDrawRectangleUseDnuApp >> subscribeToDeviceEvents [
54+
" Bind any events that are required on startup. Common events are:
55+
'load', 'deviceready', 'offline', and 'online'."
56+
super subscribeToDeviceEvents.
57+
document addEventListener: 'load' block: [: ev | self onLoad].
58+
59+
]

0 commit comments

Comments
 (0)