1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/queries/test_transitions.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,178 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.6 +/* ***** BEGIN LICENSE BLOCK ***** 1.7 + Any copyright is dedicated to the Public Domain. 1.8 + http://creativecommons.org/publicdomain/zero/1.0/ 1.9 + * ***** END LICENSE BLOCK ***** */ 1.10 +var beginTime = Date.now(); 1.11 +var testData = [ 1.12 + { 1.13 + isVisit: true, 1.14 + title: "page 0", 1.15 + uri: "http://mozilla.com/", 1.16 + transType: Ci.nsINavHistoryService.TRANSITION_TYPED 1.17 + }, 1.18 + { 1.19 + isVisit: true, 1.20 + title: "page 1", 1.21 + uri: "http://google.com/", 1.22 + transType: Ci.nsINavHistoryService.TRANSITION_DOWNLOAD 1.23 + }, 1.24 + { 1.25 + isVisit: true, 1.26 + title: "page 2", 1.27 + uri: "http://microsoft.com/", 1.28 + transType: Ci.nsINavHistoryService.TRANSITION_DOWNLOAD 1.29 + }, 1.30 + { 1.31 + isVisit: true, 1.32 + title: "page 3", 1.33 + uri: "http://en.wikipedia.org/", 1.34 + transType: Ci.nsINavHistoryService.TRANSITION_BOOKMARK 1.35 + }, 1.36 + { 1.37 + isVisit: true, 1.38 + title: "page 4", 1.39 + uri: "http://fr.wikipedia.org/", 1.40 + transType: Ci.nsINavHistoryService.TRANSITION_DOWNLOAD 1.41 + }, 1.42 + { 1.43 + isVisit: true, 1.44 + title: "page 5", 1.45 + uri: "http://apple.com/", 1.46 + transType: Ci.nsINavHistoryService.TRANSITION_TYPED 1.47 + }, 1.48 + { 1.49 + isVisit: true, 1.50 + title: "page 6", 1.51 + uri: "http://campus-bike-store.com/", 1.52 + transType: Ci.nsINavHistoryService.TRANSITION_DOWNLOAD 1.53 + }, 1.54 + { 1.55 + isVisit: true, 1.56 + title: "page 7", 1.57 + uri: "http://uwaterloo.ca/", 1.58 + transType: Ci.nsINavHistoryService.TRANSITION_TYPED 1.59 + }, 1.60 + { 1.61 + isVisit: true, 1.62 + title: "page 8", 1.63 + uri: "http://pugcleaner.com/", 1.64 + transType: Ci.nsINavHistoryService.TRANSITION_BOOKMARK 1.65 + }, 1.66 + { 1.67 + isVisit: true, 1.68 + title: "page 9", 1.69 + uri: "http://de.wikipedia.org/", 1.70 + transType: Ci.nsINavHistoryService.TRANSITION_TYPED 1.71 + }, 1.72 + { 1.73 + isVisit: true, 1.74 + title: "arewefastyet", 1.75 + uri: "http://arewefastyet.com/", 1.76 + transType: Ci.nsINavHistoryService.TRANSITION_DOWNLOAD 1.77 + }, 1.78 + { 1.79 + isVisit: true, 1.80 + title: "arewefastyet", 1.81 + uri: "http://arewefastyet.com/", 1.82 + transType: Ci.nsINavHistoryService.TRANSITION_BOOKMARK 1.83 + }]; 1.84 +// sets of indices of testData array by transition type 1.85 +var testDataTyped = [0, 5, 7, 9]; 1.86 +var testDataDownload = [1, 2, 4, 6, 10]; 1.87 +var testDataBookmark = [3, 8, 11]; 1.88 + 1.89 +/** 1.90 + * run_test is where the magic happens. This is automatically run by the test 1.91 + * harness. It is where you do the work of creating the query, running it, and 1.92 + * playing with the result set. 1.93 + */ 1.94 +function run_test() 1.95 +{ 1.96 + run_next_test(); 1.97 +} 1.98 + 1.99 +add_task(function test_transitions() 1.100 +{ 1.101 + let timeNow = Date.now(); 1.102 + for each (let item in testData) { 1.103 + yield promiseAddVisits({ 1.104 + uri: uri(item.uri), 1.105 + transition: item.transType, 1.106 + visitDate: timeNow++ * 1000, 1.107 + title: item.title 1.108 + }); 1.109 + } 1.110 + 1.111 + //dump_table("moz_places"); 1.112 + //dump_table("moz_historyvisits"); 1.113 + 1.114 + var numSortFunc = function (a,b) { return (a - b); }; 1.115 + var arrs = testDataTyped.concat(testDataDownload).concat(testDataBookmark) 1.116 + .sort(numSortFunc); 1.117 + 1.118 + // Four tests which compare the result of a query to an expected set. 1.119 + var data = arrs.filter(function (index) { 1.120 + return (testData[index].uri.match(/arewefastyet\.com/) && 1.121 + testData[index].transType == 1.122 + Ci.nsINavHistoryService.TRANSITION_DOWNLOAD); 1.123 + }); 1.124 + 1.125 + compareQueryToTestData("place:domain=arewefastyet.com&transition=" + 1.126 + Ci.nsINavHistoryService.TRANSITION_DOWNLOAD, 1.127 + data.slice()); 1.128 + 1.129 + compareQueryToTestData("place:transition=" + 1.130 + Ci.nsINavHistoryService.TRANSITION_DOWNLOAD, 1.131 + testDataDownload.slice()); 1.132 + 1.133 + compareQueryToTestData("place:transition=" + 1.134 + Ci.nsINavHistoryService.TRANSITION_TYPED, 1.135 + testDataTyped.slice()); 1.136 + 1.137 + compareQueryToTestData("place:transition=" + 1.138 + Ci.nsINavHistoryService.TRANSITION_DOWNLOAD + 1.139 + "&transition=" + 1.140 + Ci.nsINavHistoryService.TRANSITION_BOOKMARK, 1.141 + data); 1.142 + 1.143 + // Tests the live update property of transitions. 1.144 + var query = {}; 1.145 + var options = {}; 1.146 + PlacesUtils.history. 1.147 + queryStringToQueries("place:transition=" + 1.148 + Ci.nsINavHistoryService.TRANSITION_DOWNLOAD, 1.149 + query, {}, options); 1.150 + query = (query.value)[0]; 1.151 + options = PlacesUtils.history.getNewQueryOptions(); 1.152 + var result = PlacesUtils.history.executeQuery(query, options); 1.153 + var root = result.root; 1.154 + root.containerOpen = true; 1.155 + do_check_eq(testDataDownload.length, root.childCount); 1.156 + yield promiseAddVisits({ 1.157 + uri: uri("http://getfirefox.com"), 1.158 + transition: TRANSITION_DOWNLOAD 1.159 + }); 1.160 + do_check_eq(testDataDownload.length + 1, root.childCount); 1.161 + root.containerOpen = false; 1.162 +}); 1.163 + 1.164 +/* 1.165 + * Takes a query and a set of indices. The indices correspond to elements 1.166 + * of testData that are the result of the query. 1.167 + */ 1.168 +function compareQueryToTestData(queryStr, data) { 1.169 + var query = {}; 1.170 + var options = {}; 1.171 + PlacesUtils.history.queryStringToQueries(queryStr, query, {}, options); 1.172 + query = query.value[0]; 1.173 + options = options.value; 1.174 + var result = PlacesUtils.history.executeQuery(query, options); 1.175 + var root = result.root; 1.176 + for (var i = 0; i < data.length; i++) { 1.177 + data[i] = testData[data[i]]; 1.178 + data[i].isInQuery = true; 1.179 + } 1.180 + compareArrayToResult(data, root); 1.181 +}