File: test_utils.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (190 lines) | stat: -rw-r--r-- 7,133 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
// 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/test/test_utils.h"

#include <sstream>
#include <utility>

#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/notifications/scheduler/internal/notification_entry.h"
#include "chrome/browser/notifications/scheduler/public/notification_data.h"

namespace notifications {
namespace test {

ImpressionTestData::ImpressionTestData(
    SchedulerClientType type,
    size_t current_max_daily_show,
    std::vector<Impression> impressions,
    std::optional<SuppressionInfo> suppression_info,
    size_t negative_events_count,
    std::optional<base::Time> last_negative_event_ts,
    std::optional<base::Time> last_shown_ts)
    : type(type),
      current_max_daily_show(current_max_daily_show),
      impressions(std::move(impressions)),
      suppression_info(std::move(suppression_info)),
      negative_events_count(negative_events_count),
      last_negative_event_ts(last_negative_event_ts),
      last_shown_ts(last_shown_ts) {}

ImpressionTestData::ImpressionTestData(const ImpressionTestData& other) =
    default;

ImpressionTestData::~ImpressionTestData() = default;

void AddImpressionTestData(const ImpressionTestData& data,
                           ClientState* client_state) {
  DCHECK(client_state);
  client_state->type = data.type;
  client_state->current_max_daily_show = data.current_max_daily_show;
  for (const auto& impression : data.impressions) {
    client_state->impressions.emplace_back(impression);
  }
  client_state->suppression_info = data.suppression_info;
  client_state->negative_events_count = data.negative_events_count;
  client_state->last_shown_ts = data.last_shown_ts;
  client_state->last_negative_event_ts = data.last_negative_event_ts;
}

void AddImpressionTestData(
    const std::vector<ImpressionTestData>& test_data_vec,
    ImpressionHistoryTracker::ClientStates* client_states) {
  DCHECK(client_states);
  for (const auto& test_data : test_data_vec) {
    auto client_state = std::make_unique<ClientState>();
    AddImpressionTestData(test_data, client_state.get());
    client_states->emplace(test_data.type, std::move(client_state));
  }
}

void AddImpressionTestData(
    const std::vector<ImpressionTestData>& test_data_vec,
    std::vector<std::unique_ptr<ClientState>>* client_states) {
  DCHECK(client_states);
  for (const auto& test_data : test_data_vec) {
    auto client_state = std::make_unique<ClientState>();
    AddImpressionTestData(test_data, client_state.get());
    client_states->emplace_back(std::move(client_state));
  }
}

Impression CreateImpression(const base::Time& create_time,
                            UserFeedback feedback,
                            ImpressionResult impression_result,
                            bool integrated,
                            const std::string& guid,
                            SchedulerClientType type) {
  Impression impression(type, guid, create_time);
  impression.feedback = feedback;
  impression.impression = impression_result;
  impression.integrated = integrated;
  return impression;
}

std::string DebugString(const NotificationData* data) {
  DCHECK(data);
  std::ostringstream stream;
  stream << " Notification Data:"
         << " \n title:" << data->title << "\n message:" << data->message
         << " \n custom data: ";
  for (const auto& pair : data->custom_data)
    stream << " key:" << pair.first << " , value:" << pair.second;

  return stream.str();
}

std::string DebugString(const NotificationEntry* entry) {
  DCHECK(entry);
  std::ostringstream stream;
  stream << "NotificationEntry: \n  type: " << static_cast<int>(entry->type)
         << " \n guid: " << entry->guid << "\n create_time: "
         << entry->create_time.ToDeltaSinceWindowsEpoch().InMicroseconds()
         << " \n notification_data:" << DebugString(&entry->notification_data)
         << " \n schedule params: priority:"
         << static_cast<int>(entry->schedule_params.priority);

  for (const auto& mapping : entry->schedule_params.impression_mapping) {
    stream << " \n impression mapping: " << static_cast<int>(mapping.first)
           << " : " << static_cast<int>(mapping.second);
  }

  if (base::Contains(entry->icons_uuid, IconType::kSmallIcon))
    stream << " \n small_icons_id:"
           << entry->icons_uuid.at(IconType::kSmallIcon);
  if (base::Contains(entry->icons_uuid, IconType::kLargeIcon))
    stream << " \n large_icons_id:"
           << entry->icons_uuid.at(IconType::kLargeIcon);

  return stream.str();
}

std::string DebugString(const ClientState* client_state) {
  DCHECK(client_state);
  std::string log = base::StringPrintf(
      "Client state: type: %d \n"
      "current_max_daily_show: %d \n"
      "impressions.size(): %zu \n"
      "negative_events_count: %zu \n",
      static_cast<int>(client_state->type),
      client_state->current_max_daily_show, client_state->impressions.size(),
      client_state->negative_events_count);

  if (client_state->last_negative_event_ts.has_value()) {
    std::ostringstream stream;
    stream << "last negative event timestamp: ",
        client_state->last_negative_event_ts.value();
    log += stream.str();
  }

  if (client_state->last_shown_ts.has_value()) {
    std::ostringstream stream;
    stream << "last shown notification timestamp: ",
        client_state->last_shown_ts.value();
    log += stream.str();
  }

  for (const auto& impression : client_state->impressions) {
    std::ostringstream stream;
    stream << "\n"
           << "Impression, create_time:" << impression.create_time << "\n"
           << " create_time in microseconds:"
           << impression.create_time.ToDeltaSinceWindowsEpoch().InMicroseconds()
           << "\n"
           << "feedback: " << static_cast<int>(impression.feedback) << "\n"
           << "impression result: " << static_cast<int>(impression.impression)
           << " \n"
           << "integrated: " << impression.integrated << "\n"
           << "guid: " << impression.guid << "\n"
           << "type: " << static_cast<int>(impression.type);

    for (const auto& mapping : impression.impression_mapping) {
      stream << " \n impression mapping: " << static_cast<int>(mapping.first)
             << " : " << static_cast<int>(mapping.second);
    }

    for (const auto& pair : impression.custom_data) {
      stream << " \n custom data, key: " << pair.first
             << " value: " << pair.second;
    }

    log += stream.str();
  }

  if (client_state->suppression_info.has_value()) {
    std::ostringstream stream;
    stream << "\n Suppression info, last_trigger_time: "
           << client_state->suppression_info->last_trigger_time << "\n"
           << "duration:" << client_state->suppression_info->duration << "\n"
           << "recover_goal:" << client_state->suppression_info->recover_goal;
    log += stream.str();
  }
  return log;
}

}  // namespace test
}  // namespace notifications