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 virtual ~DiagnosticsModelTest() { }
23 virtual void SetUp() {
24 model_
.reset(MakeDiagnosticsModel(cmdline_
));
25 ASSERT_TRUE(model_
.get() != NULL
);
28 virtual void TearDown() {
32 scoped_ptr
<DiagnosticsModel
> model_
;
35 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelTest
);
38 // The test observer is used to know if the callbacks are being called.
39 class UTObserver
: public DiagnosticsModel::Observer
{
43 recovery_done_(false),
48 virtual void OnTestFinished(int index
, DiagnosticsModel
* model
) OVERRIDE
{
49 EXPECT_TRUE(model
!= NULL
);
51 EXPECT_NE(DiagnosticsModel::TEST_FAIL_STOP
,
52 model
->GetTest(index
).GetResult())
53 << "Failed stop test: " << index
;
56 virtual void OnAllTestsDone(DiagnosticsModel
* model
) OVERRIDE
{
57 EXPECT_TRUE(model
!= NULL
);
61 virtual void OnRecoveryFinished(int index
, DiagnosticsModel
* model
) OVERRIDE
{
62 EXPECT_TRUE(model
!= NULL
);
64 EXPECT_NE(DiagnosticsModel::RECOVERY_FAIL_STOP
,
65 model
->GetTest(index
).GetResult())
66 << "Failed stop recovery: " << index
;
69 virtual void OnAllRecoveryDone(DiagnosticsModel
* model
) OVERRIDE
{
70 EXPECT_TRUE(model
!= NULL
);
71 recovery_done_
= true;
74 bool tests_done() const { return tests_done_
; }
75 bool recovery_done() const { return recovery_done_
; }
77 int num_tested() const { return num_tested_
;}
78 int num_recovered() const { return num_recovered_
;}
86 DISALLOW_COPY_AND_ASSIGN(UTObserver
);
89 // Test that the initial state is correct.
90 TEST_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());
97 // Run all the tests, verify that the basic callbacks are run and that the
98 // final state is correct.
99 TEST_F(DiagnosticsModelTest
, RunAll
) {
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());
112 } // namespace diagnostics