blob: 0482584fa3eda9750fa9253ad53265c1cbf11845 [file] [log] [blame]
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -07001<!DOCTYPE html>
2<!--
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04003@license
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -07004Copyright (C) 2016 The Android Open Source Project
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17-->
18<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
19<title>gr-storage</title>
Ole Rehmsen62909352019-05-16 16:10:33 +020020<script src="/test/common-test-setup.js"></script>
21<script src="/bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script>
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070022
Ole Rehmsenecf0b782019-05-16 11:29:39 +020023<script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
Ole Rehmsen31640742019-05-16 11:24:47 +020024<script src="/bower_components/web-component-tester/browser.js"></script>
Mike Samuele07c4b22017-06-02 13:08:19 -040025<link rel="import" href="../../../test/common-test-setup.html"/>
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070026<link rel="import" href="gr-storage.html">
27
Viktar Donich29e1ce52017-03-28 17:02:44 -070028<script>void(0);</script>
29
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070030<test-fixture id="basic">
31 <template>
32 <gr-storage></gr-storage>
33 </template>
34</test-fixture>
35
36<script>
Kasper Nilsson7eab7db2017-05-15 16:55:52 -070037 suite('gr-storage tests', () => {
38 let element;
Kasper Nilsson18623b42017-11-14 11:40:42 -080039 let sandbox;
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070040
Wyatt Allen66e96872017-03-17 10:20:38 -070041 function mockStorage(opt_quotaExceeded) {
42 return {
Kasper Nilsson7eab7db2017-05-15 16:55:52 -070043 getItem(key) { return this[key]; },
44 removeItem(key) { delete this[key]; },
45 setItem(key, value) {
46 // eslint-disable-next-line no-throw-literal
Wyatt Allen66e96872017-03-17 10:20:38 -070047 if (opt_quotaExceeded) { throw {code: 22}; /* Quota exceeded */ }
48 this[key] = value;
49 },
50 };
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070051 }
52
Kasper Nilsson7eab7db2017-05-15 16:55:52 -070053 setup(() => {
Urs Wolfer33df0052016-07-13 21:06:03 +020054 element = fixture('basic');
Kasper Nilsson18623b42017-11-14 11:40:42 -080055 sandbox = sinon.sandbox.create();
Wyatt Allen66e96872017-03-17 10:20:38 -070056 element._storage = mockStorage();
Urs Wolfer33df0052016-07-13 21:06:03 +020057 });
58
Kasper Nilsson18623b42017-11-14 11:40:42 -080059 teardown(() => sandbox.restore());
60
Kasper Nilsson7eab7db2017-05-15 16:55:52 -070061 test('storing, retrieving and erasing drafts', () => {
62 const changeNum = 1234;
63 const patchNum = 5;
64 const path = 'my_source_file.js';
65 const line = 123;
66 const location = {
67 changeNum,
68 patchNum,
69 path,
70 line,
Wyatt Allen035c74f2016-05-23 13:53:10 -070071 };
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070072
73 // The key is in the expected format.
Kasper Nilsson7eab7db2017-05-15 16:55:52 -070074 const key = element._getDraftKey(location);
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070075 assert.equal(key, ['draft', changeNum, patchNum, path, line].join(':'));
76
77 // There should be no draft initially.
Kasper Nilsson7eab7db2017-05-15 16:55:52 -070078 const draft = element.getDraftComment(location);
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070079 assert.isNotOk(draft);
80
81 // Setting the draft stores it under the expected key.
Wyatt Allen035c74f2016-05-23 13:53:10 -070082 element.setDraftComment(location, 'my comment');
Wyatt Allen66e96872017-03-17 10:20:38 -070083 assert.isOk(element._storage.getItem(key));
84 assert.equal(JSON.parse(element._storage.getItem(key)).message,
85 'my comment');
86 assert.isOk(JSON.parse(element._storage.getItem(key)).updated);
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070087
88 // Erasing the draft removes the key.
Wyatt Allen035c74f2016-05-23 13:53:10 -070089 element.eraseDraftComment(location);
Wyatt Allen66e96872017-03-17 10:20:38 -070090 assert.isNotOk(element._storage.getItem(key));
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -070091 });
92
Kasper Nilsson7eab7db2017-05-15 16:55:52 -070093 test('automatically removes old drafts', () => {
94 const changeNum = 1234;
95 const patchNum = 5;
96 const path = 'my_source_file.js';
97 const line = 123;
98 const location = {
99 changeNum,
100 patchNum,
101 path,
102 line,
Wyatt Allen035c74f2016-05-23 13:53:10 -0700103 };
Becky Siegel64ce59f2017-01-31 16:57:14 -0800104
Kasper Nilsson7eab7db2017-05-15 16:55:52 -0700105 const key = element._getDraftKey(location);
Wyatt Allen035c74f2016-05-23 13:53:10 -0700106
107 // Make sure that the call to cleanup doesn't get throttled.
108 element._lastCleanup = 0;
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -0700109
Kasper Nilsson18623b42017-11-14 11:40:42 -0800110 const cleanupSpy = sandbox.spy(element, '_cleanupItems');
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -0700111
112 // Create a message with a timestamp that is a second behind the max age.
Wyatt Allen66e96872017-03-17 10:20:38 -0700113 element._storage.setItem(key, JSON.stringify({
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -0700114 message: 'old message',
Logan Hanksd2497fb2016-06-15 11:57:36 -0700115 updated: Date.now() - 24 * 60 * 60 * 1000 - 1000,
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -0700116 }));
117
118 // Getting the draft should cause it to be removed.
Kasper Nilsson7eab7db2017-05-15 16:55:52 -0700119 const draft = element.getDraftComment(location);
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -0700120
121 assert.isTrue(cleanupSpy.called);
122 assert.isNotOk(draft);
Wyatt Allen66e96872017-03-17 10:20:38 -0700123 assert.isNotOk(element._storage.getItem(key));
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -0700124 });
Becky Siegel64ce59f2017-01-31 16:57:14 -0800125
Kasper Nilsson7eab7db2017-05-15 16:55:52 -0700126 test('_getDraftKey', () => {
127 const changeNum = 1234;
128 const patchNum = 5;
129 const path = 'my_source_file.js';
130 const line = 123;
131 const location = {
132 changeNum,
133 patchNum,
134 path,
135 line,
Becky Siegel64ce59f2017-01-31 16:57:14 -0800136 };
Kasper Nilsson7eab7db2017-05-15 16:55:52 -0700137 let expectedResult = 'draft:1234:5:my_source_file.js:123';
Becky Siegel64ce59f2017-01-31 16:57:14 -0800138 assert.equal(element._getDraftKey(location), expectedResult);
139 location.range = {
140 start_character: 1,
141 start_line: 1,
142 end_character: 1,
143 end_line: 2,
144 };
145 expectedResult = 'draft:1234:5:my_source_file.js:123:1-1-1-2';
146 assert.equal(element._getDraftKey(location), expectedResult);
147 });
Wyatt Allen66e96872017-03-17 10:20:38 -0700148
Kasper Nilsson7eab7db2017-05-15 16:55:52 -0700149 test('exceeded quota disables storage', () => {
Wyatt Allen66e96872017-03-17 10:20:38 -0700150 element._storage = mockStorage(true);
151 assert.isFalse(element._exceededQuota);
152
Kasper Nilsson7eab7db2017-05-15 16:55:52 -0700153 const changeNum = 1234;
154 const patchNum = 5;
155 const path = 'my_source_file.js';
156 const line = 123;
157 const location = {
158 changeNum,
159 patchNum,
160 path,
161 line,
Wyatt Allen66e96872017-03-17 10:20:38 -0700162 };
Kasper Nilsson7eab7db2017-05-15 16:55:52 -0700163 const key = element._getDraftKey(location);
Wyatt Allen66e96872017-03-17 10:20:38 -0700164 element.setDraftComment(location, 'my comment');
165 assert.isTrue(element._exceededQuota);
166 assert.isNotOk(element._storage.getItem(key));
167 });
Kasper Nilsson18623b42017-11-14 11:40:42 -0800168
169 test('editable content items', () => {
170 const cleanupStub = sandbox.stub(element, '_cleanupItems');
171 const key = 'testKey';
172 const computedKey = element._getEditableContentKey(key);
173 // Key correctly computed.
174 assert.equal(computedKey, 'editablecontent:testKey');
175
176 element.setEditableContentItem(key, 'my content');
177
178 // Setting the draft stores it under the expected key.
179 let item = element._storage.getItem(computedKey);
180 assert.isOk(item);
181 assert.equal(JSON.parse(item).message, 'my content');
182 assert.isOk(JSON.parse(item).updated);
183
184 // getEditableContentItem performs as expected.
185 item = element.getEditableContentItem(key);
186 assert.isOk(item);
187 assert.equal(item.message, 'my content');
188 assert.isOk(item.updated);
189 assert.isTrue(cleanupStub.called);
Kasper Nilsson244fb182017-12-06 17:10:57 -0800190
191 // eraseEditableContentItem performs as expected.
192 element.eraseEditableContentItem(key);
Paladox none621d8b32019-01-24 01:26:49 +0000193 assert.isNotOk(element._storage.getItem(computedKey));
Kasper Nilsson18623b42017-11-14 11:40:42 -0800194 });
Wyatt Allen7a4aa8c2016-05-18 12:37:53 -0700195 });
196</script>