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.
4
5#include "chrome/browser/diagnostics/diagnostics_model.h"
6
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"
11
12namespace diagnostics {
13
14// Basic harness to acquire and release the Diagnostic model object.
15class DiagnosticsModelTest : public testing::Test {
16 protected:
17  DiagnosticsModelTest()
18      : cmdline_(CommandLine::NO_PROGRAM) {
19  }
20
21  virtual ~DiagnosticsModelTest() { }
22
23  virtual void SetUp() {
24    model_.reset(MakeDiagnosticsModel(cmdline_));
25    ASSERT_TRUE(model_.get() != NULL);
26  }
27
28  virtual void TearDown() {
29    model_.reset();
30  }
31
32  scoped_ptr<DiagnosticsModel> model_;
33  CommandLine cmdline_;
34
35  DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelTest);
36};
37
38// The test observer is used to know if the callbacks are being called.
39class UTObserver: public DiagnosticsModel::Observer {
40 public:
41  UTObserver()
42      : tests_done_(false),
43        recovery_done_(false),
44        num_tested_(0),
45        num_recovered_(0) {
46  }
47
48  virtual void OnTestFinished(int index, DiagnosticsModel* model) OVERRIDE {
49    EXPECT_TRUE(model != NULL);
50    ++num_tested_;
51    EXPECT_NE(DiagnosticsModel::TEST_FAIL_STOP,
52              model->GetTest(index).GetResult())
53        << "Failed stop test: " << index;
54  }
55
56  virtual void OnAllTestsDone(DiagnosticsModel* model) OVERRIDE {
57    EXPECT_TRUE(model != NULL);
58    tests_done_ = true;
59  }
60
61  virtual void OnRecoveryFinished(int index, DiagnosticsModel* model) OVERRIDE {
62    EXPECT_TRUE(model != NULL);
63    ++num_recovered_;
64    EXPECT_NE(DiagnosticsModel::RECOVERY_FAIL_STOP,
65              model->GetTest(index).GetResult())
66        << "Failed stop recovery: " << index;
67  }
68
69  virtual void OnAllRecoveryDone(DiagnosticsModel* model) OVERRIDE {
70    EXPECT_TRUE(model != NULL);
71    recovery_done_ = true;
72  }
73
74  bool tests_done() const { return tests_done_; }
75  bool recovery_done() const { return recovery_done_; }
76
77  int num_tested() const { return num_tested_;}
78  int num_recovered() const { return num_recovered_;}
79
80 private:
81  bool tests_done_;
82  bool recovery_done_;
83  int num_tested_;
84  int num_recovered_;
85
86  DISALLOW_COPY_AND_ASSIGN(UTObserver);
87};
88
89// Test that the initial state is correct.
90TEST_F(DiagnosticsModelTest, BeforeRun) {
91  int available = model_->GetTestAvailableCount();
92  EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, available);
93  EXPECT_EQ(0, model_->GetTestRunCount());
94  EXPECT_EQ(DiagnosticsModel::TEST_NOT_RUN, model_->GetTest(0).GetResult());
95}
96
97// Run all the tests, verify that the basic callbacks are run and that the
98// final state is correct.
99TEST_F(DiagnosticsModelTest, RunAll) {
100  UTObserver observer;
101  EXPECT_FALSE(observer.tests_done());
102  model_->RunAll(&observer);
103  EXPECT_TRUE(observer.tests_done());
104  EXPECT_FALSE(observer.recovery_done());
105  model_->RecoverAll(&observer);
106  EXPECT_TRUE(observer.recovery_done());
107  EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, model_->GetTestRunCount());
108  EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, observer.num_tested());
109  EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, observer.num_recovered());
110}
111
112}  // namespace diagnostics
113