Home | History | Annotate | Download | only in bluetooth
      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/basictypes.h"
      6 #include "base/bind.h"
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/memory/scoped_vector.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "device/bluetooth/bluetooth_device_win.h"
     11 #include "device/bluetooth/bluetooth_service_record.h"
     12 #include "device/bluetooth/bluetooth_task_manager_win.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 namespace {
     16 
     17 const char kDeviceName[] = "Device";
     18 const char kDeviceAddress[] = "device address";
     19 
     20 const char kTestAudioSdpName[] = "Audio";
     21 const char kTestAudioSdpAddress[] = "01:02:03:0A:10:A0";
     22 const char kTestAudioSdpBytes[] =
     23     "35510900000a00010001090001350319110a09000435103506190100090019350619001909"
     24     "010209000535031910020900093508350619110d090102090100250c417564696f20536f75"
     25     "726365090311090001";
     26 const char kTestAudioSdpUuid[] = "0000110a-0000-1000-8000-00805f9b34fb";
     27 
     28 const char kTestVideoSdpName[] = "Video";
     29 const char kTestVideoSdpAddress[] = "A0:10:0A:03:02:01";
     30 const char kTestVideoSdpBytes[] =
     31     "354b0900000a000100030900013506191112191203090004350c3503190100350519000308"
     32     "0b090005350319100209000935083506191108090100090100250d566f6963652047617465"
     33     "776179";
     34 const char kTestVideoSdpUuid[] = "00001112-0000-1000-8000-00805f9b34fb";
     35 
     36 }  // namespace
     37 
     38 namespace device {
     39 
     40 class BluetoothDeviceWinTest : public testing::Test {
     41  public:
     42   BluetoothDeviceWinTest()
     43       : error_called_(false),
     44         provides_service_with_name_(false) {
     45     BluetoothTaskManagerWin::DeviceState device_state;
     46     device_state.name = kDeviceName;
     47     device_state.address = kDeviceAddress;
     48 
     49     BluetoothTaskManagerWin::ServiceRecordState* audio_state =
     50         new BluetoothTaskManagerWin::ServiceRecordState();
     51     audio_state->name = kTestAudioSdpName;
     52     audio_state->address = kTestAudioSdpAddress;
     53     base::HexStringToBytes(kTestAudioSdpBytes, &audio_state->sdp_bytes);
     54     device_state.service_record_states.push_back(audio_state);
     55 
     56     BluetoothTaskManagerWin::ServiceRecordState* video_state =
     57         new BluetoothTaskManagerWin::ServiceRecordState();
     58     video_state->name = kTestVideoSdpName;
     59     video_state->address = kTestVideoSdpAddress;
     60     base::HexStringToBytes(kTestVideoSdpBytes, &video_state->sdp_bytes);
     61     device_state.service_record_states.push_back(video_state);
     62 
     63     device_.reset(new BluetoothDeviceWin(device_state));
     64   }
     65 
     66   void GetServiceRecords(
     67       const BluetoothDevice::ServiceRecordList& service_record_list) {
     68     service_records_ = &service_record_list;
     69   }
     70 
     71   void OnError() {
     72     error_called_ = true;
     73   }
     74 
     75   void SetProvidesServiceWithName(bool provides_service_with_name) {
     76     provides_service_with_name_ = provides_service_with_name;
     77   }
     78 
     79  protected:
     80   scoped_ptr<BluetoothDevice> device_;
     81   scoped_ptr<BluetoothDevice> empty_device_;
     82   const BluetoothDevice::ServiceRecordList* service_records_;
     83   bool error_called_;
     84   bool provides_service_with_name_;
     85 };
     86 
     87 TEST_F(BluetoothDeviceWinTest, GetServices) {
     88   BluetoothDevice::ServiceList service_list = device_->GetServices();
     89 
     90   EXPECT_EQ(2, service_list.size());
     91   EXPECT_STREQ(kTestAudioSdpUuid, service_list[0].c_str());
     92   EXPECT_STREQ(kTestVideoSdpUuid, service_list[1].c_str());
     93 }
     94 
     95 TEST_F(BluetoothDeviceWinTest, GetServiceRecords) {
     96   device_->GetServiceRecords(
     97       base::Bind(&BluetoothDeviceWinTest::GetServiceRecords,
     98                  base::Unretained(this)),
     99       BluetoothDevice::ErrorCallback());
    100   ASSERT_TRUE(service_records_ != NULL);
    101   EXPECT_EQ(2, service_records_->size());
    102   EXPECT_STREQ(kTestAudioSdpUuid, (*service_records_)[0]->uuid().c_str());
    103   EXPECT_STREQ(kTestVideoSdpUuid, (*service_records_)[1]->uuid().c_str());
    104 
    105   BluetoothDeviceWin* device_win =
    106       reinterpret_cast<BluetoothDeviceWin*>(device_.get());
    107 
    108   const BluetoothServiceRecord* audio_device =
    109       device_win->GetServiceRecord(kTestAudioSdpUuid);
    110   ASSERT_TRUE(audio_device != NULL);
    111   EXPECT_EQ((*service_records_)[0], audio_device);
    112 
    113   const BluetoothServiceRecord* video_device =
    114       device_win->GetServiceRecord(kTestVideoSdpUuid);
    115   ASSERT_TRUE(video_device != NULL);
    116   EXPECT_EQ((*service_records_)[1], video_device);
    117 
    118   const BluetoothServiceRecord* invalid_device =
    119       device_win->GetServiceRecord(kTestVideoSdpAddress);
    120   EXPECT_TRUE(invalid_device == NULL);
    121 }
    122 
    123 TEST_F(BluetoothDeviceWinTest, ProvidesServiceWithName) {
    124   device_->ProvidesServiceWithName(
    125       kTestAudioSdpName,
    126       base::Bind(&BluetoothDeviceWinTest::SetProvidesServiceWithName,
    127                  base::Unretained(this)));
    128   EXPECT_TRUE(provides_service_with_name_);
    129 
    130   device_->ProvidesServiceWithName(
    131       kTestVideoSdpName,
    132       base::Bind(&BluetoothDeviceWinTest::SetProvidesServiceWithName,
    133                  base::Unretained(this)));
    134   EXPECT_TRUE(provides_service_with_name_);
    135 
    136   device_->ProvidesServiceWithName(
    137       "name that does not exist",
    138       base::Bind(&BluetoothDeviceWinTest::SetProvidesServiceWithName,
    139                  base::Unretained(this)));
    140   EXPECT_FALSE(provides_service_with_name_);
    141 }
    142 
    143 }  // namespace device