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
|
// Copyright 2011 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/diagnostics/diagnostics_model.h"
#include <memory>
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace diagnostics {
// Basic harness to acquire and release the Diagnostic model object.
class DiagnosticsModelTest : public testing::Test {
public:
DiagnosticsModelTest(const DiagnosticsModelTest&) = delete;
DiagnosticsModelTest& operator=(const DiagnosticsModelTest&) = delete;
protected:
DiagnosticsModelTest() : cmdline_(base::CommandLine::NO_PROGRAM) {}
~DiagnosticsModelTest() override = default;
void SetUp() override {
model_.reset(MakeDiagnosticsModel(cmdline_));
ASSERT_TRUE(model_.get() != nullptr);
}
void TearDown() override { model_.reset(); }
std::unique_ptr<DiagnosticsModel> model_;
base::CommandLine cmdline_;
};
// The test observer is used to know if the callbacks are being called.
class UTObserver: public DiagnosticsModel::Observer {
public:
UTObserver()
: tests_done_(false),
recovery_done_(false),
num_tested_(0),
num_recovered_(0) {
}
UTObserver(const UTObserver&) = delete;
UTObserver& operator=(const UTObserver&) = delete;
void OnTestFinished(int index, DiagnosticsModel* model) override {
EXPECT_TRUE(model != nullptr);
++num_tested_;
EXPECT_NE(DiagnosticsModel::TEST_FAIL_STOP,
model->GetTest(index).GetResult())
<< "Failed stop test: " << index;
}
void OnAllTestsDone(DiagnosticsModel* model) override {
EXPECT_TRUE(model != nullptr);
tests_done_ = true;
}
void OnRecoveryFinished(int index, DiagnosticsModel* model) override {
EXPECT_TRUE(model != nullptr);
++num_recovered_;
EXPECT_NE(DiagnosticsModel::RECOVERY_FAIL_STOP,
model->GetTest(index).GetResult())
<< "Failed stop recovery: " << index;
}
void OnAllRecoveryDone(DiagnosticsModel* model) override {
EXPECT_TRUE(model != nullptr);
recovery_done_ = true;
}
bool tests_done() const { return tests_done_; }
bool recovery_done() const { return recovery_done_; }
int num_tested() const { return num_tested_;}
int num_recovered() const { return num_recovered_;}
private:
bool tests_done_;
bool recovery_done_;
int num_tested_;
int num_recovered_;
};
// Test that the initial state is correct.
TEST_F(DiagnosticsModelTest, BeforeRun) {
int available = model_->GetTestAvailableCount();
EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, available);
EXPECT_EQ(0, model_->GetTestRunCount());
EXPECT_EQ(DiagnosticsModel::TEST_NOT_RUN, model_->GetTest(0).GetResult());
}
// Run all the tests, verify that the basic callbacks are run and that the
// final state is correct.
TEST_F(DiagnosticsModelTest, RunAll) {
UTObserver observer;
EXPECT_FALSE(observer.tests_done());
model_->RunAll(&observer);
EXPECT_TRUE(observer.tests_done());
EXPECT_FALSE(observer.recovery_done());
model_->RecoverAll(&observer);
EXPECT_TRUE(observer.recovery_done());
EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, model_->GetTestRunCount());
EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, observer.num_tested());
EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, observer.num_recovered());
}
} // namespace diagnostics
|