1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "usage_stats/usage_stats.h"
31
32 #include <map>
33 #include <string>
34
35 #include "base/port.h"
36 #include "base/system_util.h"
37 #include "config/stats_config_util.h"
38 #include "config/stats_config_util_mock.h"
39 #include "storage/registry.h"
40 #include "storage/storage_interface.h"
41 #include "testing/base/public/gunit.h"
42 #include "usage_stats/usage_stats.pb.h"
43
44 DECLARE_string(test_tmpdir);
45
46 namespace mozc {
47 namespace usage_stats {
48
49 class UsageStatsTest : public ::testing::Test {
50 protected:
SetUp()51 virtual void SetUp() {
52 SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
53 EXPECT_TRUE(storage::Registry::Clear());
54 mozc::config::StatsConfigUtil::SetHandler(&stats_config_util_);
55 }
TearDown()56 virtual void TearDown() {
57 mozc::config::StatsConfigUtil::SetHandler(NULL);
58 EXPECT_TRUE(storage::Registry::Clear());
59 }
60
61 private:
62 mozc::config::StatsConfigUtilMock stats_config_util_;
63 };
64
TEST_F(UsageStatsTest,IsListedTest)65 TEST_F(UsageStatsTest, IsListedTest) {
66 EXPECT_TRUE(UsageStats::IsListed("Commit"));
67 EXPECT_FALSE(UsageStats::IsListed("WeDoNotDefinedThisStats"));
68 }
69
TEST_F(UsageStatsTest,GetterTest)70 TEST_F(UsageStatsTest, GetterTest) {
71 // Use actual items, but they do not matter with practical usages.
72 const char kCountKey[] = "ShutDown";
73 const char kIntegerKey[] = "UserRegisteredWord";
74 const char kBooleanKey[] = "ConfigUseDictionarySuggest";
75 const char kTimingKey[] = "ElapsedTimeUSec";
76 const char kVirtualKeyboardKey[] = "VirtualKeyboardStats";
77
78 uint32 count_val = 0;
79 int32 integer_val = -1;
80 bool boolean_val = false;
81 uint64 total_time = 0;
82 uint32 num_timings = 0;
83 uint32 avg_time = 0;
84 uint32 min_time = 0;
85 uint32 max_time = 0;
86 Stats virtual_keyboard_val;
87 Stats stats_val;
88 // Cannot get values when no values are registered.
89 EXPECT_FALSE(UsageStats::GetCountForTest(kCountKey, &count_val));
90 EXPECT_FALSE(UsageStats::GetIntegerForTest(kIntegerKey, &integer_val));
91 EXPECT_FALSE(UsageStats::GetBooleanForTest(kBooleanKey, &boolean_val));
92 EXPECT_FALSE(UsageStats::GetTimingForTest(kTimingKey, &total_time,
93 &num_timings, &avg_time, &min_time, &max_time));
94 EXPECT_FALSE(UsageStats::GetVirtualKeyboardForTest(kVirtualKeyboardKey,
95 &virtual_keyboard_val));
96 EXPECT_FALSE(UsageStats::GetStatsForTest(kCountKey, &stats_val));
97
98 // Update stats.
99 UsageStats::IncrementCount(kCountKey);
100 UsageStats::SetInteger(kIntegerKey, 10);
101 UsageStats::SetBoolean(kBooleanKey, true);
102 UsageStats::UpdateTiming(kTimingKey, 5);
103 UsageStats::UpdateTiming(kTimingKey, 7);
104 std::map<string, TouchEventStatsMap> inserted_virtual_keyboard_val;
105 inserted_virtual_keyboard_val["test1"] = TouchEventStatsMap();
106 UsageStats::StoreTouchEventStats(kVirtualKeyboardKey,
107 inserted_virtual_keyboard_val);
108
109 // Cannot get with wrong types.
110 EXPECT_FALSE(UsageStats::GetCountForTest(kIntegerKey, &count_val));
111 EXPECT_FALSE(UsageStats::GetIntegerForTest(kBooleanKey, &integer_val));
112 EXPECT_FALSE(UsageStats::GetBooleanForTest(kTimingKey, &boolean_val));
113 EXPECT_FALSE(UsageStats::GetTimingForTest(kVirtualKeyboardKey, NULL, NULL,
114 NULL, NULL, NULL));
115 EXPECT_FALSE(UsageStats::GetVirtualKeyboardForTest(kCountKey,
116 &virtual_keyboard_val));
117
118 // Check values.
119 EXPECT_TRUE(UsageStats::GetCountForTest(kCountKey, &count_val));
120 EXPECT_TRUE(UsageStats::GetIntegerForTest(kIntegerKey, &integer_val));
121 EXPECT_TRUE(UsageStats::GetBooleanForTest(kBooleanKey, &boolean_val));
122 EXPECT_TRUE(UsageStats::GetTimingForTest(kTimingKey, &total_time,
123 NULL, NULL, NULL, NULL));
124 EXPECT_TRUE(UsageStats::GetTimingForTest(kTimingKey, NULL, &num_timings,
125 NULL, NULL, NULL));
126 EXPECT_TRUE(UsageStats::GetTimingForTest(kTimingKey, NULL, NULL, &avg_time,
127 NULL, NULL));
128 EXPECT_TRUE(UsageStats::GetTimingForTest(kTimingKey, NULL, NULL, NULL,
129 &min_time, NULL));
130 EXPECT_TRUE(UsageStats::GetTimingForTest(kTimingKey, NULL, NULL, NULL,
131 NULL, &max_time));
132 EXPECT_TRUE(UsageStats::GetStatsForTest(kCountKey, &stats_val));
133 EXPECT_TRUE(UsageStats::GetVirtualKeyboardForTest(kVirtualKeyboardKey,
134 &virtual_keyboard_val));
135 EXPECT_EQ(1, count_val);
136 EXPECT_EQ(10, integer_val);
137 EXPECT_TRUE(boolean_val);
138 EXPECT_EQ(12, total_time);
139 EXPECT_EQ(2, num_timings);
140 EXPECT_EQ(6, avg_time);
141 EXPECT_EQ(5, min_time);
142 EXPECT_EQ(7, max_time);
143 EXPECT_EQ(Stats::COUNT, stats_val.type());
144 EXPECT_EQ(1, virtual_keyboard_val.virtual_keyboard_stats_size());
145 EXPECT_EQ(1, stats_val.count());
146
147 // Update stats.
148 UsageStats::IncrementCount(kCountKey);
149 UsageStats::SetInteger(kIntegerKey, 15);
150 UsageStats::SetBoolean(kBooleanKey, false);
151 UsageStats::UpdateTiming(kTimingKey, 9);
152 inserted_virtual_keyboard_val.clear();
153 inserted_virtual_keyboard_val["test2"] = TouchEventStatsMap();
154 UsageStats::StoreTouchEventStats(kVirtualKeyboardKey,
155 inserted_virtual_keyboard_val);
156
157 // Check updated values.
158 EXPECT_TRUE(UsageStats::GetCountForTest(kCountKey, &count_val));
159 EXPECT_TRUE(UsageStats::GetIntegerForTest(kIntegerKey, &integer_val));
160 EXPECT_TRUE(UsageStats::GetBooleanForTest(kBooleanKey, &boolean_val));
161 EXPECT_TRUE(UsageStats::GetTimingForTest(kTimingKey, &total_time,
162 &num_timings, &avg_time, &min_time,
163 &max_time));
164 EXPECT_TRUE(UsageStats::GetStatsForTest(kCountKey, &stats_val));
165 EXPECT_TRUE(UsageStats::GetVirtualKeyboardForTest(kVirtualKeyboardKey,
166 &virtual_keyboard_val));
167 EXPECT_EQ(2, count_val);
168 EXPECT_EQ(15, integer_val);
169 EXPECT_FALSE(boolean_val);
170 EXPECT_EQ(21, total_time);
171 EXPECT_EQ(3, num_timings);
172 EXPECT_EQ(7, avg_time);
173 EXPECT_EQ(5, min_time);
174 EXPECT_EQ(9, max_time);
175 EXPECT_EQ(Stats::COUNT, stats_val.type());
176 EXPECT_EQ(2, virtual_keyboard_val.virtual_keyboard_stats_size());
177 EXPECT_EQ(2, stats_val.count());
178 }
179
180 namespace {
SetDoubleValueStats(uint32 num,double total,double square_total,usage_stats::Stats::DoubleValueStats * double_stats)181 void SetDoubleValueStats(
182 uint32 num, double total, double square_total,
183 usage_stats::Stats::DoubleValueStats *double_stats) {
184 double_stats->set_num(num);
185 double_stats->set_total(total);
186 double_stats->set_square_total(square_total);
187 }
188
SetEventStats(uint32 source_id,uint32 sx_num,double sx_total,double sx_square_total,uint32 sy_num,double sy_total,double sy_square_total,uint32 dx_num,double dx_total,double dx_square_total,uint32 dy_num,double dy_total,double dy_square_total,uint32 tl_num,double tl_total,double tl_square_total,usage_stats::Stats::TouchEventStats * event_stats)189 void SetEventStats(
190 uint32 source_id,
191 uint32 sx_num, double sx_total, double sx_square_total,
192 uint32 sy_num, double sy_total, double sy_square_total,
193 uint32 dx_num, double dx_total, double dx_square_total,
194 uint32 dy_num, double dy_total, double dy_square_total,
195 uint32 tl_num, double tl_total, double tl_square_total,
196 usage_stats::Stats::TouchEventStats *event_stats) {
197 event_stats->set_source_id(source_id);
198 SetDoubleValueStats(sx_num, sx_total, sx_square_total,
199 event_stats->mutable_start_x_stats());
200 SetDoubleValueStats(sy_num, sy_total, sy_square_total,
201 event_stats->mutable_start_y_stats());
202 SetDoubleValueStats(dx_num, dx_total, dx_square_total,
203 event_stats->mutable_direction_x_stats());
204 SetDoubleValueStats(dy_num, dy_total, dy_square_total,
205 event_stats->mutable_direction_y_stats());
206 SetDoubleValueStats(tl_num, tl_total, tl_square_total,
207 event_stats->mutable_time_length_stats());
208 }
209 } // namespace
210
TEST_F(UsageStatsTest,StoreTouchEventStats)211 TEST_F(UsageStatsTest, StoreTouchEventStats) {
212 string stats_str;
213 EXPECT_FALSE(storage::Registry::Lookup("usage_stats.VirtualKeyboardStats",
214 &stats_str));
215 EXPECT_FALSE(storage::Registry::Lookup("usage_stats.VirtualKeyboardMissStats",
216 &stats_str));
217 std::map<string, std::map<uint32, Stats::TouchEventStats> > touch_stats;
218
219 Stats::TouchEventStats &event_stats1 = touch_stats["KEYBOARD_01"][10];
220 SetEventStats(10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
221 14, 15, 16, &event_stats1);
222
223 Stats::TouchEventStats &event_stats2 = touch_stats["KEYBOARD_01"][20];
224 SetEventStats(20, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
225 114, 115, 116, &event_stats2);
226
227 Stats::TouchEventStats &event_stats3 = touch_stats["KEYBOARD_02"][10];
228 SetEventStats(10, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
229 214, 215, 216, &event_stats3);
230
231 UsageStats::StoreTouchEventStats("VirtualKeyboardStats", touch_stats);
232
233 EXPECT_TRUE(storage::Registry::Lookup("usage_stats.VirtualKeyboardStats",
234 &stats_str));
235
236 Stats stats;
237 EXPECT_TRUE(stats.ParseFromString(stats_str));
238 EXPECT_EQ(Stats::VIRTUAL_KEYBOARD, stats.type());
239 EXPECT_EQ(2, stats.virtual_keyboard_stats_size());
240 EXPECT_EQ("KEYBOARD_01",
241 stats.virtual_keyboard_stats(0).keyboard_name());
242 EXPECT_EQ("KEYBOARD_02",
243 stats.virtual_keyboard_stats(1).keyboard_name());
244 EXPECT_EQ(2, stats.virtual_keyboard_stats(0).touch_event_stats_size());
245 EXPECT_EQ(1, stats.virtual_keyboard_stats(1).touch_event_stats_size());
246
247 EXPECT_EQ(event_stats1.DebugString(),
248 stats.virtual_keyboard_stats(0).touch_event_stats(0).DebugString());
249 EXPECT_EQ(event_stats2.DebugString(),
250 stats.virtual_keyboard_stats(0).touch_event_stats(1).DebugString());
251 EXPECT_EQ(event_stats3.DebugString(),
252 stats.virtual_keyboard_stats(1).touch_event_stats(0).DebugString());
253
254
255 std::map<string, std::map<uint32, Stats::TouchEventStats> > touch_stats2;
256 Stats::TouchEventStats &event_stats4 = touch_stats2["KEYBOARD_01"][20];
257 SetEventStats(20, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313,
258 314, 315, 316, &event_stats4);
259 Stats::TouchEventStats &event_stats5 = touch_stats2["KEYBOARD_02"][20];
260 SetEventStats(20, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413,
261 414, 415, 416, &event_stats5);
262
263 UsageStats::StoreTouchEventStats("VirtualKeyboardStats", touch_stats2);
264
265 EXPECT_TRUE(storage::Registry::Lookup("usage_stats.VirtualKeyboardStats",
266 &stats_str));
267
268 Stats stats2;
269 EXPECT_TRUE(stats2.ParseFromString(stats_str));
270 EXPECT_EQ(Stats::VIRTUAL_KEYBOARD, stats2.type());
271 EXPECT_EQ(2, stats2.virtual_keyboard_stats_size());
272 EXPECT_EQ("KEYBOARD_01",
273 stats2.virtual_keyboard_stats(0).keyboard_name());
274 EXPECT_EQ("KEYBOARD_02",
275 stats2.virtual_keyboard_stats(1).keyboard_name());
276 EXPECT_EQ(2, stats2.virtual_keyboard_stats(0).touch_event_stats_size());
277 EXPECT_EQ(2, stats2.virtual_keyboard_stats(1).touch_event_stats_size());
278
279 Stats::TouchEventStats event_stats6;
280 SetEventStats(20, 404, 406, 408, 410, 412, 414, 416, 418, 420, 422, 424, 426,
281 428, 430, 432, &event_stats6);
282
283 EXPECT_EQ(event_stats1.DebugString(),
284 stats2.virtual_keyboard_stats(0).touch_event_stats(0).DebugString());
285 EXPECT_EQ(event_stats6.DebugString(),
286 stats2.virtual_keyboard_stats(0).touch_event_stats(1).DebugString());
287 EXPECT_EQ(event_stats3.DebugString(),
288 stats2.virtual_keyboard_stats(1).touch_event_stats(0).DebugString());
289 EXPECT_EQ(event_stats5.DebugString(),
290 stats2.virtual_keyboard_stats(1).touch_event_stats(1).DebugString());
291
292 EXPECT_FALSE(storage::Registry::Lookup("usage_stats.VirtualKeyboardMissStats",
293 &stats_str));
294 }
295
296 } // namespace usage_stats
297 } // namespace mozc
298