Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / diagnostics / diagnostics_model_unittest.cc
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
12 namespace diagnostics {
13
14 // Basic harness to acquire and release the Diagnostic model object.
15 class DiagnosticsModelTest : public testing::Test {
16  protected:
17   DiagnosticsModelTest()
18       : cmdline_(CommandLine::NO_PROGRAM) {
19   }
20
21   ~DiagnosticsModelTest() override {}
22
23   void SetUp() override {
24     model_.reset(MakeDiagnosticsModel(cmdline_));
25     ASSERT_TRUE(model_.get() != NULL);
26   }
27
28   void TearDown() override { model_.reset(); }
29
30   scoped_ptr<DiagnosticsModel> model_;
31   CommandLine cmdline_;
32
33   DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelTest);
34 };
35
36 // The test observer is used to know if the callbacks are being called.
37 class UTObserver: public DiagnosticsModel::Observer {
38  public:
39   UTObserver()
40       : tests_done_(false),
41         recovery_done_(false),
42         num_tested_(0),
43         num_recovered_(0) {
44   }
45
46   void OnTestFinished(int index, DiagnosticsModel* model) override {
47     EXPECT_TRUE(model != NULL);
48     ++num_tested_;
49     EXPECT_NE(DiagnosticsModel::TEST_FAIL_STOP,
50               model->GetTest(index).GetResult())
51         << "Failed stop test: " << index;
52   }
53
54   void OnAllTestsDone(DiagnosticsModel* model) override {
55     EXPECT_TRUE(model != NULL);
56     tests_done_ = true;
57   }
58
59   void OnRecoveryFinished(int index, DiagnosticsModel* model) override {
60     EXPECT_TRUE(model != NULL);
61     ++num_recovered_;
62     EXPECT_NE(DiagnosticsModel::RECOVERY_FAIL_STOP,
63               model->GetTest(index).GetResult())
64         << "Failed stop recovery: " << index;
65   }
66
67   void OnAllRecoveryDone(DiagnosticsModel* model) override {
68     EXPECT_TRUE(model != NULL);
69     recovery_done_ = true;
70   }
71
72   bool tests_done() const { return tests_done_; }
73   bool recovery_done() const { return recovery_done_; }
74
75   int num_tested() const { return num_tested_;}
76   int num_recovered() const { return num_recovered_;}
77
78  private:
79   bool tests_done_;
80   bool recovery_done_;
81   int num_tested_;
82   int num_recovered_;
83
84   DISALLOW_COPY_AND_ASSIGN(UTObserver);
85 };
86
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());
93 }
94
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) {
98   UTObserver observer;
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());
108 }
109
110 }  // namespace diagnostics