1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "image_animator_tdd_test.h"
17 
18 #include "js_app_context.h"
19 #include "ui_image_animator.h"
20 #include "ui_view_group.h"
21 
22 namespace OHOS {
23 namespace ACELite {
24 const char * const BUNDLE01 =
25     "(function () {\n"
26     "  return new ViewModel({\n"
27     "    render: function render(vm) {\n"
28     "      var _vm = vm || this;\n"
29     "      return _c('div', {\n"
30     "        staticClass: ['container']\n"
31     "      }, [_c('image-animator', {\n"
32     "        staticClass: ['animator'],\n"
33     "        attrs: {\n"
34     "          ref: 'animator',\n"
35     "          images: function images() {\n"
36     "            return _vm.images;\n"
37     "          },\n"
38     "          duration: '1s'\n"
39     "        }\n"
40     "      })]);\n"
41     "    },\n"
42     "    styleSheet: {\n"
43     "      classSelectors: {\n"
44     "        container: {\n"
45     "          flexDirection: 'column',\n"
46     "          justifyContent: 'center',\n"
47     "          alignItems: 'center',\n"
48     "          height: '100%',\n"
49     "          width: '100%'\n"
50     "        },\n"
51     "        animator: {\n"
52     "          height: '80px',\n"
53     "          width: '80px'\n"
54     "        }\n"
55     "      }\n"
56     "    },\n"
57     "    data: {\n"
58     "      images: [{\n"
59     "        src: 'common/asserts/numbers/0.png'\n"
60     "      }, {\n"
61     "        src: 'common/asserts/numbers/1.png'\n"
62     "      }, {\n"
63     "        src: 'common/asserts/numbers/2.png'\n"
64     "      }, {\n"
65     "        src: 'common/asserts/numbers/3.png'\n"
66     "      }, {\n"
67     "        src: 'common/asserts/numbers/4.png'\n"
68     "      }, {\n"
69     "        src: 'common/asserts/numbers/5.png'\n"
70     "      }, {\n"
71     "        src: 'common/asserts/numbers/6.png'\n"
72     "      }, {\n"
73     "        src: 'common/asserts/numbers/7.png'\n"
74     "      }, {\n"
75     "        src: 'common/asserts/numbers/8.png'\n"
76     "      }, {\n"
77     "        src: 'common/asserts/numbers/9.png'\n"
78     "      }]\n"
79     "    }\n"
80     "  });\n"
81     "})();\n";
82 
83 const char * const BUNDLE02 =
84     "(function () {\n"
85     "  return new ViewModel({\n"
86     "    render: function render(vm) {\n"
87     "      var _vm = vm || this;\n"
88     "      return _c('div', {\n"
89     "        staticClass: ['container']\n"
90     "      }, [_c('image-animator', {\n"
91     "        staticClass: ['animator'],\n"
92     "        attrs: {\n"
93     "          ref: 'animator',\n"
94     "          images: function images() {\n"
95     "            return _vm.images;\n"
96     "          },\n"
97     "          duration: '1s',\n"
98     "          iteration: 3,\n"
99     "          reverse: true,\n"
100     "          fixedsize: false,\n"
101     "          fillmode: 'none'\n"
102     "        }\n"
103     "      })]);\n"
104     "    },\n"
105     "    styleSheet: {\n"
106     "      classSelectors: {\n"
107     "        container: {\n"
108     "          flexDirection: 'column',\n"
109     "          justifyContent: 'center',\n"
110     "          alignItems: 'center',\n"
111     "          height: '100%',\n"
112     "          width: '100%'\n"
113     "        },\n"
114     "        animator: {\n"
115     "          height: '100px',\n"
116     "          width: '100px'\n"
117     "        }\n"
118     "      }\n"
119     "    },\n"
120     "    data: {\n"
121     "      images: [{\n"
122     "        src: 'common/asserts/numbers/0.png',\n"
123     "        width: 80,\n"
124     "        height: 80,\n"
125     "        left: 10,\n"
126     "        right: 10\n"
127     "      }, {\n"
128     "        src: 'common/asserts/numbers/1.png',\n"
129     "        width: 80,\n"
130     "        height: 80,\n"
131     "        left: 10,\n"
132     "        right: 10\n"
133     "      }, {\n"
134     "        src: 'common/asserts/numbers/2.png',\n"
135     "        width: 80,\n"
136     "        height: 80,\n"
137     "        left: 10,\n"
138     "        right: 10\n"
139     "      }, {\n"
140     "        src: 'common/asserts/numbers/3.png',\n"
141     "        width: 80,\n"
142     "        height: 80,\n"
143     "        left: 10,\n"
144     "        right: 10\n"
145     "      }, {\n"
146     "        src: 'common/asserts/numbers/4.png',\n"
147     "        width: 80,\n"
148     "        height: 80,\n"
149     "        left: 10,\n"
150     "        right: 10\n"
151     "      }, {\n"
152     "        src: 'common/asserts/numbers/5.png',\n"
153     "        width: 80,\n"
154     "        height: 80,\n"
155     "        left: 10,\n"
156     "        right: 10\n"
157     "      }, {\n"
158     "        src: 'common/asserts/numbers/6.png',\n"
159     "        width: 80,\n"
160     "        height: 80,\n"
161     "        left: 10,\n"
162     "        right: 10\n"
163     "      }, {\n"
164     "        src: 'common/asserts/numbers/7.png',\n"
165     "        width: 80,\n"
166     "        height: 80,\n"
167     "        left: 10,\n"
168     "        right: 10\n"
169     "      }, {\n"
170     "        src: 'common/asserts/numbers/8.png',\n"
171     "        width: 80,\n"
172     "        height: 80,\n"
173     "        left: 10,\n"
174     "        right: 10\n"
175     "      }, {\n"
176     "        src: 'common/asserts/numbers/9.png',\n"
177     "        width: 80,\n"
178     "        height: 80,\n"
179     "        left: 10,\n"
180     "        right: 10\n"
181     "      }]\n"
182     "    }\n"
183     "  });\n"
184     "})();\n";
185 
186 const char * const BUNDLE03 =
187     "(function () {\n"
188     "  return new ViewModel({\n"
189     "    render: function render(vm) {\n"
190     "      var _vm = vm || this;\n"
191     "      return _c('div', {\n"
192     "        staticClass: ['container']\n"
193     "      }, [_c('image-animator', {\n"
194     "        staticClass: ['animator'],\n"
195     "        attrs: {\n"
196     "          ref: 'animator',\n"
197     "          images: function images() {\n"
198     "            return _vm.images;\n"
199     "          },\n"
200     "          duration: '1s'\n"
201     "        },\n"
202     "        on: {\n"
203     "          stop: _vm.handleStop\n"
204     "        }\n"
205     "      })]);\n"
206     "    },\n"
207     "    styleSheet: {\n"
208     "      classSelectors: {\n"
209     "        container: {\n"
210     "          flexDirection: 'column',\n"
211     "          justifyContent: 'center',\n"
212     "          alignItems: 'center',\n"
213     "          height: '100%',\n"
214     "          width: '100%'\n"
215     "        },\n"
216     "        animator: {\n"
217     "          height: '80px',\n"
218     "          width: '80px'\n"
219     "        }\n"
220     "      }\n"
221     "    },\n"
222     "    data: {\n"
223     "      stopped: false,\n"
224     "      images: [{\n"
225     "        src: 'common/asserts/numbers/0.png'\n"
226     "      }, {\n"
227     "        src: 'common/asserts/numbers/1.png'\n"
228     "      }, {\n"
229     "        src: 'common/asserts/numbers/2.png'\n"
230     "      }, {\n"
231     "        src: 'common/asserts/numbers/3.png'\n"
232     "      }, {\n"
233     "        src: 'common/asserts/numbers/4.png'\n"
234     "      }, {\n"
235     "        src: 'common/asserts/numbers/5.png'\n"
236     "      }, {\n"
237     "        src: 'common/asserts/numbers/6.png'\n"
238     "      }, {\n"
239     "        src: 'common/asserts/numbers/7.png'\n"
240     "      }, {\n"
241     "        src: 'common/asserts/numbers/8.png'\n"
242     "      }, {\n"
243     "        src: 'common/asserts/numbers/9.png'\n"
244     "      }]\n"
245     "    },\n"
246     "    start: function start() {\n"
247     "      this.$refs.animator.start();\n"
248     "    },\n"
249     "    stop: function stop() {\n"
250     "      this.$refs.animator.stop();\n"
251     "    },\n"
252     "    pause: function pause() {\n"
253     "      this.$refs.animator.pause();\n"
254     "    },\n"
255     "    resume: function resume() {\n"
256     "      this.$refs.animator.resume();\n"
257     "    },\n"
258     "    getState: function getState() {\n"
259     "      return this.$refs.animator.getState();\n"
260     "    },\n"
261     "    handleStop: function handleStop() {\n"
262     "      this.stopped = true;\n"
263     "    }\n"
264     "  });\n"
265     "})();\n";
266 
RunTests()267 void ImageAnimatorTddTest::RunTests()
268 {
269     ImageAnimatorTest001();
270     ImageAnimatorTest002();
271     ImageAnimatorTest003();
272 }
273 
ImageAnimatorTest001()274 void ImageAnimatorTddTest::ImageAnimatorTest001()
275 {
276     TDD_CASE_BEGIN();
277     JsAppContext::GetInstance()->SetCurrentAbilityInfo("app/run/", "com.example.test", 0);
278     JSValue page = CreatePage(BUNDLE01, strlen(BUNDLE01));
279     UIViewGroup *wrapper = reinterpret_cast<UIViewGroup *>(GetViewByRef(page, "animator"));
280     EXPECT_TRUE(wrapper != nullptr);
281     if (wrapper != nullptr) {
282         UIImageAnimatorView *animator = reinterpret_cast<UIImageAnimatorView *>(wrapper->GetChildrenHead());
283         EXPECT_TRUE(animator != nullptr);
284         if (animator != nullptr) {
285             const int16_t wrapperSize = 80;
286             const uint8_t imagseSize = 10;
287             EXPECT_EQ(wrapperSize, wrapper->GetWidth());
288             EXPECT_EQ(wrapperSize, wrapper->GetHeight());
289             EXPECT_EQ(imagseSize, animator->GetImageAnimatorImageNum());
290             EXPECT_TRUE(animator->IsRepeat());
291             EXPECT_FALSE(animator->IsReverse());
292             EXPECT_TRUE(animator->IsSizeFixed());
293             EXPECT_TRUE(animator->GetFillMode());
294         }
295     }
296     DestroyPage(page);
297     TDD_CASE_END();
298 }
299 
ImageAnimatorTest002()300 void ImageAnimatorTddTest::ImageAnimatorTest002()
301 {
302     TDD_CASE_BEGIN();
303     JsAppContext::GetInstance()->SetCurrentAbilityInfo("app/run/", "com.example.test", 0);
304     JSValue page = CreatePage(BUNDLE02, strlen(BUNDLE02));
305     UIViewGroup *wrapper = reinterpret_cast<UIViewGroup *>(GetViewByRef(page, "animator"));
306     EXPECT_TRUE(wrapper != nullptr);
307     if (wrapper != nullptr) {
308         const int16_t wrapperSize = 100;
309         EXPECT_EQ(wrapperSize, wrapper->GetWidth());
310         EXPECT_EQ(wrapperSize, wrapper->GetHeight());
311         UIImageAnimatorView *animator = reinterpret_cast<UIImageAnimatorView *>(wrapper->GetChildrenHead());
312         EXPECT_TRUE(animator != nullptr);
313         if (animator != nullptr) {
314             const int16_t animatorSize = 80;
315             const uint8_t imagesSize = 10;
316             const uint8_t repeatTimes = 3;
317             EXPECT_EQ(animatorSize, animator->GetWidth());
318             EXPECT_EQ(animatorSize, animator->GetHeight());
319             EXPECT_EQ(imagesSize, animator->GetImageAnimatorImageNum());
320             EXPECT_FALSE(animator->IsRepeat());
321             EXPECT_EQ(repeatTimes, animator->GetRepeatTimes());
322             EXPECT_TRUE(animator->IsReverse());
323             EXPECT_FALSE(animator->IsSizeFixed());
324             EXPECT_FALSE(animator->GetFillMode());
325         }
326     }
327     DestroyPage(page);
328     TDD_CASE_END();
329 }
330 
ImageAnimatorTest003()331 void ImageAnimatorTddTest::ImageAnimatorTest003()
332 {
333     TDD_CASE_BEGIN();
334     JsAppContext::GetInstance()->SetCurrentAbilityInfo("app/run/", "com.example.test", 0);
335     JSValue page = CreatePage(BUNDLE03, strlen(BUNDLE03));
336     UIViewGroup *wrapper = reinterpret_cast<UIViewGroup *>(GetViewByRef(page, "animator"));
337     EXPECT_TRUE(wrapper != nullptr);
338     if (wrapper != nullptr) {
339         UIImageAnimatorView *animator = reinterpret_cast<UIImageAnimatorView *>(wrapper->GetChildrenHead());
340         EXPECT_TRUE(animator != nullptr);
341         if (animator != nullptr) {
342             const char *propStopped = "stopped";
343             const char *methodStart = "start";
344             const char *methodPause = "pause";
345             const char *methodResume = "resume";
346             const char *methodStop = "stop";
347             const char *methodGetState = "getState";
348             const char *statePlaying = "playing";
349             const char *statePaused = "paused";
350             const char *stateStopped = "stopped";
351             EXPECT_FALSE(JSObject::GetBoolean(page, propStopped));
352             char *state = CallFuncAsString(page, methodGetState);
353             EXPECT_STREQ(state, statePlaying);
354             ACE_FREE(state);
355 
356             JSRelease(JSObject::Call(page, methodPause));
357             state = CallFuncAsString(page, methodGetState);
358             EXPECT_STREQ(state, statePaused);
359             ACE_FREE(state);
360 
361             JSRelease(JSObject::Call(page, methodResume));
362             state = CallFuncAsString(page, methodGetState);
363             EXPECT_STREQ(state, statePlaying);
364             ACE_FREE(state);
365 
366             JSRelease(JSObject::Call(page, methodStop));
367             state = CallFuncAsString(page, methodGetState);
368             EXPECT_STREQ(state, stateStopped);
369             ACE_FREE(state);
370             EXPECT_TRUE(JSObject::GetBoolean(page, propStopped));
371 
372             JSRelease(JSObject::Call(page, methodStart));
373             state = CallFuncAsString(page, methodGetState);
374             EXPECT_STREQ(state, statePlaying);
375             ACE_FREE(state);
376         }
377     }
378     DestroyPage(page);
379     TDD_CASE_END();
380 }
381 
CallFuncAsString(JSValue target,const char * const funcName) const382 char *ImageAnimatorTddTest::CallFuncAsString(JSValue target, const char * const funcName) const
383 {
384     JSValue state = JSObject::Call(target, funcName);
385     char *value = JSString::Value(state);
386     JSRelease(state);
387     return value;
388 }
389 
390 #ifdef TDD_ASSERTIONS
391 /**
392  * @tc.name: ImageAnimatorTest001
393  * @tc.desc:
394  */
395 HWTEST_F(ImageAnimatorTddTest, ImageAnimatorTest001, TestSize.Level1)
396 {
397     ImageAnimatorTddTest::ImageAnimatorTest001();
398 }
399 
400 /**
401  * @tc.name: ImageAnimatorTest002
402  * @tc.desc:
403  */
404 HWTEST_F(ImageAnimatorTddTest, ImageAnimatorTest002, TestSize.Level0)
405 {
406     ImageAnimatorTddTest::ImageAnimatorTest002();
407 }
408 
409 /**
410  * @tc.name: ImageAnimatorTest003
411  * @tc.desc:
412  */
413 HWTEST_F(ImageAnimatorTddTest, ImageAnimatorTest003, TestSize.Level1)
414 {
415     ImageAnimatorTddTest::ImageAnimatorTest003();
416 }
417 #endif
418 
419 } // namespace ACELite
420 } // namespace OHOS
421