mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS 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 "shill/result_aggregator.h" |
| 6 | |
| 7 | #include <base/bind.h> |
| 8 | #include <base/memory/ref_counted.h> |
| 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | namespace shill { |
| 13 | |
| 14 | using base::Bind; |
| 15 | using base::Unretained; |
| 16 | |
| 17 | class ResultAggregatorTest : public ::testing::Test { |
| 18 | public: |
| 19 | ResultAggregatorTest() |
| 20 | : aggregator_(new ResultAggregator( |
| 21 | Bind(&ResultAggregatorTest::ReportResult, Unretained(this)))) {} |
| 22 | virtual ~ResultAggregatorTest() {} |
| 23 | |
| 24 | virtual void TearDown() { |
| 25 | aggregator_ = NULL; // Ensure ReportResult is invoked before our dtor. |
| 26 | } |
| 27 | |
| 28 | protected: |
| 29 | MOCK_METHOD1(ReportResult, void(const Error &)); |
| 30 | scoped_refptr<ResultAggregator> aggregator_; |
| 31 | }; |
| 32 | |
| 33 | class ResultGenerator { |
| 34 | public: |
| 35 | explicit ResultGenerator(const scoped_refptr<ResultAggregator> &aggregator) |
| 36 | : aggregator_(aggregator) {} |
| 37 | ~ResultGenerator() {} |
| 38 | |
| 39 | void GenerateResult(const Error::Type error_type) { |
| 40 | aggregator_->ReportResult(Error(error_type)); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | scoped_refptr<ResultAggregator> aggregator_; |
| 45 | DISALLOW_COPY_AND_ASSIGN(ResultGenerator); |
| 46 | }; |
| 47 | |
| 48 | MATCHER_P(ErrorType, type, "") { |
| 49 | return arg.type() == type; |
| 50 | } |
| 51 | |
| 52 | TEST_F(ResultAggregatorTest, Unused) { |
| 53 | EXPECT_CALL(*this, ReportResult(ErrorType(Error::kSuccess))).Times(0); |
| 54 | } |
| 55 | |
| 56 | TEST_F(ResultAggregatorTest, BothSucceed) { |
| 57 | EXPECT_CALL(*this, ReportResult(ErrorType(Error::kSuccess))); |
| 58 | ResultGenerator first_generator(aggregator_); |
| 59 | ResultGenerator second_generator(aggregator_); |
| 60 | first_generator.GenerateResult(Error::kSuccess); |
| 61 | second_generator.GenerateResult(Error::kSuccess); |
| 62 | } |
| 63 | |
| 64 | TEST_F(ResultAggregatorTest, FirstFails) { |
| 65 | EXPECT_CALL(*this, ReportResult(ErrorType(Error::kOperationTimeout))); |
| 66 | ResultGenerator first_generator(aggregator_); |
| 67 | ResultGenerator second_generator(aggregator_); |
| 68 | first_generator.GenerateResult(Error::kOperationTimeout); |
| 69 | second_generator.GenerateResult(Error::kSuccess); |
| 70 | } |
| 71 | |
| 72 | TEST_F(ResultAggregatorTest, SecondFails) { |
| 73 | EXPECT_CALL(*this, ReportResult(ErrorType(Error::kOperationTimeout))); |
| 74 | ResultGenerator first_generator(aggregator_); |
| 75 | ResultGenerator second_generator(aggregator_); |
| 76 | first_generator.GenerateResult(Error::kSuccess); |
| 77 | second_generator.GenerateResult(Error::kOperationTimeout); |
| 78 | } |
| 79 | |
| 80 | TEST_F(ResultAggregatorTest, BothFail) { |
| 81 | EXPECT_CALL(*this, ReportResult(ErrorType(Error::kOperationTimeout))); |
| 82 | ResultGenerator first_generator(aggregator_); |
| 83 | ResultGenerator second_generator(aggregator_); |
| 84 | first_generator.GenerateResult(Error::kOperationTimeout); |
| 85 | second_generator.GenerateResult(Error::kPermissionDenied); |
| 86 | } |
| 87 | |
| 88 | } // namespace shill |