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() sends an async request to the browser process. As a result, the | |
15 // string written may not be reflected by IsFormatAvailable() or ReadPlainText() | |
16 // 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(PPB_FLASH_CLIPBOARD_INTERFACE)); |
22 return !!clipboard_interface_; | 28 return !!clipboard_interface_; |
23 } | 29 } |
24 | 30 |
25 void TestFlashClipboard::RunTests(const std::string& filter) { | 31 void TestFlashClipboard::RunTests(const std::string& filter) { |
26 RUN_TEST(ReadWrite, filter); | 32 RUN_TEST(ReadWritePlainText, filter); |
33 RUN_TEST(ReadWriteHTML, filter); | |
34 RUN_TEST(ReadWriteMultipleFormats, filter); | |
27 } | 35 } |
28 | 36 |
29 std::string TestFlashClipboard::TestReadWrite() { | 37 PP_Bool TestFlashClipboard::IsFormatAvailable( |
30 std::string input_str("Hello, world"); | 38 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; | 39 PP_Bool is_available = PP_FALSE; |
43 for (int i = 0; i < kMaxIntervals; ++i) { | 40 for (int i = 0; i < kMaxIntervals; ++i) { |
44 is_available = clipboard_interface_->IsFormatAvailable( | 41 is_available = clipboard_interface_->IsFormatAvailable( |
45 instance_->pp_instance(), | 42 instance_->pp_instance(), |
46 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | 43 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
47 PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT); | 44 format); |
48 if (is_available) | 45 if (is_available) |
49 break; | 46 break; |
50 | 47 |
51 PlatformSleep(kIntervalMs); | 48 PlatformSleep(kIntervalMs); |
52 } | 49 } |
53 ASSERT_TRUE(is_available); | 50 return is_available; |
51 } | |
54 | 52 |
53 std::string TestFlashClipboard::ReadStringVar( | |
54 PP_Flash_Clipboard_Format format) { | |
55 std::string result_str; | 55 std::string result_str; |
56 pp::Var result_var( | |
57 pp::Var::PassRef(), | |
58 clipboard_interface_->ReadData(instance_->pp_instance(), | |
59 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | |
60 format)); | |
61 if (result_var.is_string()) | |
62 result_str = result_var.AsString(); | |
63 return result_str; | |
64 } | |
65 | |
66 int32_t TestFlashClipboard::WriteStringVar(PP_Flash_Clipboard_Format format, | |
67 const std::string& input) { | |
68 pp::Var input_var(input); | |
69 PP_Var data_item = input_var.pp_var(); | |
70 int32_t success = clipboard_interface_->WriteData( | |
71 instance_->pp_instance(), | |
72 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | |
73 1, | |
74 &format, | |
75 &data_item); | |
76 return success; | |
77 } | |
78 | |
79 bool TestFlashClipboard::ReadAndMatchPlainText(const std::string& input) { | |
56 for (int i = 0; i < kMaxIntervals; ++i) { | 80 for (int i = 0; i < kMaxIntervals; ++i) { |
57 pp::Var result_var(pp::Var::PassRef(), | 81 if (ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT) == input) { |
58 clipboard_interface_->ReadPlainText(instance_->pp_instance(), | 82 return true; |
59 PP_FLASH_CLIPBOARD_TYPE_STANDARD)); | 83 } |
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); | 84 PlatformSleep(kIntervalMs); |
66 } | 85 } |
86 return false; | |
87 } | |
67 | 88 |
68 ASSERT_TRUE(result_str == input_str); | 89 bool TestFlashClipboard::ReadAndMatchHTML(const std::string& input) { |
90 for (int i = 0; i < kMaxIntervals; ++i) { | |
91 std::string result = ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML); | |
92 // Markup is inserted around the copied html, so just check that | |
93 // the pasted string contains the copied string. | |
94 bool match = result.find(input) != std::string::npos; | |
95 if (match) { | |
96 return true; | |
97 } | |
98 PlatformSleep(kIntervalMs); | |
99 } | |
100 return false; | |
101 } | |
102 | |
103 std::string TestFlashClipboard::TestReadWritePlainText() { | |
104 std::string input = "Hello world plain text!"; | |
105 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, | |
106 input) == PP_OK); | |
107 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT)); | |
108 ASSERT_TRUE(ReadAndMatchPlainText(input)); | |
109 | |
69 PASS(); | 110 PASS(); |
70 } | 111 } |
112 | |
113 std::string TestFlashClipboard::TestReadWriteHTML() { | |
114 std::string input = "Hello world html!"; | |
115 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML, | |
116 input) == PP_OK); | |
117 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_HTML)); | |
118 ASSERT_TRUE(ReadAndMatchHTML(input)); | |
119 | |
120 PASS(); | |
121 } | |
122 | |
123 std::string TestFlashClipboard::TestReadWriteMultipleFormats() { | |
124 pp::Var plain_text_var("plain text"); | |
125 pp::Var html_var("html"); | |
126 PP_Flash_Clipboard_Format formats[] = { PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, | |
127 PP_FLASH_CLIPBOARD_FORMAT_HTML }; | |
dmichael (off chromium)
2012/02/24 19:35:44
style nits: I would probably break the line just a
raymes
2012/02/24 21:38:17
Done.
| |
128 PP_Var data_items[] = { plain_text_var.pp_var(), html_var.pp_var() }; | |
129 int32_t success = clipboard_interface_->WriteData( | |
130 instance_->pp_instance(), | |
131 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | |
132 sizeof(data_items) / sizeof(*data_items), | |
133 formats, | |
134 data_items); | |
135 ASSERT_TRUE(success == PP_OK); | |
136 | |
137 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT)); | |
138 ASSERT_TRUE(IsFormatAvailable(PP_FLASH_CLIPBOARD_FORMAT_HTML)); | |
139 | |
140 ASSERT_TRUE(ReadAndMatchPlainText(plain_text_var.AsString())); | |
141 ASSERT_TRUE(ReadAndMatchHTML(html_var.AsString())); | |
142 | |
143 PASS(); | |
144 } | |
OLD | NEW |