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. |
| 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 TestFlashClipboard::kIntervalMs = 250; |
| 18 const int TestFlashClipboard::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); |
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; |
56 for (int i = 0; i < kMaxIntervals; ++i) { | 57 for (int i = 0; i < kMaxIntervals; ++i) { |
57 pp::Var result_var(pp::Var::PassRef(), | 58 pp::Var result_var(pp::Var::PassRef(), |
58 clipboard_interface_->ReadPlainText(instance_->pp_instance(), | 59 clipboard_interface_->ReadData(instance_->pp_instance(), |
59 PP_FLASH_CLIPBOARD_TYPE_STANDARD)); | 60 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
60 ASSERT_TRUE(result_var.is_string()); | 61 format)); |
61 result_str = result_var.AsString(); | 62 |
62 if (result_str == input_str) | 63 if (result_var.is_string()) { |
| 64 result_str = result_var.AsString(); |
63 break; | 65 break; |
| 66 } |
64 | 67 |
65 PlatformSleep(kIntervalMs); | 68 PlatformSleep(kIntervalMs); |
66 } | 69 } |
| 70 return result_str; |
| 71 } |
67 | 72 |
68 ASSERT_TRUE(result_str == input_str); | 73 int32_t TestFlashClipboard::WriteStringVar(PP_Flash_Clipboard_Format format, |
| 74 const std::string& input) { |
| 75 pp::Var input_var(input); |
| 76 PP_Flash_Clipboard_Data_Item item = {format, input_var.pp_var()}; |
| 77 int32_t success = clipboard_interface_->WriteData( |
| 78 instance_->pp_instance(), |
| 79 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
| 80 1, |
| 81 &item); |
| 82 return success; |
| 83 } |
| 84 |
| 85 std::string TestFlashClipboard::TestReadWritePlainText() { |
| 86 std::string input = "Hello world plain text!"; |
| 87 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, |
| 88 input) == PP_OK); |
| 89 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT)); |
| 90 ASSERT_TRUE(ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT) == input); |
| 91 |
69 PASS(); | 92 PASS(); |
70 } | 93 } |
| 94 |
| 95 std::string TestFlashClipboard::TestReadWriteHTML() { |
| 96 std::string input = "Hello world html!"; |
| 97 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML, |
| 98 input) == PP_OK); |
| 99 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_HTML)); |
| 100 std::string result = ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML); |
| 101 // Metadata is inserted at the start of pasted html clipboard data. So check |
| 102 // that it ends with what was copied in. |
| 103 int match = result.compare(result.length() - input.length(), |
| 104 input.length(), input); |
| 105 ASSERT_TRUE(match == 0); |
| 106 |
| 107 PASS(); |
| 108 } |
| 109 |
| 110 std::string TestFlashClipboard::TestReadWriteMultipleFormats() { |
| 111 std::string plain_text("plain text"); |
| 112 std::string html("html"); |
| 113 pp::Var plain_text_var(plain_text); |
| 114 pp::Var html_var(html); |
| 115 PP_Flash_Clipboard_Data_Item items[] = { |
| 116 {PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, plain_text_var.pp_var()}, |
| 117 {PP_FLASH_CLIPBOARD_FORMAT_HTML, html_var.pp_var()} |
| 118 }; |
| 119 int32_t success = clipboard_interface_->WriteData( |
| 120 instance_->pp_instance(), |
| 121 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
| 122 2, |
| 123 items); |
| 124 ASSERT_TRUE(success == PP_OK); |
| 125 |
| 126 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT)); |
| 127 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_HTML)); |
| 128 |
| 129 ASSERT_TRUE(ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT) == plain_text); |
| 130 std::string result_html = ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML); |
| 131 int match = result_html.compare(result_html.length() - html.length(), |
| 132 html.length(), html); |
| 133 ASSERT_TRUE(match == 0); |
| 134 |
| 135 PASS(); |
| 136 } |
OLD | NEW |