File: build_state_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (106 lines) | stat: -rw-r--r-- 4,580 bytes parent folder | download | duplicates (4)
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
// Copyright 2020 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/upgrade_detector/build_state.h"

#include <optional>

#include "base/version.h"
#include "chrome/browser/upgrade_detector/mock_build_state_observer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::AllOf;
using ::testing::Eq;
using ::testing::IsFalse;
using ::testing::IsTrue;
using ::testing::Property;

class BuildStateTest : public ::testing::Test {
 protected:
  BuildStateTest() { build_state_.AddObserver(&mock_observer_); }
  ~BuildStateTest() override { build_state_.RemoveObserver(&mock_observer_); }

  MockBuildStateObserver& mock_observer() { return mock_observer_; }
  BuildState& build_state() { return build_state_; }

 private:
  MockBuildStateObserver mock_observer_;
  BuildState build_state_;
};

// Observers are not notified when there's no update.
TEST_F(BuildStateTest, SetUpdateNoUpdate) {
  EXPECT_EQ(build_state().update_type(), BuildState::UpdateType::kNone);
  build_state().SetUpdate(BuildState::UpdateType::kNone, base::Version(),
                          std::nullopt);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer());
  EXPECT_EQ(build_state().update_type(), BuildState::UpdateType::kNone);
  EXPECT_FALSE(build_state().installed_version().has_value());
  EXPECT_FALSE(build_state().critical_version().has_value());
}

// Observers are notified upon update when the version couldn't be fetched.
TEST_F(BuildStateTest, SetUpdateWithNoVersion) {
  EXPECT_CALL(
      mock_observer(),
      OnUpdate(AllOf(Eq(&build_state()),
                     Property(&BuildState::update_type,
                              Eq(BuildState::UpdateType::kNormalUpdate)),
                     Property(&BuildState::installed_version, IsFalse()),
                     Property(&BuildState::critical_version, IsFalse()))));
  build_state().SetUpdate(BuildState::UpdateType::kNormalUpdate,
                          base::Version(), std::nullopt);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer());
}

// Observers are notified upon update and the version is held.
TEST_F(BuildStateTest, SetUpdateWithVersion) {
  const base::Version expected_version("1.2.3.4");
  EXPECT_CALL(mock_observer(),
              OnUpdate(AllOf(
                  Eq(&build_state()),
                  Property(&BuildState::update_type,
                           Eq(BuildState::UpdateType::kNormalUpdate)),
                  Property(&BuildState::installed_version, IsTrue()),
                  Property(&BuildState::installed_version,
                           Eq(std::optional<base::Version>(expected_version))),
                  Property(&BuildState::critical_version, IsFalse()))));
  build_state().SetUpdate(BuildState::UpdateType::kNormalUpdate,
                          expected_version, std::nullopt);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer());
}

// Observers are notified upon update with a critical version and both versions
// are held.
TEST_F(BuildStateTest, SetUpdateWithCritical) {
  const base::Version expected_version("1.2.3.4");
  const base::Version expected_critical("1.2.3.3");
  EXPECT_CALL(
      mock_observer(),
      OnUpdate(AllOf(
          Eq(&build_state()),
          Property(&BuildState::update_type,
                   Eq(BuildState::UpdateType::kNormalUpdate)),
          Property(&BuildState::installed_version, IsTrue()),
          Property(&BuildState::installed_version,
                   Eq(std::optional<base::Version>(expected_version))),
          Property(&BuildState::critical_version, IsTrue()),
          Property(&BuildState::critical_version,
                   Eq(std::optional<base::Version>(expected_critical))))));
  build_state().SetUpdate(BuildState::UpdateType::kNormalUpdate,
                          expected_version, expected_critical);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer());
}

// Observers are only notified once for duplicate calls.
TEST_F(BuildStateTest, TwoUpdatesOnceNotification) {
  const base::Version expected_version("1.2.3.4");
  EXPECT_CALL(mock_observer(), OnUpdate(&build_state()));
  build_state().SetUpdate(BuildState::UpdateType::kNormalUpdate,
                          expected_version, std::nullopt);
  build_state().SetUpdate(BuildState::UpdateType::kNormalUpdate,
                          expected_version, std::nullopt);
  ::testing::Mock::VerifyAndClearExpectations(&mock_observer());
}