1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
+
5
+ package mozilla.components.browser.storage.memory
6
+
7
+ import mozilla.components.concept.storage.PageObservation
8
+ import mozilla.components.concept.storage.VisitType
9
+ import mozilla.components.support.test.mock
10
+ import org.junit.Assert.assertEquals
11
+ import org.junit.Test
12
+ import org.mockito.Mockito.verify
13
+ import org.mockito.Mockito.reset
14
+
15
+ class InMemoryHistoryStorageTest {
16
+ @Test
17
+ fun `store can be used to track visit information` () {
18
+ val history = InMemoryHistoryStorage ()
19
+
20
+ assertEquals(0 , history.pages.size)
21
+
22
+ history.recordVisit(" http://www.mozilla.org" , VisitType .LINK )
23
+ assertEquals(1 , history.pages.size)
24
+ assertEquals(1 , history.pages[" http://www.mozilla.org" ]!! .size)
25
+ assertEquals(VisitType .LINK , history.pages[" http://www.mozilla.org" ]!! [0 ].type)
26
+
27
+ // Reloads are recorded.
28
+ history.recordVisit(" http://www.mozilla.org" , VisitType .RELOAD )
29
+ assertEquals(1 , history.pages.size)
30
+ assertEquals(2 , history.pages[" http://www.mozilla.org" ]!! .size)
31
+ assertEquals(VisitType .LINK , history.pages[" http://www.mozilla.org" ]!! [0 ].type)
32
+ assertEquals(VisitType .RELOAD , history.pages[" http://www.mozilla.org" ]!! [1 ].type)
33
+
34
+ // Visits for multiple pages are tracked.
35
+ history.recordVisit(" http://www.firefox.com" , VisitType .LINK )
36
+ assertEquals(2 , history.pages.size)
37
+ assertEquals(2 , history.pages[" http://www.mozilla.org" ]!! .size)
38
+ assertEquals(VisitType .LINK , history.pages[" http://www.mozilla.org" ]!! [0 ].type)
39
+ assertEquals(VisitType .RELOAD , history.pages[" http://www.mozilla.org" ]!! [1 ].type)
40
+ assertEquals(1 , history.pages[" http://www.firefox.com" ]!! .size)
41
+ assertEquals(VisitType .LINK , history.pages[" http://www.firefox.com" ]!! [0 ].type)
42
+ }
43
+
44
+ @Test
45
+ fun `store can be used to record and retrieve history via webview-style callbacks` () {
46
+ val history = InMemoryHistoryStorage ()
47
+
48
+ val callback = mock< (List <String >) -> Unit > ()
49
+
50
+ // Empty.
51
+ history.getVisited(callback)
52
+ verify(callback).invoke(listOf ())
53
+ reset(callback)
54
+ history.getVisited(callback)
55
+ verify(callback).invoke(listOf ())
56
+ reset(callback)
57
+
58
+ // Regular visits are tracked.
59
+ history.recordVisit(" https://www.mozilla.org" , VisitType .LINK )
60
+ history.getVisited(callback)
61
+ verify(callback).invoke(listOf (" https://www.mozilla.org" ))
62
+ reset(callback)
63
+
64
+ // Multiple visits can be tracked, results ordered by "URL's first seen first".
65
+ history.recordVisit(" https://www.firefox.com" , VisitType .LINK )
66
+ history.getVisited(callback)
67
+ verify(callback).invoke(listOf (" https://www.mozilla.org" , " https://www.firefox.com" ))
68
+ reset(callback)
69
+
70
+ // Visits marked as reloads can be tracked.
71
+ history.recordVisit(" https://www.firefox.com" , VisitType .RELOAD )
72
+ history.getVisited(callback)
73
+ verify(callback).invoke(listOf (" https://www.mozilla.org" , " https://www.firefox.com" ))
74
+ reset(callback)
75
+
76
+ // Visited urls are certainly a set.
77
+ history.recordVisit(" https://www.firefox.com" , VisitType .LINK )
78
+ history.recordVisit(" https://www.mozilla.org" , VisitType .LINK )
79
+ history.recordVisit(" https://www.wikipedia.org" , VisitType .LINK )
80
+ history.getVisited(callback)
81
+ verify(callback).invoke(listOf (" https://www.mozilla.org" , " https://www.firefox.com" , " https://www.wikipedia.org" ))
82
+ }
83
+
84
+ @Test
85
+ fun `store can be used to record and retrieve history via gecko-style callbacks` () {
86
+ val history = InMemoryHistoryStorage ()
87
+
88
+ // Empty.
89
+ val callback = mock< (List <Boolean >) -> Unit > ()
90
+
91
+ history.getVisited(listOf (), callback)
92
+ verify(callback).invoke(listOf ())
93
+ reset(callback)
94
+
95
+ // Regular visits are tracked
96
+ history.recordVisit(" https://www.mozilla.org" , VisitType .LINK )
97
+ history.getVisited(listOf (" https://www.mozilla.org" ), callback)
98
+ verify(callback).invoke(listOf (true ))
99
+ reset(callback)
100
+
101
+ // Duplicate requests are handled.
102
+ history.getVisited(listOf (" https://www.mozilla.org" , " https://www.mozilla.org" ), callback)
103
+ verify(callback).invoke(listOf (true , true ))
104
+ reset(callback)
105
+
106
+ // Visit map is returned in correct order.
107
+ history.getVisited(listOf (" https://www.mozilla.org" , " https://www.unknown.com" ), callback)
108
+ verify(callback).invoke(listOf (true , false ))
109
+ reset(callback)
110
+
111
+ history.getVisited(listOf (" https://www.unknown.com" , " https://www.mozilla.org" ), callback)
112
+ verify(callback).invoke(listOf (false , true ))
113
+ reset(callback)
114
+
115
+ // Multiple visits can be tracked. Reloads can be tracked.
116
+ history.recordVisit(" https://www.firefox.com" , VisitType .LINK )
117
+ history.recordVisit(" https://www.mozilla.org" , VisitType .RELOAD )
118
+ history.recordVisit(" https://www.wikipedia.org" , VisitType .LINK )
119
+ history.getVisited(listOf (" https://www.firefox.com" , " https://www.wikipedia.org" , " https://www.unknown.com" , " https://www.mozilla.org" ), callback)
120
+ verify(callback).invoke(listOf (true , true , false , true ))
121
+ }
122
+
123
+ @Test
124
+ fun `store can be used to track page meta information - title changes` () {
125
+ val history = InMemoryHistoryStorage ()
126
+ assertEquals(0 , history.pageMeta.size)
127
+
128
+ // Title changes are recorded.
129
+ history.recordObservation(" https://www.wikipedia.org" , PageObservation (" Wikipedia" ))
130
+ assertEquals(1 , history.pageMeta.size)
131
+ assertEquals(PageObservation (" Wikipedia" ), history.pageMeta[" https://www.wikipedia.org" ])
132
+
133
+ history.recordObservation(" https://www.wikipedia.org" , PageObservation (" Википедия" ))
134
+ assertEquals(1 , history.pageMeta.size)
135
+ assertEquals(PageObservation (" Википедия" ), history.pageMeta[" https://www.wikipedia.org" ])
136
+
137
+ // Titles for different pages are recorded.
138
+ history.recordObservation(" https://www.firefox.com" , PageObservation (" Firefox" ))
139
+ history.recordObservation(" https://www.mozilla.org" , PageObservation (" Мозилла" ))
140
+ assertEquals(3 , history.pageMeta.size)
141
+ assertEquals(PageObservation (" Википедия" ), history.pageMeta[" https://www.wikipedia.org" ])
142
+ assertEquals(PageObservation (" Firefox" ), history.pageMeta[" https://www.firefox.com" ])
143
+ assertEquals(PageObservation (" Мозилла" ), history.pageMeta[" https://www.mozilla.org" ])
144
+ }
145
+ }
0 commit comments