test_treeview_date.xhtml (6035B)
1 <?xml version="1.0"?> 2 3 <!-- This Source Code Form is subject to the terms of the Mozilla Public 4 - License, v. 2.0. If a copy of the MPL was not distributed with this 5 - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> 6 7 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> 8 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" 9 type="text/css"?> 10 11 <?xml-stylesheet href="chrome://browser/content/places/places.css"?> 12 <?xml-stylesheet href="chrome://browser/skin/places/tree-icons.css"?> 13 14 <window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 15 title="435322: Places tree view's formatting" 16 onload="runTest();"> 17 18 <script type="application/javascript" 19 src="chrome://browser/content/places/places-tree.js"/> 20 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" /> 21 <script type="application/javascript" src="head.js" /> 22 23 <body xmlns="http://www.w3.org/1999/xhtml" /> 24 25 <tree id="tree" 26 is="places-tree" 27 flatList="true" 28 flex="1"> 29 <treecols> 30 <treecol label="Title" id="title" anonid="title" primary="true" style="order: 1;" flex="1"/> 31 <splitter class="tree-splitter"/> 32 <treecol label="Tags" id="tags" anonid="tags" flex="1"/> 33 <splitter class="tree-splitter"/> 34 <treecol label="Url" id="url" anonid="url" flex="1"/> 35 <splitter class="tree-splitter"/> 36 <treecol label="Visit Date" id="date" anonid="date" flex="1"/> 37 <splitter class="tree-splitter"/> 38 <treecol label="Visit Count" id="visitCount" anonid="visitCount" flex="1"/> 39 </treecols> 40 <treechildren flex="1"/> 41 </tree> 42 43 <script type="application/javascript"> 44 <![CDATA[ 45 46 /** 47 * Bug 435322 48 * https://bugzilla.mozilla.org/show_bug.cgi?id=435322 49 * 50 * Ensures that date in places treeviews is correctly formatted. 51 */ 52 53 function runTest() { 54 SimpleTest.waitForExplicitFinish(); 55 56 function uri(spec) { 57 return Services.io.newURI(spec); 58 } 59 60 (async function() { 61 await PlacesUtils.history.clear(); 62 63 let midnight = new Date(); 64 midnight.setHours(0); 65 midnight.setMinutes(0); 66 midnight.setSeconds(0); 67 midnight.setMilliseconds(0); 68 69 // Add a visit 1ms before midnight, a visit at midnight, and 70 // a visit 1ms after midnight. 71 await PlacesTestUtils.addVisits([ 72 {uri: uri("https://before.midnight.com/"), 73 visitDate: (midnight.getTime() - 1) * 1000, 74 transition: PlacesUtils.history.TRANSITION_TYPED}, 75 {uri: uri("https://at.midnight.com/"), 76 visitDate: (midnight.getTime()) * 1000, 77 transition: PlacesUtils.history.TRANSITION_TYPED}, 78 {uri: uri("https://after.midnight.com/"), 79 visitDate: (midnight.getTime() + 1) * 1000, 80 transition: PlacesUtils.history.TRANSITION_TYPED} 81 ]); 82 83 // add a bookmark to the midnight visit 84 let bm = await PlacesUtils.bookmarks.insert({ 85 parentGuid: PlacesUtils.bookmarks.toolbarGuid, 86 index: PlacesUtils.bookmarks.DEFAULT_INDEX, 87 url: "https://at.midnight.com/", 88 title: "A bookmark at midnight", 89 type: PlacesUtils.bookmarks.TYPE_BOOKMARK 90 }); 91 92 // Make a history query. 93 let query = PlacesUtils.history.getNewQuery(); 94 let opts = PlacesUtils.history.getNewQueryOptions(); 95 let queryURI = PlacesUtils.history.queryToQueryString(query, opts); 96 97 // Setup the places tree contents. 98 let tree = document.getElementById("tree"); 99 tree.place = queryURI; 100 101 // loop through the rows and check formatting 102 let treeView = tree.view; 103 let rc = treeView.rowCount; 104 ok(rc >= 3, "Rows found"); 105 let columns = tree.columns; 106 ok(columns.count > 0, "Columns found"); 107 for (let r = 0; r < rc; r++) { 108 let node = treeView.nodeForTreeIndex(r); 109 ok(node, "Places node found"); 110 for (let ci = 0; ci < columns.count; ci++) { 111 let c = columns.getColumnAt(ci); 112 let text = treeView.getCellText(r, c); 113 switch (c.element.getAttribute("anonid")) { 114 case "title": 115 // The title can differ, we did not set any title so we would 116 // expect null, but in such a case the view will generate a title 117 // through PlacesUIUtils.getBestTitle. 118 if (node.title) 119 is(text, node.title, "Title is correct"); 120 break; 121 case "url": 122 is(text, node.uri, "Uri is correct"); 123 break; 124 case "date": { 125 let timeObj = new Date(node.time / 1000); 126 // Default is short date format. 127 let dtOptions = { 128 dateStyle: "short", 129 timeStyle: "short" 130 }; 131 132 // For today's visits we don't show date portion. 133 if (node.uri == "https://at.midnight.com/" || 134 node.uri == "https://after.midnight.com/") { 135 dtOptions.dateStyle = undefined; 136 } else if (node.uri != "https://before.midnight.com/") { 137 // Avoid to test spurious uris, due to how the test works 138 // a redirecting uri could be put in the tree while we test. 139 break; 140 } 141 let timeStr = new Services.intl.DateTimeFormat(undefined, dtOptions).format(timeObj); 142 143 is(text, timeStr, "Date format is correct"); 144 break; 145 } 146 case "visitCount": 147 is(text, 1, "Visit count is correct"); 148 break; 149 } 150 } 151 } 152 153 // Cleanup. 154 await PlacesUtils.bookmarks.remove(bm.guid); 155 await PlacesUtils.history.clear(); 156 })().then(SimpleTest.finish); 157 } 158 ]]> 159 </script> 160 </window>