1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/diagnostics/diagnostics_model.h"
7 #include "base/command_line.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace diagnostics {
14 // Basic harness to acquire and release the Diagnostic model object.
15 class DiagnosticsModelTest : public testing::Test {
17 DiagnosticsModelTest()
18 : cmdline_(CommandLine::NO_PROGRAM) {
21 ~DiagnosticsModelTest() override {}
23 void SetUp() override {
24 model_.reset(MakeDiagnosticsModel(cmdline_));
25 ASSERT_TRUE(model_.get() != NULL);
28 void TearDown() override { model_.reset(); }
30 scoped_ptr<DiagnosticsModel> model_;
33 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelTest);
36 // The test observer is used to know if the callbacks are being called.
37 class UTObserver: public DiagnosticsModel::Observer {
41 recovery_done_(false),
46 void OnTestFinished(int index, DiagnosticsModel* model) override {
47 EXPECT_TRUE(model != NULL);
49 EXPECT_NE(DiagnosticsModel::TEST_FAIL_STOP,
50 model->GetTest(index).GetResult())
51 << "Failed stop test: " << index;
54 void OnAllTestsDone(DiagnosticsModel* model) override {
55 EXPECT_TRUE(model != NULL);
59 void OnRecoveryFinished(int index, DiagnosticsModel* model) override {
60 EXPECT_TRUE(model != NULL);
62 EXPECT_NE(DiagnosticsModel::RECOVERY_FAIL_STOP,
63 model->GetTest(index).GetResult())
64 << "Failed stop recovery: " << index;
67 void OnAllRecoveryDone(DiagnosticsModel* model) override {
68 EXPECT_TRUE(model != NULL);
69 recovery_done_ = true;
72 bool tests_done() const { return tests_done_; }
73 bool recovery_done() const { return recovery_done_; }
75 int num_tested() const { return num_tested_;}
76 int num_recovered() const { return num_recovered_;}
84 DISALLOW_COPY_AND_ASSIGN(UTObserver);
87 // Test that the initial state is correct.
88 TEST_F(DiagnosticsModelTest, BeforeRun) {
89 int available = model_->GetTestAvailableCount();
90 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, available);
91 EXPECT_EQ(0, model_->GetTestRunCount());
92 EXPECT_EQ(DiagnosticsModel::TEST_NOT_RUN, model_->GetTest(0).GetResult());
95 // Run all the tests, verify that the basic callbacks are run and that the
96 // final state is correct.
97 TEST_F(DiagnosticsModelTest, RunAll) {
99 EXPECT_FALSE(observer.tests_done());
100 model_->RunAll(&observer);
101 EXPECT_TRUE(observer.tests_done());
102 EXPECT_FALSE(observer.recovery_done());
103 model_->RecoverAll(&observer);
104 EXPECT_TRUE(observer.recovery_done());
105 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, model_->GetTestRunCount());
106 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, observer.num_tested());
107 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, observer.num_recovered());
110 } // namespace diagnostics