Home | History | Annotate | Download | only in diagnostics
      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 "testing/gtest/include/gtest/gtest.h"
      9 
     10 // Basic harness to adquire and release the Diagnostic model object.
     11 class DiagnosticsModelTest : public testing::Test {
     12  protected:
     13   DiagnosticsModelTest()
     14       : model_(NULL),
     15         cmdline_(CommandLine::NO_PROGRAM) {
     16   }
     17 
     18   virtual ~DiagnosticsModelTest() { }
     19 
     20   virtual void SetUp() {
     21     model_ = MakeDiagnosticsModel(cmdline_);
     22     ASSERT_TRUE(model_ != NULL);
     23   }
     24 
     25   virtual void TearDown() {
     26     delete model_;
     27   }
     28 
     29   DiagnosticsModel* model_;
     30   CommandLine cmdline_;
     31 };
     32 
     33 // The test observer is used to know if the callbacks are being called.
     34 class UTObserver: public DiagnosticsModel::Observer {
     35  public:
     36   UTObserver()
     37       : done_(false),
     38         progress_called_(0),
     39         finished_(0),
     40         id_of_failed_stop_test(-1) {
     41   }
     42 
     43   virtual void OnProgress(int id, int percent, DiagnosticsModel* model) {
     44     EXPECT_TRUE(model != NULL);
     45     ++progress_called_;
     46   }
     47 
     48   virtual void OnSkipped(int id, DiagnosticsModel* model) {
     49     EXPECT_TRUE(model != NULL);
     50   }
     51 
     52   virtual void OnFinished(int id, DiagnosticsModel* model) {
     53     EXPECT_TRUE(model != NULL);
     54     ++finished_;
     55     if (model->GetTest(id).GetResult() == DiagnosticsModel::TEST_FAIL_STOP) {
     56       id_of_failed_stop_test = id;
     57       ASSERT_TRUE(false);
     58     }
     59   }
     60 
     61   virtual void OnDoneAll(DiagnosticsModel* model) {
     62     done_ = true;
     63     EXPECT_TRUE(model != NULL);
     64   }
     65 
     66   bool done() const { return done_; }
     67 
     68   int progress_called() const { return progress_called_; }
     69 
     70   int finished() const { return finished_;}
     71 
     72  private:
     73   bool done_;
     74   int progress_called_;
     75   int finished_;
     76   int id_of_failed_stop_test;
     77 };
     78 
     79 // We currently have more tests operational on windows.
     80 #if defined(OS_WIN)
     81 const int kDiagnosticsTestCount = 19;
     82 #elif defined(OS_MACOSX)
     83 const int kDiagnosticsTestCount = 16;
     84 #elif defined(OS_POSIX)
     85 const int kDiagnosticsTestCount = 17;
     86 #endif
     87 
     88 // Test that the initial state is correct.
     89 TEST_F(DiagnosticsModelTest, BeforeRun) {
     90   int available = model_->GetTestAvailableCount();
     91   EXPECT_EQ(kDiagnosticsTestCount, available);
     92   EXPECT_EQ(0, model_->GetTestRunCount());
     93   EXPECT_EQ(DiagnosticsModel::TEST_NOT_RUN, model_->GetTest(0).GetResult());
     94 }
     95 
     96 // Run all the tests, verify that the basic callbacks are run and that the
     97 // final state is correct.
     98 TEST_F(DiagnosticsModelTest, RunAll) {
     99   UTObserver observer;
    100   EXPECT_FALSE(observer.done());
    101   model_->RunAll(&observer);
    102   EXPECT_TRUE(observer.done());
    103   EXPECT_GT(observer.progress_called(), 0);
    104   EXPECT_EQ(kDiagnosticsTestCount, model_->GetTestRunCount());
    105   EXPECT_EQ(kDiagnosticsTestCount, observer.finished());
    106 }
    107