520fd955eecf552b1324d4161668c3d254eb9164
[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="code-review.js"></script>
6 <pre id="output"></pre>
7 <script>
8
9 function inherits(childConstructor, parentConstructor) {
10   function tempConstructor() {};
11   tempConstructor.prototype = parentConstructor.prototype;
12   childConstructor.prototype = new tempConstructor();
13   childConstructor.prototype.constructor = childConstructor;
14 }
15
16 function MockLocalStorage() {
17     this.localStorageStore = {};
18     this.log = [];
19
20     this.getItem = function(key) {
21         this.log.push('getItem:' + key);
22         return this.localStorageStore[key];
23     };
24
25     this.setItem = function(key, value) {
26         // For testing sake, consider having more than 2 items to exceed the storage quota.
27         if (Object.keys(this.localStorageStore).length > 2) {
28             this.log.push('QuotaExceeded on setItem:' + key + ',' + value);
29             throw "QuotaExceeded";
30         }
31         this.log.push('setItem:' + key + ',' + value);
32         this.localStorageStore[key] = value;
33     };
34
35     this.removeItem = function(key) {
36         this.log.push('removeItem:' + key);
37         delete this.localStorageStore[key];
38     };
39
40     this.log_string = function() {
41         return this.log.join('\n');
42     };
43
44     this.key = function(i) {
45         return Object.keys(this.localStorageStore)[i];
46     };
47
48     this.__defineGetter__('length', function() {
49         return Object.keys(this.localStorageStore).length;
50     });
51 }
52
53 function MockDraftCommentSaver(attachment_id, opt_localStorage) {
54     DraftCommentSaver.call(this, attachment_id, opt_localStorage);
55 }
56
57 inherits(MockDraftCommentSaver, DraftCommentSaver)
58
59 MockDraftCommentSaver.prototype._json = function() {
60     return "{MOCK JSON}";
61 }
62
63 MockDraftCommentSaver.prototype._should_remove_comments = function(message) {
64     return false;
65 }
66
67 function log(msg) {
68     document.getElementById('output').innerHTML += msg + '\n\n';
69 }
70
71 function ASSERT_EQUAL(actual, expected) {
72     if (actual == expected)
73         log('PASSED');
74     else
75         log('FAILED:\ngot:\n' + actual + '\nexpected:\n' + expected + '');
76 }
77
78 // Basic setItem.
79 var ls = new MockLocalStorage();
80 new MockDraftCommentSaver('1234', ls).save();
81 ASSERT_EQUAL(ls.log_string(), 'setItem:draft-comments-for-attachment-1234,{MOCK JSON}');
82
83 // Exceed quota, but succeed after erasing old reviews.
84 var ls = new MockLocalStorage();
85 ls.localStorageStore = {
86     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
87     'draft-comments-for-attachment-2': '{"born-on": 100, "comments":[]}',
88     'draft-comments-for-attachment-3': '{"born-on": 100, "comments":[]}',
89     'draft-comments-for-attachment-4': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}'
90 };
91 new MockDraftCommentSaver('1234', ls).save();
92 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}');
93
94 // Exceed quota after erasing old reviews and fail after prompt.
95 var ls = new MockLocalStorage();
96 ls.localStorageStore = {
97     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
98     'draft-comments-for-attachment-2': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
99     'draft-comments-for-attachment-3': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
100     'draft-comments-for-attachment-4': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}'
101 };
102 var mockDraftSaver = new MockDraftCommentSaver('1234', ls);
103 mockDraftSaver.save();
104 // Second save to ensure that we stop trying to save when we fail the prompt.
105 mockDraftSaver.save();
106 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}');
107
108 // Exceed quota after erasing old reviews, but succeed after prompt.
109 var ls = new MockLocalStorage();
110 ls.localStorageStore = {
111     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
112     'draft-comments-for-attachment-2': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
113     'draft-comments-for-attachment-3': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
114     'draft-comments-for-attachment-4': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}'
115 };
116 var mockDraftSaver = new MockDraftCommentSaver('1234', ls);
117 mockDraftSaver._should_remove_comments = function() { return true; };
118 mockDraftSaver.save();
119 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}');
120
121 // Always exceeds quota, even after erasing all review comments. There should be no setItem calls.
122 var ls = new MockLocalStorage();
123 ls.setItem = function() {
124     this.log.push('QuotaExceeded on setItem:' + key + ',' + value);
125     throw "QuotaExceeded"; 
126 }
127 ls.localStorageStore = {
128     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
129     'draft-comments-for-attachment-2': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
130     'draft-comments-for-attachment-3': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}',
131     'draft-comments-for-attachment-4': '{"born-on": ' + (Date.now() - 100) + ', "comments":[]}'
132 };
133 var mockDraftSaver = new MockDraftCommentSaver('1234', ls);
134 mockDraftSaver._should_remove_comments = function() { return true; };
135 mockDraftSaver.save();
136 // Second save to ensure that we stop trying to save when we fail the prompt.
137 mockDraftSaver.save();
138 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');
139
140 var ls = new MockLocalStorage();
141 ls.localStorageStore = {
142     'draft-comments-for-attachment-1': '{"born-on": 100, "comments":[]}',
143     '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"}]}'
144 };
145 var comments = new MockDraftCommentSaver('2', ls).saved_comments().comments;
146 ASSERT_EQUAL(comments.length, 2);
147 ASSERT_EQUAL(ls.log_string(), 'getItem:draft-comments-for-attachment-2');
148
149 var ls = new MockLocalStorage();
150 ls.localStorageStore = {
151     'draft-comments-for-attachment-1': 'corrupt comments'
152 };
153 var comments = new MockDraftCommentSaver('1', ls).saved_comments().comments;
154 ASSERT_EQUAL(ls.log_string(), 'getItem:draft-comments-for-attachment-1\nremoveItem:draft-comments-for-attachment-1');
155
156 var ls = new MockLocalStorage();
157 ls.localStorageStore = {
158     'draft-comments-for-attachment-1': '["also 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
164 </script>