clipboard_history_controller_unittest.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // Copyright 2020 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. #include "ash/clipboard/clipboard_history_controller_impl.h"
  5. #include <memory>
  6. #include <vector>
  7. #include "ash/app_list/app_list_controller_impl.h"
  8. #include "ash/clipboard/clipboard_history.h"
  9. #include "ash/public/cpp/clipboard_image_model_factory.h"
  10. #include "ash/public/cpp/session/session_types.h"
  11. #include "ash/session/session_controller_impl.h"
  12. #include "ash/shell.h"
  13. #include "ash/test/ash_test_base.h"
  14. #include "base/location.h"
  15. #include "base/run_loop.h"
  16. #include "base/strings/utf_string_conversions.h"
  17. #include "base/test/metrics/histogram_tester.h"
  18. #include "base/test/repeating_test_future.h"
  19. #include "base/test/test_future.h"
  20. #include "base/threading/sequenced_task_runner_handle.h"
  21. #include "base/unguessable_token.h"
  22. #include "testing/gmock/include/gmock/gmock.h"
  23. #include "testing/gtest/include/gtest/gtest.h"
  24. #include "ui/base/clipboard/clipboard_buffer.h"
  25. #include "ui/base/clipboard/clipboard_data.h"
  26. #include "ui/base/clipboard/scoped_clipboard_writer.h"
  27. #include "ui/base/models/image_model.h"
  28. #include "ui/base/webui/web_ui_util.h"
  29. #include "ui/events/test/event_generator.h"
  30. #include "ui/gfx/codec/png_codec.h"
  31. #include "ui/gfx/image/image_unittest_util.h"
  32. namespace ash {
  33. namespace {
  34. void FlushMessageLoop() {
  35. base::RunLoop run_loop;
  36. base::SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE,
  37. run_loop.QuitClosure());
  38. run_loop.Run();
  39. }
  40. class MockClipboardImageModelFactory : public ClipboardImageModelFactory {
  41. public:
  42. MockClipboardImageModelFactory() = default;
  43. MockClipboardImageModelFactory(const MockClipboardImageModelFactory&) =
  44. delete;
  45. MockClipboardImageModelFactory& operator=(
  46. const MockClipboardImageModelFactory&) = delete;
  47. ~MockClipboardImageModelFactory() override = default;
  48. // ClipboardImageModelFactory:
  49. void Render(const base::UnguessableToken& clipboard_history_item_id,
  50. const std::string& markup,
  51. const gfx::Size& bounding_box_size,
  52. ImageModelCallback callback) override {
  53. std::move(callback).Run(ui::ImageModel());
  54. }
  55. void CancelRequest(const base::UnguessableToken& request_id) override {}
  56. void Activate() override {}
  57. void Deactivate() override {}
  58. void RenderCurrentPendingRequests() override {}
  59. void OnShutdown() override {}
  60. };
  61. void ExpectHistoryValueMatchesBitmap(const base::Value::Dict* value,
  62. const SkBitmap& expected_bitmap) {
  63. ASSERT_TRUE(value);
  64. auto* format = value->FindString("displayFormat");
  65. ASSERT_TRUE(format);
  66. EXPECT_EQ("png", *format);
  67. auto* image_data = value->FindString("imageData");
  68. ASSERT_TRUE(image_data);
  69. auto png = ui::ClipboardData::EncodeBitmapData(expected_bitmap);
  70. std::string png_data_url = webui::GetPngDataUrl(png.data(), png.size());
  71. EXPECT_EQ(png_data_url, *image_data);
  72. }
  73. } // namespace
  74. class ClipboardHistoryControllerTest : public AshTestBase {
  75. public:
  76. ClipboardHistoryControllerTest() = default;
  77. ClipboardHistoryControllerTest(const ClipboardHistoryControllerTest&) =
  78. delete;
  79. ClipboardHistoryControllerTest& operator=(
  80. const ClipboardHistoryControllerTest&) = delete;
  81. ~ClipboardHistoryControllerTest() override = default;
  82. // AshTestBase:
  83. void SetUp() override {
  84. AshTestBase::SetUp();
  85. mock_image_factory_ = std::make_unique<MockClipboardImageModelFactory>();
  86. GetClipboardHistoryController()->set_confirmed_operation_callback_for_test(
  87. operation_confirmed_future_.GetCallback());
  88. }
  89. ClipboardHistoryControllerImpl* GetClipboardHistoryController() {
  90. return Shell::Get()->clipboard_history_controller();
  91. }
  92. void ShowMenu() { PressAndReleaseKey(ui::VKEY_V, ui::EF_COMMAND_DOWN); }
  93. void WaitForOperationConfirmed() {
  94. EXPECT_TRUE(operation_confirmed_future_.Take());
  95. }
  96. void WriteImageToClipboardAndConfirm(const SkBitmap& bitmap) {
  97. {
  98. ui::ScopedClipboardWriter scw(ui::ClipboardBuffer::kCopyPaste);
  99. scw.WriteImage(bitmap);
  100. }
  101. WaitForOperationConfirmed();
  102. }
  103. void WriteTextToClipboardAndConfirm(const std::u16string& str) {
  104. {
  105. ui::ScopedClipboardWriter scw(ui::ClipboardBuffer::kCopyPaste);
  106. scw.WriteText(str);
  107. }
  108. WaitForOperationConfirmed();
  109. }
  110. base::Value::List GetHistoryValues() {
  111. base::test::TestFuture<base::Value> future;
  112. GetClipboardHistoryController()->GetHistoryValuesForTest(
  113. future.GetCallback());
  114. auto result = future.Take();
  115. EXPECT_TRUE(result.is_list());
  116. return std::move(result.GetList());
  117. }
  118. void TestEnteringLockScreen() {
  119. // Querying clipboard history should return nothing if the screen is locked
  120. // while the request is in progress.
  121. GetClipboardHistoryController()->BlockGetHistoryValuesForTest();
  122. base::test::TestFuture<base::Value> future;
  123. GetClipboardHistoryController()->GetHistoryValuesForTest(
  124. future.GetCallback());
  125. EXPECT_FALSE(future.IsReady());
  126. auto* session_controller = Shell::Get()->session_controller();
  127. session_controller->LockScreen();
  128. GetSessionControllerClient()->FlushForTest(); // `LockScreen()` is async.
  129. EXPECT_TRUE(session_controller->IsScreenLocked());
  130. GetClipboardHistoryController()->ResumeGetHistoryValuesForTest();
  131. auto* locked_during_query_result = future.Get().GetIfList();
  132. ASSERT_TRUE(locked_during_query_result);
  133. EXPECT_EQ(0u, locked_during_query_result->size());
  134. // Querying clipboard history should return nothing if the screen is locked
  135. // before the request is made.
  136. auto locked_before_query_result = GetHistoryValues();
  137. EXPECT_EQ(0u, locked_before_query_result.size());
  138. }
  139. protected:
  140. base::test::RepeatingTestFuture<bool> operation_confirmed_future_;
  141. private:
  142. std::unique_ptr<MockClipboardImageModelFactory> mock_image_factory_;
  143. };
  144. // Tests that search + v with no history fails to show a menu.
  145. TEST_F(ClipboardHistoryControllerTest, NoHistoryNoMenu) {
  146. base::HistogramTester histogram_tester;
  147. ShowMenu();
  148. EXPECT_FALSE(GetClipboardHistoryController()->IsMenuShowing());
  149. histogram_tester.ExpectTotalCount(
  150. "Ash.ClipboardHistory.ContextMenu.NumberOfItemsShown", 0);
  151. histogram_tester.ExpectTotalCount(
  152. "Ash.ClipboardHistory.ContextMenu.UserJourneyTime", 0);
  153. histogram_tester.ExpectTotalCount(
  154. "Ash.ClipboardHistory.ContextMenu.DisplayFormatShown", 0);
  155. }
  156. // Tests that search + v shows a menu when there is something to show.
  157. TEST_F(ClipboardHistoryControllerTest, MultiShowMenu) {
  158. base::HistogramTester histogram_tester;
  159. // Copy something to enable the clipboard history menu.
  160. WriteTextToClipboardAndConfirm(u"test");
  161. ShowMenu();
  162. EXPECT_TRUE(GetClipboardHistoryController()->IsMenuShowing());
  163. histogram_tester.ExpectBucketCount(
  164. "Ash.ClipboardHistory.ContextMenu.NumberOfItemsShown", 1, 1);
  165. // No UserJourneyTime should be recorded as the menu is still showing.
  166. histogram_tester.ExpectTotalCount(
  167. "Ash.ClipboardHistory.ContextMenu.UserJourneyTime", 0);
  168. histogram_tester.ExpectBucketCount(
  169. "Ash.ClipboardHistory.ContextMenu.NumberOfItemsShown", 1, 1);
  170. histogram_tester.ExpectTotalCount(
  171. "Ash.ClipboardHistory.ContextMenu.DisplayFormatShown", 1);
  172. // Hide the menu.
  173. PressAndReleaseKey(ui::VKEY_ESCAPE);
  174. EXPECT_FALSE(GetClipboardHistoryController()->IsMenuShowing());
  175. histogram_tester.ExpectBucketCount(
  176. "Ash.ClipboardHistory.ContextMenu.NumberOfItemsShown", 1, 1);
  177. histogram_tester.ExpectTotalCount(
  178. "Ash.ClipboardHistory.ContextMenu.UserJourneyTime", 1);
  179. histogram_tester.ExpectTotalCount(
  180. "Ash.ClipboardHistory.ContextMenu.DisplayFormatShown", 1);
  181. // Reshow the menu.
  182. ShowMenu();
  183. EXPECT_TRUE(GetClipboardHistoryController()->IsMenuShowing());
  184. histogram_tester.ExpectBucketCount(
  185. "Ash.ClipboardHistory.ContextMenu.NumberOfItemsShown", 1, 2);
  186. // No new UserJourneyTime histogram should be recorded as the menu is
  187. // still showing.
  188. histogram_tester.ExpectTotalCount(
  189. "Ash.ClipboardHistory.ContextMenu.UserJourneyTime", 1);
  190. histogram_tester.ExpectBucketCount(
  191. "Ash.ClipboardHistory.ContextMenu.NumberOfItemsShown", 1, 2);
  192. histogram_tester.ExpectTotalCount(
  193. "Ash.ClipboardHistory.ContextMenu.DisplayFormatShown", 2);
  194. // Hide the menu.
  195. PressAndReleaseKey(ui::VKEY_ESCAPE);
  196. EXPECT_FALSE(GetClipboardHistoryController()->IsMenuShowing());
  197. histogram_tester.ExpectTotalCount(
  198. "Ash.ClipboardHistory.ContextMenu.UserJourneyTime", 2);
  199. }
  200. // Verifies that the clipboard history is disabled in some user modes, which
  201. // means that the clipboard history should not be recorded and meanwhile the
  202. // menu view should not show (https://crbug.com/1100739).
  203. TEST_F(ClipboardHistoryControllerTest, VerifyAvailabilityInUserModes) {
  204. // Write one item into the clipboard history.
  205. WriteTextToClipboardAndConfirm(u"text");
  206. constexpr struct {
  207. user_manager::UserType user_type;
  208. bool is_enabled;
  209. } kTestCases[] = {{user_manager::USER_TYPE_REGULAR, true},
  210. {user_manager::USER_TYPE_GUEST, true},
  211. {user_manager::USER_TYPE_PUBLIC_ACCOUNT, false},
  212. {user_manager::USER_TYPE_KIOSK_APP, false},
  213. {user_manager::USER_TYPE_CHILD, true},
  214. {user_manager::USER_TYPE_ARC_KIOSK_APP, false},
  215. {user_manager::USER_TYPE_WEB_KIOSK_APP, false}};
  216. UserSession session;
  217. session.session_id = 1u;
  218. session.user_info.account_id = AccountId::FromUserEmail("user1@test.com");
  219. session.user_info.display_name = "User 1";
  220. session.user_info.display_email = "user1@test.com";
  221. for (const auto& test_case : kTestCases) {
  222. // Switch to the target user mode.
  223. session.user_info.type = test_case.user_type;
  224. Shell::Get()->session_controller()->UpdateUserSession(session);
  225. // Write a new item into the clipboard buffer.
  226. {
  227. ui::ScopedClipboardWriter scw(ui::ClipboardBuffer::kCopyPaste);
  228. scw.WriteText(u"test");
  229. }
  230. if (test_case.is_enabled) {
  231. WaitForOperationConfirmed();
  232. } else {
  233. FlushMessageLoop();
  234. // Note: This check might not catch a scenario where a mode expected to be
  235. // disabled actually allows writes to go through, because the operation
  236. // might not have finished yet in that case. The history verification
  237. // below mitigates the chance that such a bug would not be caught.
  238. EXPECT_TRUE(operation_confirmed_future_.IsEmpty());
  239. }
  240. const std::list<ClipboardHistoryItem>& items =
  241. Shell::Get()->clipboard_history_controller()->history()->GetItems();
  242. if (test_case.is_enabled) {
  243. // Verify that the new item should be included in the clipboard history
  244. // and the menu should be able to show.
  245. EXPECT_EQ(2u, items.size());
  246. ShowMenu();
  247. EXPECT_TRUE(
  248. Shell::Get()->clipboard_history_controller()->IsMenuShowing());
  249. PressAndReleaseKey(ui::VKEY_ESCAPE);
  250. EXPECT_FALSE(
  251. Shell::Get()->clipboard_history_controller()->IsMenuShowing());
  252. // Restore the clipboard history by removing the new item.
  253. ClipboardHistory* clipboard_history = const_cast<ClipboardHistory*>(
  254. Shell::Get()->clipboard_history_controller()->history());
  255. clipboard_history->RemoveItemForId(items.begin()->id());
  256. } else {
  257. // Verify that the new item should not be written into the clipboard
  258. // history. The menu cannot show although the clipboard history is
  259. // non-empty.
  260. EXPECT_EQ(1u, items.size());
  261. ShowMenu();
  262. EXPECT_FALSE(
  263. Shell::Get()->clipboard_history_controller()->IsMenuShowing());
  264. }
  265. }
  266. }
  267. // Verifies that the clipboard history menu is disabled when the screen for
  268. // user adding shows.
  269. TEST_F(ClipboardHistoryControllerTest, DisableInUserAddingScreen) {
  270. WriteTextToClipboardAndConfirm(u"text");
  271. // Emulate that the user adding screen displays.
  272. Shell::Get()->session_controller()->ShowMultiProfileLogin();
  273. // Try to show the clipboard history menu; verify that the menu does not show.
  274. ShowMenu();
  275. EXPECT_FALSE(Shell::Get()->clipboard_history_controller()->IsMenuShowing());
  276. }
  277. // Tests that pressing and holding VKEY_V, then the search key (EF_COMMAND_DOWN)
  278. // does not show the AppList.
  279. TEST_F(ClipboardHistoryControllerTest, VThenSearchDoesNotShowLauncher) {
  280. GetEventGenerator()->PressKey(ui::VKEY_V, /*event_flags=*/0);
  281. GetEventGenerator()->PressKey(ui::VKEY_LWIN, /*event_flags=*/0);
  282. // Release VKEY_V, which could trigger a key released accelerator.
  283. GetEventGenerator()->ReleaseKey(ui::VKEY_V, /*event_flags=*/0);
  284. EXPECT_FALSE(Shell::Get()->app_list_controller()->IsVisible(
  285. /*display_id=*/absl::nullopt));
  286. // Release VKEY_LWIN(search/launcher), which could trigger the app list.
  287. GetEventGenerator()->ReleaseKey(ui::VKEY_LWIN, /*event_flags=*/0);
  288. EXPECT_FALSE(Shell::Get()->app_list_controller()->IsVisible(
  289. /*display_id=*/absl::nullopt));
  290. }
  291. // Tests that clearing the clipboard clears ClipboardHistory
  292. TEST_F(ClipboardHistoryControllerTest, ClearClipboardClearsHistory) {
  293. // Write a single item to ClipboardHistory.
  294. WriteTextToClipboardAndConfirm(u"test");
  295. // Clear the clipboard.
  296. ui::Clipboard::GetForCurrentThread()->Clear(ui::ClipboardBuffer::kCopyPaste);
  297. FlushMessageLoop();
  298. // History should also be cleared.
  299. const std::list<ClipboardHistoryItem>& items =
  300. Shell::Get()->clipboard_history_controller()->history()->GetItems();
  301. EXPECT_EQ(0u, items.size());
  302. ShowMenu();
  303. EXPECT_FALSE(GetClipboardHistoryController()->IsMenuShowing());
  304. }
  305. // Tests that clearing the clipboard closes the ClipboardHistory menu.
  306. TEST_F(ClipboardHistoryControllerTest,
  307. ClearingClipboardClosesClipboardHistory) {
  308. // Write a single item to ClipboardHistory.
  309. WriteTextToClipboardAndConfirm(u"test");
  310. ASSERT_TRUE(Shell::Get()->cursor_manager()->IsCursorVisible());
  311. ShowMenu();
  312. EXPECT_TRUE(GetClipboardHistoryController()->IsMenuShowing());
  313. // The cursor is visible after showing the clipboard history menu through
  314. // the accelerator.
  315. EXPECT_TRUE(Shell::Get()->cursor_manager()->IsCursorVisible());
  316. ui::Clipboard::GetForCurrentThread()->Clear(ui::ClipboardBuffer::kCopyPaste);
  317. FlushMessageLoop();
  318. EXPECT_FALSE(GetClipboardHistoryController()->IsMenuShowing());
  319. }
  320. TEST_F(ClipboardHistoryControllerTest, EncodeImage) {
  321. // Write a bitmap to ClipboardHistory.
  322. SkBitmap test_bitmap = gfx::test::CreateBitmap(3, 2);
  323. WriteImageToClipboardAndConfirm(test_bitmap);
  324. // The bitmap should be encoded to a PNG. Manually pry into the contents of
  325. // the result to confirm that the newly-encoded PNG is included.
  326. auto result = GetHistoryValues();
  327. EXPECT_EQ(1u, result.size());
  328. ExpectHistoryValueMatchesBitmap(result[0].GetIfDict(), test_bitmap);
  329. }
  330. TEST_F(ClipboardHistoryControllerTest, EncodeMultipleImages) {
  331. // Write a bunch of bitmaps to ClipboardHistory.
  332. std::vector<const SkBitmap> test_bitmaps;
  333. test_bitmaps.emplace_back(gfx::test::CreateBitmap(2, 1));
  334. test_bitmaps.emplace_back(gfx::test::CreateBitmap(3, 2));
  335. test_bitmaps.emplace_back(gfx::test::CreateBitmap(4, 3));
  336. for (const auto& test_bitmap : test_bitmaps)
  337. WriteImageToClipboardAndConfirm(test_bitmap);
  338. auto result = GetHistoryValues();
  339. auto num_results = result.size();
  340. EXPECT_EQ(num_results, test_bitmaps.size());
  341. // The bitmaps should be encoded to PNGs. Manually pry into the contents of
  342. // the result to confirm that the newly-encoded PNGs are included. History
  343. // values should be sorted by recency.
  344. for (uint i = 0; i < num_results; ++i) {
  345. ExpectHistoryValueMatchesBitmap(result[i].GetIfDict(),
  346. test_bitmaps[num_results - 1 - i]);
  347. }
  348. }
  349. TEST_F(ClipboardHistoryControllerTest, WriteBitmapWhileEncodingImage) {
  350. // Write a bitmap to ClipboardHistory.
  351. std::vector<const SkBitmap> test_bitmaps;
  352. test_bitmaps.emplace_back(gfx::test::CreateBitmap(3, 2));
  353. test_bitmaps.emplace_back(gfx::test::CreateBitmap(4, 3));
  354. WriteImageToClipboardAndConfirm(test_bitmaps[0]);
  355. // Write another bitmap to the clipboard while encoding the first bitmap.
  356. GetClipboardHistoryController()
  357. ->set_new_bitmap_to_write_while_encoding_for_test(test_bitmaps[1]);
  358. // Make sure the second bitmap is written to the clipboard before history
  359. // values are returned.
  360. GetClipboardHistoryController()->BlockGetHistoryValuesForTest();
  361. base::test::TestFuture<base::Value> future;
  362. GetClipboardHistoryController()->GetHistoryValuesForTest(
  363. future.GetCallback());
  364. EXPECT_FALSE(future.IsReady());
  365. WaitForOperationConfirmed();
  366. GetClipboardHistoryController()->ResumeGetHistoryValuesForTest();
  367. auto* result = future.Get().GetIfList();
  368. ASSERT_TRUE(result);
  369. auto num_results = result->size();
  370. EXPECT_EQ(num_results, test_bitmaps.size());
  371. // Both bitmaps should be encoded to PNGs. Manually pry into the contents of
  372. // the result to confirm that the newly-encoded PNGs are included. History
  373. // values should be sorted by recency.
  374. for (uint i = 0; i < num_results; ++i) {
  375. ExpectHistoryValueMatchesBitmap((*result)[i].GetIfDict(),
  376. test_bitmaps[num_results - 1 - i]);
  377. }
  378. }
  379. TEST_F(ClipboardHistoryControllerTest, LockedScreenText) {
  380. // Write text to ClipboardHistory and verify that it can be retrieved.
  381. WriteTextToClipboardAndConfirm(u"test");
  382. auto history_list_value = GetHistoryValues();
  383. EXPECT_EQ(1u, history_list_value.size());
  384. auto* history_list_item = history_list_value[0].GetIfDict();
  385. EXPECT_TRUE(history_list_item);
  386. auto* history_list_item_text = history_list_item->FindString("textData");
  387. EXPECT_TRUE(history_list_item_text);
  388. EXPECT_EQ("test", *history_list_item_text);
  389. TestEnteringLockScreen();
  390. }
  391. TEST_F(ClipboardHistoryControllerTest, LockedScreenImage) {
  392. // Write a bitmap to ClipboardHistory and verify that it can be returned.
  393. SkBitmap test_bitmap = gfx::test::CreateBitmap(3, 2);
  394. WriteImageToClipboardAndConfirm(test_bitmap);
  395. auto result = GetHistoryValues();
  396. EXPECT_EQ(1u, result.size());
  397. ExpectHistoryValueMatchesBitmap(result[0].GetIfDict(), test_bitmap);
  398. TestEnteringLockScreen();
  399. }
  400. } // namespace ash