1 // Copyright 2014 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/local_discovery/privetv3_session.h"
6
7 #include "chrome/browser/local_discovery/privet_http.h"
8 #include "content/public/test/test_utils.h"
9 #include "net/url_request/test_url_fetcher_factory.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace local_discovery {
14
15 namespace {
16
17 using testing::Invoke;
18 using testing::InvokeWithoutArgs;
19 using testing::StrictMock;
20 using testing::_;
21
22 class MockDelegate : public PrivetV3Session::Delegate {
23 public:
24 MOCK_METHOD2(OnSetupConfirmationNeeded,
25 void(const std::string&,
26 extensions::api::gcd_private::ConfirmationType));
27 MOCK_METHOD1(OnSessionStatus, void(extensions::api::gcd_private::Status));
28 };
29
30 class PrivetV3SessionTest : public testing::Test {
31 public:
PrivetV3SessionTest()32 PrivetV3SessionTest()
33 : session_(scoped_ptr<PrivetHTTPClient>(), &delegate_) {}
34
~PrivetV3SessionTest()35 virtual ~PrivetV3SessionTest() {}
36
QuitLoop()37 void QuitLoop() {
38 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
39 }
40
ConfirmCode(const std::string & code,extensions::api::gcd_private::ConfirmationType type)41 void ConfirmCode(const std::string& code,
42 extensions::api::gcd_private::ConfirmationType type) {
43 session_.ConfirmCode(code);
44 }
45
46 protected:
SetUp()47 virtual void SetUp() OVERRIDE {
48 quit_closure_ = run_loop_.QuitClosure();
49 EXPECT_CALL(delegate_, OnSetupConfirmationNeeded(_, _)).Times(0);
50 EXPECT_CALL(delegate_, OnSessionStatus(_)).Times(0);
51 }
52
53 StrictMock<MockDelegate> delegate_;
54 PrivetV3Session session_;
55 base::MessageLoop loop_;
56 base::RunLoop run_loop_;
57 base::Closure quit_closure_;
58 };
59
TEST_F(PrivetV3SessionTest,NotConfirmed)60 TEST_F(PrivetV3SessionTest, NotConfirmed) {
61 EXPECT_CALL(delegate_, OnSetupConfirmationNeeded(_, _)).Times(1).WillOnce(
62 InvokeWithoutArgs(this, &PrivetV3SessionTest::QuitLoop));
63 session_.Start();
64 run_loop_.Run();
65 }
66
TEST_F(PrivetV3SessionTest,Confirmed)67 TEST_F(PrivetV3SessionTest, Confirmed) {
68 EXPECT_CALL(delegate_,
69 OnSessionStatus(extensions::api::gcd_private::STATUS_SUCCESS))
70 .Times(1)
71 .WillOnce(InvokeWithoutArgs(this, &PrivetV3SessionTest::QuitLoop));
72 EXPECT_CALL(delegate_, OnSetupConfirmationNeeded(_, _)).Times(1).WillOnce(
73 Invoke(this, &PrivetV3SessionTest::ConfirmCode));
74 session_.Start();
75 run_loop_.Run();
76 }
77
78 } // namespace
79
80 } // namespace local_discovery
81