File: proto_conversion_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (231 lines) | stat: -rw-r--r-- 8,344 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/scheduler/internal/proto_conversion.h"

#include <string>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/notifications/scheduler/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

using IconProto = notifications::proto::Icon;

namespace notifications {
namespace {

const char kGuid[] = "testGuid";
const char kData[] = "bitmapdata";

void TestClientStateConversion(ClientState* client_state) {
  DCHECK(client_state);
  notifications::proto::ClientState proto;
  ClientState expected;
  ClientStateToProto(client_state, &proto);
  ClientStateFromProto(&proto, &expected);
  EXPECT_EQ(*client_state, expected)
      << " \n Output: \n " << test::DebugString(client_state)
      << " \n Expected: \n"
      << test::DebugString(&expected);
}

void TestNotificationEntryConversion(NotificationEntry* entry) {
  DCHECK(entry);
  notifications::proto::NotificationEntry proto;
  NotificationEntry expected(SchedulerClientType::kTest1, "");
  NotificationEntryToProto(entry, &proto);
  NotificationEntryFromProto(&proto, &expected);
  EXPECT_EQ(entry->notification_data, expected.notification_data);
  EXPECT_EQ(entry->schedule_params, expected.schedule_params);

  EXPECT_EQ(*entry, expected)
      << "Output: " << test::DebugString(entry)
      << " \n Expected: " << test::DebugString(&expected);
}

NotificationData::Button CreateButton(const char* text,
                                      ActionButtonType type,
                                      const char* id) {
  NotificationData::Button button;
  button.text = base::UTF8ToUTF16(text);
  button.type = type;
  button.id = id;
  return button;
}

TEST(ProtoConversionTest, IconEntryFromProto) {
  IconProto proto;
  proto.set_icon(kData);
  IconEntry entry;

  IconEntryFromProto(&proto, &entry);

  // Verify entry data.
  EXPECT_EQ(entry.data, kData);
}

TEST(ProtoConversionTest, IconEntryToProto) {
  IconEntry entry;
  entry.data = kData;
  IconProto proto;

  IconEntryToProto(&entry, &proto);

  // Verify proto data.
  EXPECT_EQ(proto.icon(), kData);
}

// Verifies client state proto conversion.
TEST(ProtoConversionTest, ClientStateProtoConversion) {
  // Verify basic fields.
  ClientState client_state;
  test::ImpressionTestData test_data{
      SchedulerClientType::kTest1,
      3 /* current_max_daily_show */,
      {} /* impressions */,
      std::nullopt /* suppression_info */,
      0 /* negative_events_count */,
      std::nullopt /* negative_event_ts */,
      std::nullopt /* last_shown_ts */,
  };
  test::AddImpressionTestData(test_data, &client_state);
  TestClientStateConversion(&client_state);

  // Verify suppression info.
  base::Time last_trigger_time;
  bool success =
      base::Time::FromString("04/25/20 01:00:00 AM", &last_trigger_time);
  DCHECK(success);
  auto duration = base::Days(7);
  auto suppression = SuppressionInfo(last_trigger_time, duration);
  suppression.recover_goal = 5;
  client_state.suppression_info = std::move(suppression);
  client_state.last_shown_ts = last_trigger_time;
  client_state.negative_events_count = 1;
  client_state.last_negative_event_ts = last_trigger_time + base::Minutes(1);
  TestClientStateConversion(&client_state);
}

// Verifies impression proto conversion.
TEST(ProtoConversionTest, ImpressionProtoConversion) {
  ClientState client_state;
  client_state.type = SchedulerClientType::kTest1;
  base::Time create_time;
  bool success = base::Time::FromString("03/25/19 00:00:00 AM", &create_time);
  DCHECK(success);

  Impression impression = test::CreateImpression(
      create_time, UserFeedback::kHelpful, ImpressionResult::kPositive,
      true /*integrated*/, kGuid, SchedulerClientType::kTest1);
  client_state.impressions.emplace_back(impression);
  TestClientStateConversion(&client_state);

  auto& first_impression = *client_state.impressions.begin();

  // Verify all feedback types.
  std::vector<UserFeedback> feedback_types{
      UserFeedback::kNoFeedback, UserFeedback::kHelpful,
      UserFeedback::kNotHelpful, UserFeedback::kClick,
      UserFeedback::kDismiss,    UserFeedback::kIgnore};
  for (const auto feedback_type : feedback_types) {
    first_impression.feedback = feedback_type;
    TestClientStateConversion(&client_state);
  }

  // Verify all impression result types.
  std::vector<ImpressionResult> impression_results{
      ImpressionResult::kInvalid, ImpressionResult::kPositive,
      ImpressionResult::kNegative, ImpressionResult::kNeutral};
  for (const auto impression_result : impression_results) {
    first_impression.impression = impression_result;
    TestClientStateConversion(&client_state);
  }

  // Verify impression mapping.
  first_impression.impression_mapping[UserFeedback::kClick] =
      ImpressionResult::kNeutral;
  TestClientStateConversion(&client_state);

  // Verify custom data.
  first_impression.custom_data = {{"url", "https://www.example.com"}};
  TestClientStateConversion(&client_state);
}

// Verifies multiple impressions are serialized correctly.
TEST(ProtoConversionTest, MultipleImpressionConversion) {
  ClientState client_state;
  base::Time create_time;
  bool success = base::Time::FromString("04/25/20 01:00:00 AM", &create_time);
  DCHECK(success);

  Impression impression = test::CreateImpression(
      create_time, UserFeedback::kHelpful, ImpressionResult::kPositive,
      true /*integrated*/, "guid1", SchedulerClientType::kUnknown);
  Impression other_impression = test::CreateImpression(
      create_time, UserFeedback::kNoFeedback, ImpressionResult::kNegative,
      false /*integrated*/, "guid2", SchedulerClientType::kUnknown);
  client_state.impressions.emplace_back(std::move(impression));
  client_state.impressions.emplace_back(std::move(other_impression));
  TestClientStateConversion(&client_state);
}

// Verifies notification entry proto conversion.
TEST(ProtoConversionTest, NotificationEntryConversion) {
  NotificationEntry entry(SchedulerClientType::kTest2, kGuid);
  bool success =
      base::Time::FromString("04/25/20 01:00:00 AM", &entry.create_time);
  DCHECK(success);
  TestNotificationEntryConversion(&entry);

  // Test notification data.
  entry.notification_data.title = u"title";
  entry.notification_data.message = u"message";
  entry.icons_uuid.emplace(IconType::kSmallIcon, "small_icon_uuid");
  entry.icons_uuid.emplace(IconType::kLargeIcon, "large_icon_uuid");
  entry.notification_data.custom_data = {{"url", "https://www.example.com"}};
  TestNotificationEntryConversion(&entry);

  // Test scheduling params.
  const ScheduleParams::Priority priorities[] = {
      ScheduleParams::Priority::kLow, ScheduleParams::Priority::kNoThrottle};
  for (auto priority : priorities) {
    entry.schedule_params.priority = priority;
    TestNotificationEntryConversion(&entry);
  }
  entry.schedule_params.impression_mapping[UserFeedback::kDismiss] =
      ImpressionResult::kPositive;
  entry.schedule_params.impression_mapping[UserFeedback::kClick] =
      ImpressionResult::kNeutral;
  TestNotificationEntryConversion(&entry);

  entry.schedule_params.deliver_time_start = entry.create_time;
  entry.schedule_params.deliver_time_end =
      entry.create_time + base::Minutes(10);
  entry.schedule_params.ignore_timeout_duration = base::Days(3);
  TestNotificationEntryConversion(&entry);
}

// Verifies buttons are converted correctly to proto buffers.
TEST(ProtoConversionTest, NotificationEntryButtonsConversion) {
  NotificationEntry entry(SchedulerClientType::kTest2, kGuid);
  bool success =
      base::Time::FromString("04/25/20 01:00:00 AM", &entry.create_time);
  DCHECK(success);

  NotificationData::Button button;
  entry.notification_data.buttons.emplace_back(
      CreateButton("text1", ActionButtonType::kUnknownAction, "id1"));
  entry.notification_data.buttons.emplace_back(
      CreateButton("text2", ActionButtonType::kHelpful, "id2"));
  entry.notification_data.buttons.emplace_back(
      CreateButton("text3", ActionButtonType::kUnhelpful, "id3"));
  TestNotificationEntryConversion(&entry);
}

}  // namespace
}  // namespace notifications