Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 3 | @license |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 4 | Copyright (C) 2016 The Android Open Source Project |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations 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 Rehmsen | 6290935 | 2019-05-16 16:10:33 +0200 | [diff] [blame] | 20 | <script src="/test/common-test-setup.js"></script> |
| 21 | <script src="/bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script> |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 22 | |
Ole Rehmsen | ecf0b78 | 2019-05-16 11:29:39 +0200 | [diff] [blame] | 23 | <script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script> |
Ole Rehmsen | 3164074 | 2019-05-16 11:24:47 +0200 | [diff] [blame] | 24 | <script src="/bower_components/web-component-tester/browser.js"></script> |
Mike Samuel | e07c4b2 | 2017-06-02 13:08:19 -0400 | [diff] [blame] | 25 | <link rel="import" href="../../../test/common-test-setup.html"/> |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 26 | <link rel="import" href="gr-storage.html"> |
| 27 | |
Viktar Donich | 29e1ce5 | 2017-03-28 17:02:44 -0700 | [diff] [blame] | 28 | <script>void(0);</script> |
| 29 | |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 30 | <test-fixture id="basic"> |
| 31 | <template> |
| 32 | <gr-storage></gr-storage> |
| 33 | </template> |
| 34 | </test-fixture> |
| 35 | |
| 36 | <script> |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 37 | suite('gr-storage tests', () => { |
| 38 | let element; |
Kasper Nilsson | 18623b4 | 2017-11-14 11:40:42 -0800 | [diff] [blame] | 39 | let sandbox; |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 40 | |
Wyatt Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 41 | function mockStorage(opt_quotaExceeded) { |
| 42 | return { |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 43 | getItem(key) { return this[key]; }, |
| 44 | removeItem(key) { delete this[key]; }, |
| 45 | setItem(key, value) { |
| 46 | // eslint-disable-next-line no-throw-literal |
Wyatt Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 47 | if (opt_quotaExceeded) { throw {code: 22}; /* Quota exceeded */ } |
| 48 | this[key] = value; |
| 49 | }, |
| 50 | }; |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 53 | setup(() => { |
Urs Wolfer | 33df005 | 2016-07-13 21:06:03 +0200 | [diff] [blame] | 54 | element = fixture('basic'); |
Kasper Nilsson | 18623b4 | 2017-11-14 11:40:42 -0800 | [diff] [blame] | 55 | sandbox = sinon.sandbox.create(); |
Wyatt Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 56 | element._storage = mockStorage(); |
Urs Wolfer | 33df005 | 2016-07-13 21:06:03 +0200 | [diff] [blame] | 57 | }); |
| 58 | |
Kasper Nilsson | 18623b4 | 2017-11-14 11:40:42 -0800 | [diff] [blame] | 59 | teardown(() => sandbox.restore()); |
| 60 | |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 61 | 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 Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 71 | }; |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 72 | |
| 73 | // The key is in the expected format. |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 74 | const key = element._getDraftKey(location); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 75 | assert.equal(key, ['draft', changeNum, patchNum, path, line].join(':')); |
| 76 | |
| 77 | // There should be no draft initially. |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 78 | const draft = element.getDraftComment(location); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 79 | assert.isNotOk(draft); |
| 80 | |
| 81 | // Setting the draft stores it under the expected key. |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 82 | element.setDraftComment(location, 'my comment'); |
Wyatt Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 83 | 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 Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 87 | |
| 88 | // Erasing the draft removes the key. |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 89 | element.eraseDraftComment(location); |
Wyatt Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 90 | assert.isNotOk(element._storage.getItem(key)); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 91 | }); |
| 92 | |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 93 | 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 Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 103 | }; |
Becky Siegel | 64ce59f | 2017-01-31 16:57:14 -0800 | [diff] [blame] | 104 | |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 105 | const key = element._getDraftKey(location); |
Wyatt Allen | 035c74f | 2016-05-23 13:53:10 -0700 | [diff] [blame] | 106 | |
| 107 | // Make sure that the call to cleanup doesn't get throttled. |
| 108 | element._lastCleanup = 0; |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 109 | |
Kasper Nilsson | 18623b4 | 2017-11-14 11:40:42 -0800 | [diff] [blame] | 110 | const cleanupSpy = sandbox.spy(element, '_cleanupItems'); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 111 | |
| 112 | // Create a message with a timestamp that is a second behind the max age. |
Wyatt Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 113 | element._storage.setItem(key, JSON.stringify({ |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 114 | message: 'old message', |
Logan Hanks | d2497fb | 2016-06-15 11:57:36 -0700 | [diff] [blame] | 115 | updated: Date.now() - 24 * 60 * 60 * 1000 - 1000, |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 116 | })); |
| 117 | |
| 118 | // Getting the draft should cause it to be removed. |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 119 | const draft = element.getDraftComment(location); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 120 | |
| 121 | assert.isTrue(cleanupSpy.called); |
| 122 | assert.isNotOk(draft); |
Wyatt Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 123 | assert.isNotOk(element._storage.getItem(key)); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 124 | }); |
Becky Siegel | 64ce59f | 2017-01-31 16:57:14 -0800 | [diff] [blame] | 125 | |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 126 | 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 Siegel | 64ce59f | 2017-01-31 16:57:14 -0800 | [diff] [blame] | 136 | }; |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 137 | let expectedResult = 'draft:1234:5:my_source_file.js:123'; |
Becky Siegel | 64ce59f | 2017-01-31 16:57:14 -0800 | [diff] [blame] | 138 | 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 Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 148 | |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 149 | test('exceeded quota disables storage', () => { |
Wyatt Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 150 | element._storage = mockStorage(true); |
| 151 | assert.isFalse(element._exceededQuota); |
| 152 | |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 153 | 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 Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 162 | }; |
Kasper Nilsson | 7eab7db | 2017-05-15 16:55:52 -0700 | [diff] [blame] | 163 | const key = element._getDraftKey(location); |
Wyatt Allen | 66e9687 | 2017-03-17 10:20:38 -0700 | [diff] [blame] | 164 | element.setDraftComment(location, 'my comment'); |
| 165 | assert.isTrue(element._exceededQuota); |
| 166 | assert.isNotOk(element._storage.getItem(key)); |
| 167 | }); |
Kasper Nilsson | 18623b4 | 2017-11-14 11:40:42 -0800 | [diff] [blame] | 168 | |
| 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 Nilsson | 244fb18 | 2017-12-06 17:10:57 -0800 | [diff] [blame] | 190 | |
| 191 | // eraseEditableContentItem performs as expected. |
| 192 | element.eraseEditableContentItem(key); |
Paladox none | 621d8b3 | 2019-01-24 01:26:49 +0000 | [diff] [blame] | 193 | assert.isNotOk(element._storage.getItem(computedKey)); |
Kasper Nilsson | 18623b4 | 2017-11-14 11:40:42 -0800 | [diff] [blame] | 194 | }); |
Wyatt Allen | 7a4aa8c | 2016-05-18 12:37:53 -0700 | [diff] [blame] | 195 | }); |
| 196 | </script> |