1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "AnimationParser.h"
18 
19 #include <gtest/gtest.h>
20 
21 using namespace android;
22 
TEST(AnimationParserTest,Test_can_ignore_line)23 TEST(AnimationParserTest, Test_can_ignore_line) {
24     EXPECT_TRUE(can_ignore_line(""));
25     EXPECT_TRUE(can_ignore_line("     "));
26     EXPECT_TRUE(can_ignore_line("#"));
27     EXPECT_TRUE(can_ignore_line("   # comment"));
28 
29     EXPECT_FALSE(can_ignore_line("text"));
30     EXPECT_FALSE(can_ignore_line("text # comment"));
31     EXPECT_FALSE(can_ignore_line("     text"));
32     EXPECT_FALSE(can_ignore_line("     text # comment"));
33 }
34 
TEST(AnimationParserTest,Test_remove_prefix)35 TEST(AnimationParserTest, Test_remove_prefix) {
36     static const char TEST_STRING[] = "abcdef";
37     const char* rest = nullptr;
38     EXPECT_FALSE(remove_prefix(TEST_STRING, "def", &rest));
39     // Ignore strings that only consist of the prefix
40     EXPECT_FALSE(remove_prefix(TEST_STRING, TEST_STRING, &rest));
41 
42     EXPECT_TRUE(remove_prefix(TEST_STRING, "abc", &rest));
43     EXPECT_STREQ("def", rest);
44 
45     EXPECT_TRUE(remove_prefix("  abcdef", "abc", &rest));
46     EXPECT_STREQ("def", rest);
47 }
48 
TEST(AnimationParserTest,Test_parse_text_field)49 TEST(AnimationParserTest, Test_parse_text_field) {
50     static const char TEST_FILE_NAME[] = "font_file";
51     static const int TEST_X = 3;
52     static const int TEST_Y = 6;
53     static const int TEST_R = 1;
54     static const int TEST_G = 2;
55     static const int TEST_B = 4;
56     static const int TEST_A = 8;
57 
58     static const char TEST_XCENT_YCENT[] = "c c 1 2 4 8  font_file ";
59     static const char TEST_XCENT_YVAL[]  = "c 6 1 2 4 8  font_file ";
60     static const char TEST_XVAL_YCENT[]  = "3 c 1 2 4 8  font_file ";
61     static const char TEST_XVAL_YVAL[]   = "3 6 1 2 4 8  font_file ";
62     static const char TEST_BAD_MISSING[] = "c c 1 2 4 font_file";
63     static const char TEST_BAD_NO_FILE[] = "c c 1 2 4 8";
64 
65     animation::text_field out;
66 
67     EXPECT_TRUE(parse_text_field(TEST_XCENT_YCENT, &out));
68     EXPECT_EQ(CENTER_VAL, out.pos_x);
69     EXPECT_EQ(CENTER_VAL, out.pos_y);
70     EXPECT_EQ(TEST_R, out.color_r);
71     EXPECT_EQ(TEST_G, out.color_g);
72     EXPECT_EQ(TEST_B, out.color_b);
73     EXPECT_EQ(TEST_A, out.color_a);
74     EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str());
75 
76     EXPECT_TRUE(parse_text_field(TEST_XCENT_YVAL, &out));
77     EXPECT_EQ(CENTER_VAL, out.pos_x);
78     EXPECT_EQ(TEST_Y, out.pos_y);
79     EXPECT_EQ(TEST_R, out.color_r);
80     EXPECT_EQ(TEST_G, out.color_g);
81     EXPECT_EQ(TEST_B, out.color_b);
82     EXPECT_EQ(TEST_A, out.color_a);
83     EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str());
84 
85     EXPECT_TRUE(parse_text_field(TEST_XVAL_YCENT, &out));
86     EXPECT_EQ(TEST_X, out.pos_x);
87     EXPECT_EQ(CENTER_VAL, out.pos_y);
88     EXPECT_EQ(TEST_R, out.color_r);
89     EXPECT_EQ(TEST_G, out.color_g);
90     EXPECT_EQ(TEST_B, out.color_b);
91     EXPECT_EQ(TEST_A, out.color_a);
92     EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str());
93 
94     EXPECT_TRUE(parse_text_field(TEST_XVAL_YVAL, &out));
95     EXPECT_EQ(TEST_X, out.pos_x);
96     EXPECT_EQ(TEST_Y, out.pos_y);
97     EXPECT_EQ(TEST_R, out.color_r);
98     EXPECT_EQ(TEST_G, out.color_g);
99     EXPECT_EQ(TEST_B, out.color_b);
100     EXPECT_EQ(TEST_A, out.color_a);
101     EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str());
102 
103     EXPECT_FALSE(parse_text_field(TEST_BAD_MISSING, &out));
104     EXPECT_FALSE(parse_text_field(TEST_BAD_NO_FILE, &out));
105 }
106 
TEST(AnimationParserTest,Test_parse_animation_desc_basic)107 TEST(AnimationParserTest, Test_parse_animation_desc_basic) {
108     static const char TEST_ANIMATION[] = R"desc(
109         # Basic animation
110         animation: 5 1 test/animation_file
111         frame: 1000 0 100
112     )desc";
113     animation anim;
114 
115     EXPECT_TRUE(parse_animation_desc(TEST_ANIMATION, &anim));
116 }
117 
TEST(AnimationParserTest,Test_parse_animation_desc_bad_no_animation_line)118 TEST(AnimationParserTest, Test_parse_animation_desc_bad_no_animation_line) {
119     static const char TEST_ANIMATION[] = R"desc(
120         # Bad animation
121         frame: 1000 90  10
122     )desc";
123     animation anim;
124 
125     EXPECT_FALSE(parse_animation_desc(TEST_ANIMATION, &anim));
126 }
127 
TEST(AnimationParserTest,Test_parse_animation_desc_bad_no_frame)128 TEST(AnimationParserTest, Test_parse_animation_desc_bad_no_frame) {
129     static const char TEST_ANIMATION[] = R"desc(
130         # Bad animation
131         animation: 5 1 test/animation_file
132     )desc";
133     animation anim;
134 
135     EXPECT_FALSE(parse_animation_desc(TEST_ANIMATION, &anim));
136 }
137 
TEST(AnimationParserTest,Test_parse_animation_desc_bad_animation_line_format)138 TEST(AnimationParserTest, Test_parse_animation_desc_bad_animation_line_format) {
139     static const char TEST_ANIMATION[] = R"desc(
140         # Bad animation
141         animation: 5 1
142         frame: 1000 90  10
143     )desc";
144     animation anim;
145 
146     EXPECT_FALSE(parse_animation_desc(TEST_ANIMATION, &anim));
147 }
148 
TEST(AnimationParserTest,Test_parse_animation_desc_full)149 TEST(AnimationParserTest, Test_parse_animation_desc_full) {
150     static const char TEST_ANIMATION[] = R"desc(
151         # Full animation
152         animation: 5 1 test/animation_file
153         clock_display:    11 12 13 14 15 16 test/time_font
154         percent_display:  21 22 23 24 25 26 test/percent_font
155 
156         frame: 10 20 30
157         frame: 40 50 60
158     )desc";
159     animation anim;
160 
161     EXPECT_TRUE(parse_animation_desc(TEST_ANIMATION, &anim));
162 
163     EXPECT_EQ(5, anim.num_cycles);
164     EXPECT_EQ(1, anim.first_frame_repeats);
165     EXPECT_STREQ("test/animation_file", anim.animation_file.c_str());
166 
167     EXPECT_EQ(11, anim.text_clock.pos_x);
168     EXPECT_EQ(12, anim.text_clock.pos_y);
169     EXPECT_EQ(13, anim.text_clock.color_r);
170     EXPECT_EQ(14, anim.text_clock.color_g);
171     EXPECT_EQ(15, anim.text_clock.color_b);
172     EXPECT_EQ(16, anim.text_clock.color_a);
173     EXPECT_STREQ("test/time_font", anim.text_clock.font_file.c_str());
174 
175     EXPECT_EQ(21, anim.text_percent.pos_x);
176     EXPECT_EQ(22, anim.text_percent.pos_y);
177     EXPECT_EQ(23, anim.text_percent.color_r);
178     EXPECT_EQ(24, anim.text_percent.color_g);
179     EXPECT_EQ(25, anim.text_percent.color_b);
180     EXPECT_EQ(26, anim.text_percent.color_a);
181     EXPECT_STREQ("test/percent_font", anim.text_percent.font_file.c_str());
182 
183     EXPECT_EQ(2, anim.num_frames);
184 
185     EXPECT_EQ(10, anim.frames[0].disp_time);
186     EXPECT_EQ(20, anim.frames[0].min_level);
187     EXPECT_EQ(30, anim.frames[0].max_level);
188 
189     EXPECT_EQ(40, anim.frames[1].disp_time);
190     EXPECT_EQ(50, anim.frames[1].min_level);
191     EXPECT_EQ(60, anim.frames[1].max_level);
192 }
193