blob: 11cfd4909036ea39249aab69af4653fae66f4425 [file] [log] [blame]
mukesh agrawal46c27cc2013-07-10 16:39:10 -07001// 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
12namespace shill {
13
14using base::Bind;
15using base::Unretained;
16
17class 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
33class 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
48MATCHER_P(ErrorType, type, "") {
49 return arg.type() == type;
50}
51
52TEST_F(ResultAggregatorTest, Unused) {
53 EXPECT_CALL(*this, ReportResult(ErrorType(Error::kSuccess))).Times(0);
54}
55
56TEST_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
64TEST_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
72TEST_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
80TEST_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