1 // Copyright 2018 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 "components/mirroring/service/receiver_setup_querier.h"
6 
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h"
11 #include "base/macros.h"
12 #include "base/test/mock_callback.h"
13 #include "base/test/task_environment.h"
14 #include "base/time/default_tick_clock.h"
15 #include "components/mirroring/service/value_util.h"
16 #include "media/cast/cast_environment.h"
17 #include "media/cast/test/utility/net_utility.h"
18 #include "mojo/public/cpp/bindings/self_owned_receiver.h"
19 #include "net/base/ip_endpoint.h"
20 #include "services/network/test/test_url_loader_factory.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 
24 namespace mirroring {
25 
26 class ReceiverSetupQuerierTest : public ::testing::Test {
27  public:
ReceiverSetupQuerierTest()28   ReceiverSetupQuerierTest()
29       : receiver_address_(media::cast::test::GetFreeLocalPort().address()) {}
30   ~ReceiverSetupQuerierTest() override = default;
31 
32  protected:
CreateReceiverSetupQuerier()33   void CreateReceiverSetupQuerier() {
34     mojo::PendingRemote<network::mojom::URLLoaderFactory> url_loader_factory;
35     auto test_url_loader_factory =
36         std::make_unique<network::TestURLLoaderFactory>();
37     url_loader_factory_ = test_url_loader_factory.get();
38     mojo::MakeSelfOwnedReceiver(
39         std::move(test_url_loader_factory),
40         url_loader_factory.InitWithNewPipeAndPassReceiver());
41 
42     setup_querier_ = std::make_unique<ReceiverSetupQuerier>(
43         receiver_address_, std::move(url_loader_factory));
44   }
45 
SendReceiverSetupInfo(const std::string & setup_info)46   void SendReceiverSetupInfo(const std::string& setup_info) {
47     url_loader_factory_->AddResponse(
48         "http://" + receiver_address_.ToString() + ":8008/setup/eureka_info",
49         setup_info);
50     task_environment_.RunUntilIdle();
51   }
52 
setup_querier()53   ReceiverSetupQuerier* setup_querier() { return setup_querier_.get(); }
54 
55  private:
56   base::test::TaskEnvironment task_environment_;
57   const net::IPAddress receiver_address_;
58   network::TestURLLoaderFactory* url_loader_factory_ = nullptr;
59   std::unique_ptr<ReceiverSetupQuerier> setup_querier_;
60 
61   DISALLOW_COPY_AND_ASSIGN(ReceiverSetupQuerierTest);
62 };
63 
TEST_F(ReceiverSetupQuerierTest,ValidSetupInfo)64 TEST_F(ReceiverSetupQuerierTest, ValidSetupInfo) {
65   const std::string kReceiverSetupInfo =
66       R"({ "cast_build_revision": "1.26.0.1",
67            "connected": true,
68            "ethernet_connected": false,
69            "has_update": false,
70            "uptime": 13253.6,
71            "name": "Eureka"
72          })";
73 
74   CreateReceiverSetupQuerier();
75   SendReceiverSetupInfo(kReceiverSetupInfo);
76 
77   EXPECT_EQ("1.26.0.1", setup_querier()->build_version());
78   EXPECT_EQ("Eureka", setup_querier()->friendly_name());
79 }
80 
TEST_F(ReceiverSetupQuerierTest,SetupInfoMissingName)81 TEST_F(ReceiverSetupQuerierTest, SetupInfoMissingName) {
82   const std::string kReceiverSetupInfo =
83       R"({ "cast_build_revision": "1.26.0.1" })";
84 
85   CreateReceiverSetupQuerier();
86   SendReceiverSetupInfo(kReceiverSetupInfo);
87 
88   EXPECT_EQ("1.26.0.1", setup_querier()->build_version());
89   EXPECT_EQ("", setup_querier()->friendly_name());
90 }
91 
TEST_F(ReceiverSetupQuerierTest,SetupInfoMissingRevision)92 TEST_F(ReceiverSetupQuerierTest, SetupInfoMissingRevision) {
93   const std::string kReceiverSetupInfo = R"({ "name": "Eureka" })";
94 
95   CreateReceiverSetupQuerier();
96   SendReceiverSetupInfo(kReceiverSetupInfo);
97 
98   EXPECT_EQ("", setup_querier()->build_version());
99   EXPECT_EQ("Eureka", setup_querier()->friendly_name());
100 }
101 
TEST_F(ReceiverSetupQuerierTest,EmptySetupInfo)102 TEST_F(ReceiverSetupQuerierTest, EmptySetupInfo) {
103   const std::string kReceiverSetupInfo = "{}";
104 
105   CreateReceiverSetupQuerier();
106   SendReceiverSetupInfo(kReceiverSetupInfo);
107 
108   EXPECT_EQ("", setup_querier()->build_version());
109   EXPECT_EQ("", setup_querier()->friendly_name());
110 }
111 
TEST_F(ReceiverSetupQuerierTest,InfoNeverSent)112 TEST_F(ReceiverSetupQuerierTest, InfoNeverSent) {
113   CreateReceiverSetupQuerier();
114   EXPECT_EQ("", setup_querier()->build_version());
115   EXPECT_EQ("", setup_querier()->friendly_name());
116 }
117 
118 }  // namespace mirroring
119