File: speedometer_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (195 lines) | stat: -rw-r--r-- 6,387 bytes parent folder | download | duplicates (8)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/file_manager/speedometer.h"

#include <cmath>
#include <limits>

#include "base/test/scoped_mock_clock_override.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace file_manager {
namespace {

using base::Milliseconds;
using base::Seconds;
using testing::IsNan;

TEST(SpeedometerTest, RemainingTime) {
  base::ScopedMockClockOverride clock;
  Speedometer meter;

  // Testing without setting the total bytes:
  EXPECT_EQ(meter.GetSampleCount(), 0u);
  EXPECT_TRUE(meter.GetRemainingTime().is_max());

  // Sets total number of bytes.
  meter.SetTotalBytes(2000);
  EXPECT_EQ(meter.GetSampleCount(), 0u);
  EXPECT_TRUE(meter.GetRemainingTime().is_max());

  // 1st sample.
  // 1st sample, but not enough to calculate the remaining time.
  clock.Advance(Seconds(11));

  EXPECT_TRUE(meter.Update(100));
  EXPECT_EQ(meter.GetSampleCount(), 1u);
  EXPECT_TRUE(meter.GetRemainingTime().is_max());

  // Sample received less than 3 second after the previous one should be
  // ignored.
  clock.Advance(Milliseconds(2999));
  EXPECT_FALSE(meter.Update(300));
  EXPECT_EQ(meter.GetSampleCount(), 1u);
  EXPECT_TRUE(meter.GetRemainingTime().is_max());

  // 2nd sample, the remaining time can be computed.
  clock.Advance(Milliseconds(1));
  EXPECT_TRUE(meter.Update(300));
  EXPECT_EQ(meter.GetSampleCount(), 2u);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 25);

  // 3rd sample. +3 seconds and still only processed 300 bytes.
  clock.Advance(Seconds(3));
  EXPECT_TRUE(meter.Update(300));
  EXPECT_EQ(meter.GetSampleCount(), 3u);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 50);

  // 4th sample, +5 seconds and still only 300 bytes.
  clock.Advance(Seconds(5));
  EXPECT_TRUE(meter.Update(300));
  EXPECT_EQ(meter.GetSampleCount(), 4u);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 109);

  // 5th sample, +3 seconds and now bumped from 300 to 600 bytes.
  clock.Advance(Seconds(3));
  EXPECT_TRUE(meter.Update(600));
  EXPECT_EQ(meter.GetSampleCount(), 5u);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 55);

  // Elapsed time should impact the remaining time.
  clock.Advance(Seconds(12));
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 43);

  // GetRemainingTime() can return negative value.
  clock.Advance(Seconds(60));
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), -16);
}

TEST(SpeedometerTest, Samples) {
  base::ScopedMockClockOverride clock;
  Speedometer meter;

  constexpr size_t kMaxSamples = 20;
  meter.SetTotalBytes(20000);

  // Slow speed of 100 bytes per second.
  int total_transferred = 0;
  for (size_t i = 0; i < kMaxSamples; i++) {
    EXPECT_EQ(meter.GetSampleCount(), i);
    clock.Advance(Seconds(3));
    total_transferred = i * 100;
    EXPECT_TRUE(meter.Update(total_transferred));
  }

  EXPECT_EQ(meter.GetSampleCount(), kMaxSamples);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 543);

  // +200 to make it compatible with the values in the unittest in the JS
  // version.
  const int initial_transferred_bytes = total_transferred + 200;
  // Faster speed of 300 bytes per second.
  for (size_t i = 0; i < kMaxSamples; i++) {
    // Check buffer not expanded more than the specified length.
    EXPECT_EQ(meter.GetSampleCount(), kMaxSamples);
    clock.Advance(Seconds(3));
    total_transferred = initial_transferred_bytes + (i * 300);
    EXPECT_TRUE(meter.Update(total_transferred));

    // Current speed should be seen as accelerating, thus the remaining time
    // decreasing.
    EXPECT_LT(meter.GetRemainingTime().InSeconds(), 543);
  }

  EXPECT_EQ(meter.GetSampleCount(), kMaxSamples);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 122);

  // Stalling.
  for (size_t i = 0; i < kMaxSamples; i++) {
    // Check buffer not expanded more than the specified length.
    EXPECT_EQ(meter.GetSampleCount(), kMaxSamples);
    clock.Advance(Seconds(3));
    EXPECT_TRUE(meter.Update(total_transferred));
  }

  // When all samples have the same value the remaining time goes to infinity,
  // because the Linear Interpolation expects an inclination/slope, but with all
  // values the same, it becomes a horizontal line, meaning that the bytes will
  // never grow towards the total bytes.
  EXPECT_TRUE(meter.GetRemainingTime().is_max());
}

TEST(SpeedometerTest, ProgressGoingBackwards) {
  base::ScopedMockClockOverride clock;
  Speedometer meter;

  // Sets total number of bytes and a couple of samples.
  meter.SetTotalBytes(2000);
  EXPECT_TRUE(meter.Update(100));
  clock.Advance(Seconds(5));
  EXPECT_TRUE(meter.Update(300));
  EXPECT_EQ(meter.GetSampleCount(), 2u);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 42);

  // Progress going backwards should clear the samples.
  clock.Advance(Seconds(3));
  EXPECT_TRUE(meter.Update(200));
  EXPECT_EQ(meter.GetSampleCount(), 1u);
  EXPECT_TRUE(meter.GetRemainingTime().is_max());

  clock.Advance(Seconds(5));
  EXPECT_TRUE(meter.Update(300));
  EXPECT_EQ(meter.GetSampleCount(), 2u);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 85);
}

TEST(SpeedometerTest, ChangeTotalBytes) {
  base::ScopedMockClockOverride clock;
  Speedometer meter;

  // Sets total number of bytes and a couple of samples.
  meter.SetTotalBytes(2000);
  EXPECT_TRUE(meter.Update(100));
  clock.Advance(Seconds(5));
  EXPECT_TRUE(meter.Update(300));
  EXPECT_EQ(meter.GetSampleCount(), 2u);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 42);

  // Setting the total number of bytes to the existing value shouldn't change
  // anything.
  meter.SetTotalBytes(2000);
  EXPECT_EQ(meter.GetSampleCount(), 2u);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 42);

  // Changing the total number of bytes should clear the samples.
  meter.SetTotalBytes(3000);
  EXPECT_EQ(meter.GetSampleCount(), 0u);
  EXPECT_TRUE(meter.GetRemainingTime().is_max());

  clock.Advance(Seconds(5));
  EXPECT_TRUE(meter.Update(3000));
  EXPECT_EQ(meter.GetSampleCount(), 1u);

  // Go beyond the number of expected bytes.
  clock.Advance(Seconds(5));
  EXPECT_TRUE(meter.Update(4000));
  EXPECT_EQ(meter.GetSampleCount(), 2u);
  EXPECT_EQ(meter.GetRemainingTime().InSeconds(), 0);
}

}  // namespace
}  // namespace file_manager