File: download_ukm_helper_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 (137 lines) | stat: -rw-r--r-- 5,357 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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/download/public/common/download_ukm_helper.h"

#include <memory>
#include <string_view>

#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "components/ukm/test_ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using UkmDownloadStarted = ukm::builders::Download_Started;
using UkmDownloadInterrupted = ukm::builders::Download_Interrupted;
using UkmDownloadResumed = ukm::builders::Download_Resumed;
using UkmDownloadCompleted = ukm::builders::Download_Completed;

namespace download {

class DownloadUkmHelperTest : public testing::Test {
 public:
  DownloadUkmHelperTest() : download_id_(123) { ResetUkmRecorder(); }

  DownloadUkmHelperTest(const DownloadUkmHelperTest&) = delete;
  DownloadUkmHelperTest& operator=(const DownloadUkmHelperTest&) = delete;

  ~DownloadUkmHelperTest() override = default;

  void ResetUkmRecorder() {
    test_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
  }

  void ExpectUkmMetrics(std::string_view entry_name,
                        const std::vector<std::string_view>& keys,
                        const std::vector<int>& values) {
    const auto& entries = test_recorder_->GetEntriesByName(entry_name);
    EXPECT_EQ(1u, entries.size());
    for (const ukm::mojom::UkmEntry* entry : entries) {
      const size_t keys_size = keys.size();
      EXPECT_EQ(keys_size, values.size());
      for (size_t i = 0; i < keys_size; ++i) {
        test_recorder_->ExpectEntryMetric(entry, keys[i], values[i]);
      }
    }
  }

 protected:
  int download_id_;

  base::test::TaskEnvironment task_environment_;
  std::unique_ptr<ukm::TestAutoSetUkmRecorder> test_recorder_;
};

TEST_F(DownloadUkmHelperTest, TestBasicReporting) {
  // RecordDownloadStarted
  ukm::SourceId source_id = ukm::UkmRecorder::GetNewSourceID();
  DownloadContent file_type = DownloadContent::kAudio;
  DownloadSource download_source = DownloadSource::UNKNOWN;
  DownloadConnectionSecurity state =
      DownloadConnectionSecurity::DOWNLOAD_SECURE;
  bool is_same_host_download = true;
  DownloadUkmHelper::RecordDownloadStarted(download_id_, source_id, file_type,
                                           download_source, state,
                                           is_same_host_download);

  ExpectUkmMetrics(
      UkmDownloadStarted::kEntryName,
      {UkmDownloadStarted::kDownloadIdName, UkmDownloadStarted::kFileTypeName,
       UkmDownloadStarted::kDownloadSourceName,
       UkmDownloadStarted::kDownloadConnectionSecurityName,
       UkmDownloadStarted::kIsSameHostDownloadName},
      {download_id_, static_cast<int>(file_type),
       static_cast<int>(download_source), static_cast<int>(state),
       is_same_host_download});

  // RecordDownloadInterrupted, has change in file size.
  int change_in_file_size = 1000;
  DownloadInterruptReason reason = DOWNLOAD_INTERRUPT_REASON_NONE;
  int resulting_file_size = 2000;
  int time_since_start = 250;
  int bytes_wasted = 1234;
  DownloadUkmHelper::RecordDownloadInterrupted(
      download_id_, change_in_file_size, reason, resulting_file_size,
      base::Milliseconds(time_since_start), bytes_wasted);

  ExpectUkmMetrics(
      UkmDownloadInterrupted::kEntryName,
      {UkmDownloadInterrupted::kDownloadIdName,
       UkmDownloadInterrupted::kChangeInFileSizeName,
       UkmDownloadInterrupted::kReasonName,
       UkmDownloadInterrupted::kResultingFileSizeName,
       UkmDownloadInterrupted::kTimeSinceStartName,
       UkmDownloadInterrupted::kBytesWastedName},
      {download_id_,
       DownloadUkmHelper::CalcExponentialBucket(change_in_file_size),
       static_cast<int>(reason),
       DownloadUkmHelper::CalcExponentialBucket(resulting_file_size),
       time_since_start, DownloadUkmHelper::CalcNearestKB(bytes_wasted)});

  // RecordDownloadResumed.
  ResumeMode mode = ResumeMode::IMMEDIATE_RESTART;
  int time_since_start_resume = 300;
  DownloadUkmHelper::RecordDownloadResumed(
      download_id_, mode, base::Milliseconds(time_since_start_resume));

  ExpectUkmMetrics(
      UkmDownloadResumed::kEntryName,
      {UkmDownloadResumed::kDownloadIdName, UkmDownloadResumed::kModeName,
       UkmDownloadResumed::kTimeSinceStartName},
      {download_id_, static_cast<int>(mode), time_since_start_resume});

  // RecordDownloadCompleted.
  int resulting_file_size_completed = 3000;
  int time_since_start_completed = 400;
  int bytes_wasted_completed = 2345;
  DownloadUkmHelper::RecordDownloadCompleted(
      download_id_, resulting_file_size_completed,
      base::Milliseconds(time_since_start_completed), bytes_wasted_completed);

  ExpectUkmMetrics(
      UkmDownloadCompleted::kEntryName,
      {UkmDownloadCompleted::kDownloadIdName,
       UkmDownloadCompleted::kResultingFileSizeName,
       UkmDownloadCompleted::kTimeSinceStartName,
       UkmDownloadCompleted::kBytesWastedName},
      {download_id_,
       DownloadUkmHelper::CalcExponentialBucket(resulting_file_size_completed),
       time_since_start_completed,
       DownloadUkmHelper::CalcNearestKB(bytes_wasted_completed)});
}

}  // namespace download