Would be nice if the review-tool offered to show the corresponding header
[WebKit-https.git] / Websites / bugs.webkit.org / code-review-test.html
1 <div>Tests for some of the easily unittestable parts of code-review.js. You should see a series of "PASSED" lines.</div>
2 <div>FIXME: Run these as part of the layout test suite?</div>
3
4 <script>CODE_REVIEW_UNITTEST = true</script>
5 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
6 <script src="code-review.js"></script>
7 <pre id="output"></pre>
8 <script>
9
10 function inherits(childConstructor, parentConstructor) {
11   function tempConstructor() {};
12   tempConstructor.prototype = parentConstructor.prototype;
13   childConstructor.prototype = new tempConstructor();
14   childConstructor.prototype.constructor = childConstructor;
15 }
16
17 function MockLocalStorage() {
18     this.localStorageStore = {};
19     this.log = [];
20
21     this.getItem = function(key) {
22         this.log.push('getItem:' + key);
23         return this.localStorageStore[key];
24     };
25
26     this.setItem = function(key, value) {
27         // For testing sake, consider having more than 2 items to exceed the storage quota.
28         if (Object.keys(this.localStorageStore).length > 2) {
29             this.log.push('QuotaExceeded on setItem:' + key + ',' + value);
30             throw "QuotaExceeded";
31         }
32         this.log.push('setItem:' + key + ',' + value);
33         this.localStorageStore[key] = value;
34     };
35
36     this.removeItem = function(key) {
37         this.log.push('removeItem:' + key);
38         delete this.localStorageStore[key];
39     };
40
41     this.log_string = function() {
42         return this.log.join('\n');
43     };
44
45     this.key = function(i) {
46         return Object.keys(this.localStorageStore)[i];
47     };
48
49     this.__defineGetter__('length', function() {
50         return Object.keys(this.localStorageStore).length;
51     });
52 }
53
54 function MockDraftCommentSaver(attachment_id, opt_localStorage) {
55     DraftCommentSaver.call(this, attachment_id, opt_localStorage);
56 }
57
58 inherits(MockDraftCommentSaver, DraftCommentSaver)
59
60 MockDraftCommentSaver.prototype._json = function() {
61     return "{MOCK JSON}";
62 }
63
64 MockDraftCommentSaver.prototype._should_remove_comments = function(message) {
65     return false;
66 }
67
68 function log(msg) {
69     document.getElementById('output').innerHTML += msg + '\n\n';
70 }
71
72 function ASSERT_EQUAL(actual, expected) {
73     if (actual == expected)
74         log('PASSED');
75     else
76         log('FAILED:\ngot:\n' + actual + '\nexpected:\n' + expected + '');
77 }
78
79 var links = tracLinks('foo/bar/baz.cpp', '');
80 ASSERT_EQUAL($(links).attr('href'), 'http://trac.webkit.org/log/trunk/foo/bar/baz.h');
81
82 var links = tracLinks('foo/bar.cpp/qux.mm', '');
83 ASSERT_EQUAL($(links).attr('href'), 'http://trac.webkit.org/log/trunk/foo/bar.cpp/qux.h');
84
85 // Basic setItem.
86 var ls = new MockLocalStorage();
87 new MockDraftCommentSaver('1234', ls).save();
88 ASSERT_EQUAL(ls.log_string(), 'setItem:draft-comments-for-attachment-1234,{MOCK JSON}');
89
90 // Exceed quota, but succeed after erasing old reviews.
91 var ls = new MockLocalStorage();
92 ls.localStorageStore = {
93     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
94     'draft-comments-for-attachment-2': '{"born-on": 100, "comments":[]}',
95     'draft-comments-for-attachment-3': '{"born-on": 100, "comments":[]}',
96     'draft-comments-for-attachment-4': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}'
97 };
98 new MockDraftCommentSaver('1234', ls).save();
99 ASSERT_EQUAL(ls.log_string(), 'QuotaExceeded on setItem:draft-comments-for-attachment-1234,{MOCK JSON}\ngetItem:draft-comments-for-attachment-1\ngetItem:draft-comments-for-attachment-2\ngetItem:draft-comments-for-attachment-3\ngetItem:draft-comments-for-attachment-4\nremoveItem:draft-comments-for-attachment-1\nremoveItem:draft-comments-for-attachment-2\nremoveItem:draft-comments-for-attachment-3\nsetItem:draft-comments-for-attachment-1234,{MOCK JSON}');
100
101 // Exceed quota after erasing old reviews and fail after prompt.
102 var ls = new MockLocalStorage();
103 ls.localStorageStore = {
104     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
105     'draft-comments-for-attachment-2': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
106     'draft-comments-for-attachment-3': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
107     'draft-comments-for-attachment-4': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}'
108 };
109 var mockDraftSaver = new MockDraftCommentSaver('1234', ls);
110 mockDraftSaver.save();
111 // Second save to ensure that we stop trying to save when we fail the prompt.
112 mockDraftSaver.save();
113 ASSERT_EQUAL(ls.log_string(), 'QuotaExceeded on setItem:draft-comments-for-attachment-1234,{MOCK JSON}\ngetItem:draft-comments-for-attachment-1\ngetItem:draft-comments-for-attachment-2\ngetItem:draft-comments-for-attachment-3\ngetItem:draft-comments-for-attachment-4\nremoveItem:draft-comments-for-attachment-1\nQuotaExceeded on setItem:draft-comments-for-attachment-1234,{MOCK JSON}');
114
115 // Exceed quota after erasing old reviews, but succeed after prompt.
116 var ls = new MockLocalStorage();
117 ls.localStorageStore = {
118     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
119     'draft-comments-for-attachment-2': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
120     'draft-comments-for-attachment-3': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
121     'draft-comments-for-attachment-4': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}'
122 };
123 var mockDraftSaver = new MockDraftCommentSaver('1234', ls);
124 mockDraftSaver._should_remove_comments = function() { return true; };
125 mockDraftSaver.save();
126 ASSERT_EQUAL(ls.log_string(), 'QuotaExceeded on setItem:draft-comments-for-attachment-1234,{MOCK JSON}\ngetItem:draft-comments-for-attachment-1\ngetItem:draft-comments-for-attachment-2\ngetItem:draft-comments-for-attachment-3\ngetItem:draft-comments-for-attachment-4\nremoveItem:draft-comments-for-attachment-1\nQuotaExceeded on setItem:draft-comments-for-attachment-1234,{MOCK JSON}\nremoveItem:draft-comments-for-attachment-2\nremoveItem:draft-comments-for-attachment-3\nremoveItem:draft-comments-for-attachment-4\nsetItem:draft-comments-for-attachment-1234,{MOCK JSON}');
127
128 // Always exceeds quota, even after erasing all review comments. There should be no setItem calls.
129 var ls = new MockLocalStorage();
130 ls.setItem = function() {
131     this.log.push('QuotaExceeded on setItem:' + key + ',' + value);
132     throw "QuotaExceeded"; 
133 }
134 ls.localStorageStore = {
135     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
136     'draft-comments-for-attachment-2': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
137     'draft-comments-for-attachment-3': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
138     'draft-comments-for-attachment-4': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}'
139 };
140 var mockDraftSaver = new MockDraftCommentSaver('1234', ls);
141 mockDraftSaver._should_remove_comments = function() { return true; };
142 mockDraftSaver.save();
143 // Second save to ensure that we stop trying to save when we fail the prompt.
144 mockDraftSaver.save();
145 ASSERT_EQUAL(ls.log_string(), 'getItem:draft-comments-for-attachment-1\ngetItem:draft-comments-for-attachment-2\ngetItem:draft-comments-for-attachment-3\ngetItem:draft-comments-for-attachment-4\nremoveItem:draft-comments-for-attachment-1\nremoveItem:draft-comments-for-attachment-2\nremoveItem:draft-comments-for-attachment-3\nremoveItem:draft-comments-for-attachment-4');
146
147 var ls = new MockLocalStorage();
148 ls.localStorageStore = {
149     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
150     'draft-comments-for-attachment-2': '{"born-on": 100, "comments":[{"start_line_id": 1, "end_line_id": 2, "contents": "DUMMY CONTENTS"}, {"start_line_id": 3, "end_line_id": 4, "contents": "DUMMY CONTENTS 2"}]}'
151 };
152 var comments = new MockDraftCommentSaver('2', ls).saved_comments().comments;
153 ASSERT_EQUAL(comments.length, 2);
154 ASSERT_EQUAL(ls.log_string(), 'getItem:draft-comments-for-attachment-2');
155
156 var ls = new MockLocalStorage();
157 ls.localStorageStore = {
158     'draft-comments-for-attachment-1': 'corrupt comments'
159 };
160 var comments = new MockDraftCommentSaver('1', ls).saved_comments().comments;
161 ASSERT_EQUAL(ls.log_string(), 'getItem:draft-comments-for-attachment-1\nremoveItem:draft-comments-for-attachment-1');
162
163 var ls = new MockLocalStorage();
164 ls.localStorageStore = {
165     'draft-comments-for-attachment-1': '["also corrupt comments"]'
166 };
167 var comments = new MockDraftCommentSaver('1', ls).saved_comments().comments;
168 ASSERT_EQUAL(ls.log_string(), 'getItem:draft-comments-for-attachment-1\nremoveItem:draft-comments-for-attachment-1');
169
170
171 </script>