1// Copyright (c) 2013 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 "base/memory/scoped_ptr.h" 6#include "base/message_loop/message_loop.h" 7#include "base/values.h" 8#include "content/browser/media/webrtc_internals.h" 9#include "content/browser/media/webrtc_internals_ui_observer.h" 10#include "content/public/test/test_browser_thread.h" 11#include "testing/gtest/include/gtest/gtest.h" 12 13namespace content { 14 15namespace { 16 17static const std::string kContraints = "c"; 18static const std::string kServers = "s"; 19static const std::string kUrl = "u"; 20 21class MockWebRTCInternalsProxy : public WebRTCInternalsUIObserver { 22 public: 23 virtual void OnUpdate(const std::string& command, 24 const base::Value* value) OVERRIDE { 25 command_ = command; 26 value_.reset(value->DeepCopy()); 27 } 28 29 std::string command() { 30 return command_; 31 } 32 33 base::Value* value() { 34 return value_.get(); 35 } 36 37 private: 38 std::string command_; 39 scoped_ptr<base::Value> value_; 40}; 41 42class WebRTCInternalsTest : public testing::Test { 43 public: 44 WebRTCInternalsTest() : io_thread_(BrowserThread::UI, &io_loop_) {} 45 46 protected: 47 std::string ExpectedInfo(std::string prefix, 48 std::string id, 49 std::string suffix) { 50 static const std::string kstatic_part1 = std::string( 51 "{\"constraints\":\"c\","); 52 static const std::string kstatic_part2 = std::string( 53 ",\"servers\":\"s\",\"url\":\"u\"}"); 54 return prefix + kstatic_part1 + id + kstatic_part2 + suffix; 55 } 56 57 base::MessageLoop io_loop_; 58 TestBrowserThread io_thread_; 59}; 60 61} // namespace 62 63TEST_F(WebRTCInternalsTest, AddRemoveObserver) { 64 scoped_ptr<MockWebRTCInternalsProxy> observer( 65 new MockWebRTCInternalsProxy()); 66 WebRTCInternals::GetInstance()->AddObserver(observer.get()); 67 WebRTCInternals::GetInstance()->RemoveObserver(observer.get()); 68 WebRTCInternals::GetInstance()->OnAddPeerConnection( 69 0, 3, 4, kUrl, kServers, kContraints); 70 EXPECT_EQ("", observer->command()); 71 72 WebRTCInternals::GetInstance()->OnRemovePeerConnection(3, 4); 73} 74 75TEST_F(WebRTCInternalsTest, SendAddPeerConnectionUpdate) { 76 scoped_ptr<MockWebRTCInternalsProxy> observer( 77 new MockWebRTCInternalsProxy()); 78 WebRTCInternals::GetInstance()->AddObserver(observer.get()); 79 WebRTCInternals::GetInstance()->OnAddPeerConnection( 80 0, 1, 2, kUrl, kServers, kContraints); 81 EXPECT_EQ("addPeerConnection", observer->command()); 82 83 base::DictionaryValue* dict = NULL; 84 EXPECT_TRUE(observer->value()->GetAsDictionary(&dict)); 85 86 int int_value; 87 EXPECT_TRUE(dict->GetInteger("pid", &int_value)); 88 EXPECT_EQ(1, int_value); 89 EXPECT_TRUE(dict->GetInteger("lid", &int_value)); 90 EXPECT_EQ(2, int_value); 91 92 std::string value; 93 EXPECT_TRUE(dict->GetString("url", &value)); 94 EXPECT_EQ(kUrl, value); 95 EXPECT_TRUE(dict->GetString("servers", &value)); 96 EXPECT_EQ(kServers, value); 97 EXPECT_TRUE(dict->GetString("constraints", &value)); 98 EXPECT_EQ(kContraints, value); 99 100 WebRTCInternals::GetInstance()->RemoveObserver(observer.get()); 101 WebRTCInternals::GetInstance()->OnRemovePeerConnection(1, 2); 102} 103 104TEST_F(WebRTCInternalsTest, SendRemovePeerConnectionUpdate) { 105 scoped_ptr<MockWebRTCInternalsProxy> observer( 106 new MockWebRTCInternalsProxy()); 107 WebRTCInternals::GetInstance()->AddObserver(observer.get()); 108 WebRTCInternals::GetInstance()->OnAddPeerConnection( 109 0, 1, 2, kUrl, kServers, kContraints); 110 WebRTCInternals::GetInstance()->OnRemovePeerConnection(1, 2); 111 EXPECT_EQ("removePeerConnection", observer->command()); 112 113 base::DictionaryValue* dict = NULL; 114 EXPECT_TRUE(observer->value()->GetAsDictionary(&dict)); 115 116 int int_value; 117 EXPECT_TRUE(dict->GetInteger("pid", &int_value)); 118 EXPECT_EQ(1, int_value); 119 EXPECT_TRUE(dict->GetInteger("lid", &int_value)); 120 EXPECT_EQ(2, int_value); 121 122 WebRTCInternals::GetInstance()->RemoveObserver(observer.get()); 123} 124 125TEST_F(WebRTCInternalsTest, SendUpdatePeerConnectionUpdate) { 126 scoped_ptr<MockWebRTCInternalsProxy> observer( 127 new MockWebRTCInternalsProxy()); 128 WebRTCInternals::GetInstance()->AddObserver(observer.get()); 129 WebRTCInternals::GetInstance()->OnAddPeerConnection( 130 0, 1, 2, kUrl, kServers, kContraints); 131 132 const std::string update_type = "fakeType"; 133 const std::string update_value = "fakeValue"; 134 WebRTCInternals::GetInstance()->OnUpdatePeerConnection( 135 1, 2, update_type, update_value); 136 137 EXPECT_EQ("updatePeerConnection", observer->command()); 138 139 base::DictionaryValue* dict = NULL; 140 EXPECT_TRUE(observer->value()->GetAsDictionary(&dict)); 141 142 int int_value; 143 EXPECT_TRUE(dict->GetInteger("pid", &int_value)); 144 EXPECT_EQ(1, int_value); 145 EXPECT_TRUE(dict->GetInteger("lid", &int_value)); 146 EXPECT_EQ(2, int_value); 147 148 std::string value; 149 EXPECT_TRUE(dict->GetString("type", &value)); 150 EXPECT_EQ(update_type, value); 151 EXPECT_TRUE(dict->GetString("value", &value)); 152 EXPECT_EQ(update_value, value); 153 154 WebRTCInternals::GetInstance()->OnRemovePeerConnection(1, 2); 155 WebRTCInternals::GetInstance()->RemoveObserver(observer.get()); 156} 157 158} // namespace content 159