Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/tests/test_flash_clipboard.h" | 5 #include "ppapi/tests/test_flash_clipboard.h" |
| 6 | 6 |
| 7 #include "ppapi/cpp/instance.h" | 7 #include "ppapi/cpp/instance.h" |
| 8 #include "ppapi/cpp/module.h" | 8 #include "ppapi/cpp/module.h" |
| 9 #include "ppapi/cpp/point.h" | 9 #include "ppapi/cpp/point.h" |
| 10 #include "ppapi/tests/testing_instance.h" | 10 #include "ppapi/tests/testing_instance.h" |
| 11 | 11 |
| 12 REGISTER_TEST_CASE(FlashClipboard); | 12 REGISTER_TEST_CASE(FlashClipboard); |
| 13 | 13 |
| 14 // WriteData() causes an async request sent to the browser process. | |
|
dmichael (off chromium)
2012/02/23 18:13:25
nit: request "to be" sent?
Or just say "sends an a
raymes
2012/02/24 07:28:28
Done.
| |
| 15 // As a result, the string written may not be reflected by IsFormatAvailable() | |
| 16 // or ReadPlainText() immediately. We need to wait and retry. | |
| 17 const int kIntervalMs = 250; | |
| 18 const int kMaxIntervals = kActionTimeoutMs / kIntervalMs; | |
| 19 | |
| 14 TestFlashClipboard::TestFlashClipboard(TestingInstance* instance) | 20 TestFlashClipboard::TestFlashClipboard(TestingInstance* instance) |
| 15 : TestCase(instance), | 21 : TestCase(instance), |
| 16 clipboard_interface_(NULL) { | 22 clipboard_interface_(NULL) { |
| 17 } | 23 } |
| 18 | 24 |
| 19 bool TestFlashClipboard::Init() { | 25 bool TestFlashClipboard::Init() { |
| 20 clipboard_interface_ = static_cast<const PPB_Flash_Clipboard*>( | 26 clipboard_interface_ = static_cast<const PPB_Flash_Clipboard*>( |
| 21 pp::Module::Get()->GetBrowserInterface(PPB_FLASH_CLIPBOARD_INTERFACE)); | 27 pp::Module::Get()->GetBrowserInterface( |
| 28 PPB_FLASH_CLIPBOARD_INTERFACE)); | |
| 22 return !!clipboard_interface_; | 29 return !!clipboard_interface_; |
| 23 } | 30 } |
| 24 | 31 |
| 25 void TestFlashClipboard::RunTests(const std::string& filter) { | 32 void TestFlashClipboard::RunTests(const std::string& filter) { |
| 26 RUN_TEST(ReadWrite, filter); | 33 RUN_TEST(ReadWritePlainText, filter); |
| 34 RUN_TEST(ReadWriteHTML, filter); | |
| 35 RUN_TEST(ReadWriteMultipleFormats, filter); | |
|
dmichael (off chromium)
2012/02/23 18:13:25
Don't you also need to change ppapi_uitest.cc to g
raymes
2012/02/24 07:28:28
I don't know about that, they seem to be running a
dmichael (off chromium)
2012/02/24 19:35:44
You're right. There are two ways to run tests in p
raymes
2012/02/24 21:38:17
Ah I see, thanks for the explanation.
On 2012/02/
| |
| 27 } | 36 } |
| 28 | 37 |
| 29 std::string TestFlashClipboard::TestReadWrite() { | 38 PP_Bool TestFlashClipboard::IsFormatAvailable( |
| 30 std::string input_str("Hello, world"); | 39 PP_Flash_Clipboard_Format format) { |
| 31 pp::Var input_var(input_str); | |
| 32 clipboard_interface_->WritePlainText(instance_->pp_instance(), | |
| 33 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | |
| 34 input_var.pp_var()); | |
| 35 | |
| 36 // WritePlainText() causes an async request sent to the browser process. | |
| 37 // As a result, the string written may not be reflected by IsFormatAvailable() | |
| 38 // or ReadPlainText() immediately. We need to wait and retry. | |
| 39 const int kIntervalMs = 250; | |
| 40 const int kMaxIntervals = kActionTimeoutMs / kIntervalMs; | |
| 41 | |
| 42 PP_Bool is_available = PP_FALSE; | 40 PP_Bool is_available = PP_FALSE; |
| 43 for (int i = 0; i < kMaxIntervals; ++i) { | 41 for (int i = 0; i < kMaxIntervals; ++i) { |
| 44 is_available = clipboard_interface_->IsFormatAvailable( | 42 is_available = clipboard_interface_->IsFormatAvailable( |
| 45 instance_->pp_instance(), | 43 instance_->pp_instance(), |
| 46 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | 44 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
| 47 PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT); | 45 format); |
| 48 if (is_available) | 46 if (is_available) |
| 49 break; | 47 break; |
| 50 | 48 |
| 51 PlatformSleep(kIntervalMs); | 49 PlatformSleep(kIntervalMs); |
| 52 } | 50 } |
| 53 ASSERT_TRUE(is_available); | 51 return is_available; |
| 52 } | |
| 54 | 53 |
| 54 std::string TestFlashClipboard::ReadStringVar( | |
| 55 PP_Flash_Clipboard_Format format) { | |
| 55 std::string result_str; | 56 std::string result_str; |
| 57 pp::Var result_var(pp::Var::PassRef(), | |
|
dmichael (off chromium)
2012/02/23 18:13:25
style nit: I'd usually put the first param on the
raymes
2012/02/24 07:28:28
Done.
| |
| 58 clipboard_interface_->ReadData(instance_->pp_instance(), | |
| 59 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | |
| 60 format)); | |
| 61 | |
|
dmichael (off chromium)
2012/02/23 18:13:25
style nit: unnecessary blank line. Blank line on 6
raymes
2012/02/24 07:28:28
Done.
| |
| 62 if (result_var.is_string()) { | |
| 63 result_str = result_var.AsString(); | |
| 64 } | |
|
dmichael (off chromium)
2012/02/23 18:13:25
style nit: maybe no {} here to be consistent with
raymes
2012/02/24 07:28:28
Done.
| |
| 65 | |
| 66 return result_str; | |
| 67 } | |
| 68 | |
| 69 int32_t TestFlashClipboard::WriteStringVar(PP_Flash_Clipboard_Format format, | |
| 70 const std::string& input) { | |
| 71 pp::Var input_var(input); | |
| 72 PP_Flash_Clipboard_Data_Item item = {format, input_var.pp_var()}; | |
|
dmichael (off chromium)
2012/02/23 18:13:25
style nit: space after { and before }
raymes
2012/02/24 07:28:28
Done.
| |
| 73 int32_t success = clipboard_interface_->WriteData( | |
| 74 instance_->pp_instance(), | |
| 75 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | |
| 76 1, | |
| 77 &item); | |
|
dmichael (off chromium)
2012/02/23 18:13:25
Does WriteData expect to be passed a reference cou
raymes
2012/02/24 07:28:28
Not sure what you mean exactly. WriteData finishes
dmichael (off chromium)
2012/02/24 19:35:44
I guess I wanted to make sure you had thought abou
| |
| 78 return success; | |
| 79 } | |
| 80 | |
| 81 bool TestFlashClipboard::ReadAndMatchPlainText(const std::string& input) { | |
| 56 for (int i = 0; i < kMaxIntervals; ++i) { | 82 for (int i = 0; i < kMaxIntervals; ++i) { |
| 57 pp::Var result_var(pp::Var::PassRef(), | 83 if (ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT) == input) { |
| 58 clipboard_interface_->ReadPlainText(instance_->pp_instance(), | 84 return true; |
| 59 PP_FLASH_CLIPBOARD_TYPE_STANDARD)); | 85 } |
| 60 ASSERT_TRUE(result_var.is_string()); | |
| 61 result_str = result_var.AsString(); | |
| 62 if (result_str == input_str) | |
| 63 break; | |
| 64 | |
| 65 PlatformSleep(kIntervalMs); | 86 PlatformSleep(kIntervalMs); |
| 66 } | 87 } |
| 88 return false; | |
| 89 } | |
| 67 | 90 |
| 68 ASSERT_TRUE(result_str == input_str); | 91 bool TestFlashClipboard::ReadAndMatchHTML(const std::string& input) { |
| 92 for (int i = 0; i < kMaxIntervals; ++i) { | |
| 93 std::string result = ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML); | |
| 94 // Markup is inserted around the copied html, so just check that | |
| 95 // the pasted string contains the copied string. | |
| 96 bool match = result.find(input) != std::string::npos; | |
| 97 if (match) { | |
| 98 return true; | |
| 99 } | |
| 100 PlatformSleep(kIntervalMs); | |
| 101 } | |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 std::string TestFlashClipboard::TestReadWritePlainText() { | |
| 106 std::string input = "Hello world plain text!"; | |
| 107 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, | |
| 108 input) == PP_OK); | |
| 109 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT)); | |
| 110 ASSERT_TRUE(ReadAndMatchPlainText(input)); | |
| 111 | |
| 69 PASS(); | 112 PASS(); |
| 70 } | 113 } |
| 114 | |
| 115 std::string TestFlashClipboard::TestReadWriteHTML() { | |
| 116 std::string input = "Hello world html!"; | |
| 117 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML, | |
| 118 input) == PP_OK); | |
| 119 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_HTML)); | |
| 120 ASSERT_TRUE(ReadAndMatchHTML(input)); | |
| 121 | |
| 122 PASS(); | |
| 123 } | |
| 124 | |
| 125 std::string TestFlashClipboard::TestReadWriteMultipleFormats() { | |
| 126 std::string plain_text("plain text"); | |
| 127 std::string html("html"); | |
| 128 pp::Var plain_text_var(plain_text); | |
| 129 pp::Var html_var(html); | |
|
dmichael (off chromium)
2012/02/23 18:13:25
nit: I think you could do each of these in one lin
raymes
2012/02/24 07:28:28
Done.
| |
| 130 PP_Flash_Clipboard_Data_Item items[] = { | |
| 131 {PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, plain_text_var.pp_var()}, | |
| 132 {PP_FLASH_CLIPBOARD_FORMAT_HTML, html_var.pp_var()} | |
| 133 }; | |
| 134 int32_t success = clipboard_interface_->WriteData( | |
| 135 instance_->pp_instance(), | |
| 136 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | |
| 137 2, | |
|
dmichael (off chromium)
2012/02/23 18:13:25
nit: best practice would probably be to compute th
raymes
2012/02/24 07:28:28
Done.
| |
| 138 items); | |
| 139 ASSERT_TRUE(success == PP_OK); | |
| 140 | |
| 141 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT)); | |
| 142 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_HTML)); | |
| 143 | |
| 144 ASSERT_TRUE(ReadAndMatchPlainText(plain_text)); | |
| 145 ASSERT_TRUE(ReadAndMatchHTML(html)); | |
| 146 | |
| 147 PASS(); | |
| 148 } | |
| OLD | NEW |