File: health_module_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (145 lines) | stat: -rw-r--r-- 4,679 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
// Copyright 2022 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/reporting/health/health_module.h"

#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/strcat.h"
#include "base/task/thread_pool.h"
#include "base/test/protobuf_matchers.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/reporting/util/test_support_callbacks.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::base::test::EqualsProto;
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithoutArgs;

namespace reporting {
namespace {

class MockHealthModuleDelegate : public HealthModuleDelegate {
 public:
  MOCK_METHOD(Status, DoInit, (), (override));
  MOCK_METHOD(void, DoGetERPHealthData, (HealthCallback cb), (const override));
  MOCK_METHOD(void,
              DoPostHealthRecord,
              (HealthDataHistory history),
              (override));
};

HealthDataHistory AddEnqueueRecordCall() {
  HealthDataHistory history;
  EnqueueRecordCall call;
  call.set_priority(Priority::IMMEDIATE);
  *history.mutable_enqueue_record_call() = call;
  history.set_timestamp_seconds(base::Time::Now().ToTimeT());
  return history;
}

class HealthModuleTest : public ::testing::Test {
 protected:
  void SetUp() override {
    delegate_ = std::make_unique<MockHealthModuleDelegate>();
    mock_delegate_ = delegate_.get();
  }

  void TearDown() override {
    mock_delegate_ = nullptr;  // Prevent runaway pointer.
  }

  void CreateModule() {
    // Next line will asynchronously invoke delegate_->Init.
    module_ = HealthModule::Create(std::move(delegate_));
  }

  base::test::TaskEnvironment task_environment_;

  std::unique_ptr<MockHealthModuleDelegate> delegate_;
  raw_ptr<MockHealthModuleDelegate> mock_delegate_ = nullptr;
  scoped_refptr<HealthModule> module_;
};

TEST_F(HealthModuleTest, Init) {
  test::TestCallbackAutoWaiter init_waiter;
  EXPECT_CALL(*mock_delegate_, DoInit())
      .WillOnce(
          DoAll(Invoke(&init_waiter, &test::TestCallbackAutoWaiter::Signal),
                Return(Status::StatusOK())));
  CreateModule();
}

TEST_F(HealthModuleTest, InitFails) {
  test::TestCallbackAutoWaiter init_waiter;
  EXPECT_CALL(*mock_delegate_, DoInit())
      .WillOnce(
          DoAll(Invoke(&init_waiter, &test::TestCallbackAutoWaiter::Signal),
                Return(Status(error::UNKNOWN, "Test fails init"))));
  CreateModule();
}

TEST_F(HealthModuleTest, WriteAndReadData) {
  {
    test::TestCallbackAutoWaiter init_waiter;
    EXPECT_CALL(*mock_delegate_, DoInit())
        .WillOnce(
            DoAll(Invoke(&init_waiter, &test::TestCallbackAutoWaiter::Signal),
                  Return(Status::StatusOK())));
    CreateModule();
  }

  ERPHealthData ref_data;
  auto call = AddEnqueueRecordCall();
  *ref_data.add_history() = call;
  {
    test::TestCallbackAutoWaiter post_waiter;
    EXPECT_CALL(*mock_delegate_, DoPostHealthRecord(EqualsProto(call)))
        .WillOnce(WithoutArgs(
            Invoke(&post_waiter, &test::TestCallbackAutoWaiter::Signal)));
    module_->PostHealthRecord(call);
  }

  test::TestEvent<const ERPHealthData> read_event;
  EXPECT_CALL(*mock_delegate_, DoGetERPHealthData(_))
      .WillOnce(Invoke(
          [&ref_data](HealthCallback cb) { std::move(cb).Run(ref_data); }));
  module_->GetHealthData(read_event.cb());
  EXPECT_THAT(read_event.ref_result(), EqualsProto(ref_data));
}

TEST_F(HealthModuleTest, UseRecorder) {
  {
    test::TestCallbackAutoWaiter init_waiter;
    EXPECT_CALL(*mock_delegate_, DoInit())
        .WillOnce(
            DoAll(Invoke(&init_waiter, &test::TestCallbackAutoWaiter::Signal),
                  Return(Status::StatusOK())));
    CreateModule();
  }

  test::TestCallbackAutoWaiter post_waiter;
  auto call = AddEnqueueRecordCall();
  EXPECT_CALL(*mock_delegate_, DoPostHealthRecord(EqualsProto(call)))
      .WillOnce(WithoutArgs(
          Invoke(&post_waiter, &test::TestCallbackAutoWaiter::Signal)));
  auto recorder = HealthModule::Recorder(module_);
  // Hand recorder over for async processing.
  // PostHealthRecord will be called by its destructor.
  base::ThreadPool::PostTask(
      FROM_HERE, {base::TaskPriority::BEST_EFFORT},
      base::BindOnce([](HealthModule::Recorder recorder,
                        HealthDataHistory call) { *recorder = call; },
                     std::move(recorder), call));
}
}  // namespace
}  // namespace reporting