OLD | NEW |
(Empty) | |
| 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 #ifndef USE_BRLAPI |
| 6 #error This test requires brlapi. |
| 7 #endif |
| 8 |
| 9 #include <deque> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "chrome/browser/extensions/api/braille_display_private/braille_controll
er.h" |
| 13 #include "chrome/browser/extensions/api/braille_display_private/brlapi_connectio
n.h" |
| 14 #include "chrome/browser/extensions/extension_apitest.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using content::BrowserThread; |
| 19 using extensions::api::braille_display_private::BrailleController; |
| 20 using extensions::api::braille_display_private::BrlapiConnection; |
| 21 |
| 22 // Data maintained by the mock BrlapiConnection. This data lives throughout |
| 23 // a test, while the api implementation takes ownership of the connection |
| 24 // itself. |
| 25 struct MockBrlapiConnectionData { |
| 26 bool connected; |
| 27 size_t display_size; |
| 28 brlapi_error_t error; |
| 29 std::vector<std::string> written_content; |
| 30 std::deque<brlapi_keyCode_t> pending_keys; |
| 31 }; |
| 32 |
| 33 class MockBrlapiConnection : public BrlapiConnection { |
| 34 public: |
| 35 MockBrlapiConnection(MockBrlapiConnectionData* data) |
| 36 : data_(data) {} |
| 37 virtual bool Connect(const OnDataReadyCallback& on_data_ready) OVERRIDE { |
| 38 data_->connected = true; |
| 39 on_data_ready_ = on_data_ready; |
| 40 if (!data_->pending_keys.empty()) { |
| 41 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 42 base::Bind(&MockBrlapiConnection::NotifyDataReady, |
| 43 base::Unretained(this))); |
| 44 } |
| 45 return true; |
| 46 } |
| 47 |
| 48 virtual void Disconnect() OVERRIDE { |
| 49 data_->connected = false; |
| 50 } |
| 51 |
| 52 virtual bool Connected() OVERRIDE { |
| 53 return data_->connected; |
| 54 } |
| 55 |
| 56 virtual brlapi_error_t* BrlapiError() OVERRIDE { |
| 57 return &data_->error; |
| 58 } |
| 59 |
| 60 virtual std::string BrlapiStrError() OVERRIDE { |
| 61 return data_->error.brlerrno != BRLAPI_ERROR_SUCCESS ? "Error" : "Success"; |
| 62 } |
| 63 |
| 64 virtual bool GetDisplaySize(size_t* size) OVERRIDE { |
| 65 *size = data_->display_size; |
| 66 return true; |
| 67 } |
| 68 |
| 69 virtual bool WriteDots(const unsigned char* cells) OVERRIDE { |
| 70 std::string written(reinterpret_cast<const char*>(cells), |
| 71 data_->display_size); |
| 72 data_->written_content.push_back(written); |
| 73 return true; |
| 74 } |
| 75 |
| 76 virtual int ReadKey(brlapi_keyCode_t* keyCode) { |
| 77 if (!data_->pending_keys.empty()) { |
| 78 *keyCode = data_->pending_keys.front(); |
| 79 data_->pending_keys.pop_front(); |
| 80 return 1; |
| 81 } else { |
| 82 return 0; |
| 83 } |
| 84 } |
| 85 |
| 86 private: |
| 87 |
| 88 void NotifyDataReady() { |
| 89 on_data_ready_.Run(); |
| 90 if (!data_->pending_keys.empty()) { |
| 91 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 92 base::Bind(&MockBrlapiConnection::NotifyDataReady, |
| 93 base::Unretained(this))); |
| 94 } |
| 95 } |
| 96 |
| 97 MockBrlapiConnectionData* data_; |
| 98 OnDataReadyCallback on_data_ready_; |
| 99 }; |
| 100 |
| 101 class BrailleDisplayPrivateApiTest : public ExtensionApiTest { |
| 102 public: |
| 103 virtual void SetUpInProcessBrowserTestFixture() { |
| 104 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); |
| 105 connection_data_.connected = false; |
| 106 connection_data_.display_size = 0; |
| 107 connection_data_.error.brlerrno = BRLAPI_ERROR_SUCCESS; |
| 108 BrailleController::GetInstance()->SetCreateBrlapiConnectionForTesting( |
| 109 base::Bind( |
| 110 &BrailleDisplayPrivateApiTest::CreateBrlapiConnection, |
| 111 base::Unretained(this))); |
| 112 } |
| 113 |
| 114 protected: |
| 115 MockBrlapiConnectionData connection_data_; |
| 116 |
| 117 private: |
| 118 scoped_ptr<BrlapiConnection> CreateBrlapiConnection() { |
| 119 return scoped_ptr<BrlapiConnection>( |
| 120 new MockBrlapiConnection(&connection_data_)); |
| 121 } |
| 122 }; |
| 123 |
| 124 IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateApiTest, WriteDots) { |
| 125 connection_data_.display_size = 11; |
| 126 ASSERT_TRUE(RunComponentExtensionTest("braille_display_private/write_dots")) |
| 127 << message_; |
| 128 ASSERT_EQ(3U, connection_data_.written_content.size()); |
| 129 const std::string expected_content(connection_data_.display_size, '\0'); |
| 130 for (size_t i = 0; i < connection_data_.written_content.size(); ++i) { |
| 131 ASSERT_EQ(std::string( |
| 132 connection_data_.display_size, |
| 133 static_cast<char>(i)), |
| 134 connection_data_.written_content[i]) |
| 135 << "String " << i << " doesn't match"; |
| 136 } |
| 137 } |
| 138 |
| 139 IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateApiTest, KeyEvents) { |
| 140 connection_data_.display_size = 11; |
| 141 connection_data_.pending_keys.push_back( |
| 142 BRLAPI_KEY_TYPE_CMD | BRLAPI_KEY_CMD_LNUP); |
| 143 connection_data_.pending_keys.push_back( |
| 144 BRLAPI_KEY_TYPE_CMD | BRLAPI_KEY_CMD_LNDN); |
| 145 ASSERT_TRUE(RunComponentExtensionTest("braille_display_private/key_events")); |
| 146 } |
OLD | NEW |