Debian Codegraph @

addons/test_website/static/tests/tours/image_upload_progress.js

raw
1 import { insertSnippet, registerWebsitePreviewTour } from "@website/js/tours/tour_utils";
2
3 import { FileSelectorControlPanel } from "@html_editor/main/media/media_dialog/file_selector";
4 import { patch } from "@web/core/utils/patch";
5
6 let patchWithError = false;
7 const patchMediaDialog = () => patch(FileSelectorControlPanel.prototype, {
8 async onChangeFileInput() {
9 const getFileFromB64 = (fileData) => {
10 const binary = atob(fileData[2]);
11 let len = binary.length;
12 const arr = new Uint8Array(len);
13 while (len--) {
14 arr[len] = binary.charCodeAt(len);
15 }
16 return new File([arr], fileData[1], {type: fileData[0]});
17 };
18
19 let files = [
20 getFileFromB64(['image/vnd.microsoft.icon', 'icon.ico', "AAABAAEAAQEAAAEAIAAwAAAAFgAAACgAAAABAAAAAgAAAAEAIAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA=="]),
21 getFileFromB64(['image/webp', 'image.webp', "UklGRhwAAABXRUJQVlA4TBAAAAAvE8AEAAfQhuh//wMR0f8A"]),
22 getFileFromB64(['image/png', 'image.png', "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAApElEQVR42u3RAQ0AAAjDMO5fNCCDkC5z0HTVrisFCBABASIgQAQEiIAAAQJEQIAICBABASIgQAREQIAICBABASIgQAREQIAICBABASIgQAREQIAICBABASIgQAREQIAICBABASIgQAREQIAICBABASIgQAREQIAICBABASIgQAREQIAICBABASIgQAREQIAICBABASIgQAQECBAgAgJEQIAIyPcGFY7HnV2aPXoAAAAASUVORK5CYII="]),
23 getFileFromB64(['image/jpeg', 'image.jpeg', "/9j/4AAQSkZJRgABAQAAAQABAAD//gAfQ29tcHJlc3NlZCBieSBqcGVnLXJlY29tcHJlc3P/2wCEAA0NDQ0ODQ4QEA4UFhMWFB4bGRkbHi0gIiAiIC1EKjIqKjIqRDxJOzc7STxsVUtLVWx9aWNpfZeHh5e+tb75+f8BDQ0NDQ4NDhAQDhQWExYUHhsZGRseLSAiICIgLUQqMioqMipEPEk7NztJPGxVS0tVbH1pY2l9l4eHl761vvn5///CABEIAEsASwMBIgACEQEDEQH/xAAVAAEBAAAAAAAAAAAAAAAAAAAABv/aAAgBAQAAAACHAAAAAAAAAAAAAAAAH//EABUBAQEAAAAAAAAAAAAAAAAAAAAH/9oACAECEAAAAKYAAAB//8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/2gAIAQMQAAAAngAAAf/EABQQAQAAAAAAAAAAAAAAAAAAAGD/2gAIAQEAAT8ASf/EABQRAQAAAAAAAAAAAAAAAAAAAED/2gAIAQIBAT8AT//EABQRAQAAAAAAAAAAAAAAAAAAAED/2gAIAQMBAT8AT//Z"]),
24 ];
25
26 if (!this.props.multiSelect) {
27 if (patchWithError) {
28 files = [files[0]];
29 } else {
30 files = [files[2]];
31 }
32 }
33 await this.props.uploadFiles(files);
34 }
35 });
36
37 let unpatchMediaDialog = null;
38
39 const setupSteps = function () {
40 return [
41 {
42 content: "reload to load patch",
43 trigger: ".o_website_preview",
44 run() {
45 unpatchMediaDialog = patchMediaDialog();
46 },
47 },
48 ...insertSnippet({
49 id: "s_text_image",
50 name: "Text - Image",
51 groupName: "Content",
52 }),
53 ...insertSnippet({
54 id: "s_image_gallery",
55 name: "Image Gallery",
56 groupName: "Images",
57 })
58 ];
59 };
60
61 const formatErrorMsg = "format is not supported. Try with: .gif, .jpe, .jpeg, .jpg, .png, .svg, .webp";
62
63 registerWebsitePreviewTour('test_image_upload_progress', {
64 url: '/test_image_progress',
65 edition: true,
66 }, () => [
67 ...setupSteps(),
68 // 1. Check multi image upload
69 {
70 content: "click on dropped snippet",
71 trigger: ":iframe #wrap .s_image_gallery .img",
72 run: "click",
73 }, {
74 content: "click on add images to open image dialog (in multi mode)",
75 trigger: "button[data-action-id='addImage']",
76 run: "click",
77 }, {
78 content: "manually trigger input change",
79 trigger: ".o_select_media_dialog .o_upload_media_button",
80 run() {
81 // This will trigger upload of dummy files for test purpose, as a
82 // test can't select local files to upload into the input.
83 document.body.querySelector('.o_select_media_dialog .o_file_input').dispatchEvent(new Event('change'));
84 },
85 }, {
86 content: "check upload progress bar is correctly shown (1)",
87 trigger: `.o_we_progressbar:contains('icon.ico'):contains('${formatErrorMsg}')`,
88 }, {
89 content: "check upload progress bar is correctly shown (2)",
90 trigger: ".o_we_progressbar:contains('image.webp'):contains('File has been uploaded')",
91 }, {
92 content: "check upload progress bar is correctly shown (3)",
93 trigger: ".o_we_progressbar:contains('image.png'):contains('File has been uploaded')",
94 }, {
95 content: "check upload progress bar is correctly shown (4)",
96 trigger: ".o_we_progressbar:contains('image.jpeg'):contains('File has been uploaded')",
97 },
98 {
99 trigger: ".o_notification",
100 },
101 {
102 content: "there should only have one notification toaster",
103 trigger: "body",
104 run() {
105 const notificationCount = document.querySelectorAll(".o_notification").length;
106 if (notificationCount !== 1) {
107 throw new Error(`There should be one notification toaster opened, and only one, found ${notificationCount}.`);
108 }
109 }
110 }, {
111 content: "close notification",
112 trigger: '.o_notification_close',
113 run: "click",
114 }, {
115 content: "close media dialog",
116 trigger: '.modal-footer .btn-secondary',
117 run: "click",
118 },
119 // 2. Check success single image upload
120 {
121 content: "click on dropped snippet",
122 trigger: ":iframe #wrap .s_text_image .img",
123 run: "click",
124 }, {
125 content: "click on replace media to open image dialog",
126 trigger: "button[data-action-id='replaceMedia']",
127 run: "click",
128 }, {
129 content: "manually trigger input change",
130 trigger: ".o_select_media_dialog .o_upload_media_button",
131 run() {
132 // This will trigger upload of dummy files for test purpose, as a
133 // test can't select local files to upload into the input.
134 document.body.querySelector('.o_select_media_dialog .o_file_input').dispatchEvent(new Event('change'));
135 },
136 }, {
137 content: "check upload progress bar is correctly shown",
138 trigger: ".o_we_progressbar:contains('image.png')",
139 }, {
140 content: "there should only have one notification toaster",
141 trigger: ".o_notification",
142 run() {
143 const notificationCount = document.querySelectorAll(".o_notification").length;
144 if (notificationCount !== 1) {
145 throw new Error(`There should be one notification toaster opened, and only one, found ${notificationCount}.`);
146 }
147 }
148 }, {
149 content: "media dialog has closed after the upload",
150 trigger: 'body:not(:has(.o_select_media_dialog))',
151 }, {
152 content: "the upload progress toast was updated",
153 trigger: ".o_we_progressbar:contains('image.png'):contains('File has been uploaded')",
154 }, {
155 content: "toaster should disappear after a few seconds if the uploaded image is successful",
156 trigger: "body:not(:has(.o_we_progressbar))",
157 },
158 // 3. Check error single image upload
159 {
160 content: "click on dropped snippet",
161 trigger: ":iframe #wrap .s_text_image .img",
162 run: "click",
163 }, {
164 content: "click on replace media to open image dialog",
165 trigger: "button[data-action-id='replaceMedia']",
166 run: "click",
167 }, {
168 content: "manually trigger input change",
169 trigger: ".o_select_media_dialog .o_upload_media_button",
170 run() {
171 patchWithError = true;
172 // This will trigger upload of dummy files for test purpose, as a
173 // test can't select local files to upload into the input.
174 document.body.querySelector('.o_select_media_dialog .o_file_input').dispatchEvent(new Event('change'));
175
176 },
177 }, {
178 content: "check upload progress bar is correctly shown",
179 trigger: `.o_we_progressbar:contains('icon.ico'):contains('${formatErrorMsg}')`,
180 run() {
181 patchWithError = false;
182 },
183 }, {
184 content: "there should only have one notification toaster",
185 trigger: ".o_notification",
186 run() {
187 const notificationCount = document.querySelectorAll(".o_notification").length;
188 if (notificationCount !== 1) {
189 throw new Error(`There should be one noficiation toaster opened, and only one, found ${notificationCount}.`);
190 }
191 unpatchMediaDialog();
192 }
193 },
194 ]);
195
196
197 registerWebsitePreviewTour('test_image_upload_progress_unsplash', {
198 url: '/test_image_progress',
199 edition: true,
200 }, () => [
201 ...setupSteps(),
202 // 1. Check multi image upload
203 {
204 content: "click on dropped snippet",
205 trigger: ":iframe #wrap .s_image_gallery .img",
206 run: "click",
207 }, {
208 content: "click on replace media to open image dialog",
209 trigger: "button[data-action-id='replaceMedia']",
210 run: "click",
211 }, {
212 content: "search 'fox' images",
213 trigger: ".o_we_search",
214 run: "edit fox",
215 }, {
216 content: "click on unsplash result", // note that unsplash is mocked
217 trigger: ".o_we_media_dialog_img_wrapper:has(img[alt~=fox]) + .o_button_area",
218 run: "click",
219 },
220 {
221 trigger: ".o_notification_close",
222 },
223 {
224 content: "check that the upload progress bar is correctly shown",
225 // ensure it is there so we are sure next step actually test something
226 trigger: ".o_we_progressbar:contains('fox'):contains('File has been uploaded')",
227 }, {
228 content: "notification should close after 3 seconds",
229 trigger: 'body:not(:has(.o_notification_close))',
230 run: "click",
231 }, {
232 content: "unsplash image (mocked to logo) should have been used",
233 trigger: ":iframe #wrap .s_image_gallery img[data-original-src^='/unsplash/HQqIOc8oYro/fox']",
234 run() {
235 unpatchMediaDialog();
236 },
237 },
238 ]);