palette_tool_manager_unittest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2016 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 <memory>
  5. #include "ash/system/palette/palette_tool.h"
  6. #include "ash/system/palette/palette_tool_manager.h"
  7. #include "base/bind.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "base/notreached.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace ash {
  12. namespace {
  13. // A simple tool instance that exposes some additional data for testing.
  14. class TestTool : public PaletteTool {
  15. public:
  16. TestTool(Delegate* delegate, PaletteGroup group, PaletteToolId tool_id)
  17. : PaletteTool(delegate), group_(group), tool_id_(tool_id) {}
  18. TestTool(const TestTool&) = delete;
  19. TestTool& operator=(const TestTool&) = delete;
  20. // PaletteTool:
  21. PaletteGroup GetGroup() const override { return group_; }
  22. PaletteToolId GetToolId() const override { return tool_id_; }
  23. // Shadows the parent declaration since PaletteTool::enabled is not virtual.
  24. bool enabled() const { return PaletteTool::enabled(); }
  25. private:
  26. // PaletteTool:
  27. views::View* CreateView() override {
  28. NOTREACHED();
  29. return nullptr;
  30. }
  31. void OnViewDestroyed() override { FAIL(); }
  32. PaletteGroup group_;
  33. PaletteToolId tool_id_;
  34. };
  35. // Base class for tool manager unittests.
  36. class PaletteToolManagerTest : public ::testing::Test,
  37. public PaletteToolManager::Delegate,
  38. public PaletteTool::Delegate {
  39. public:
  40. PaletteToolManagerTest()
  41. : palette_tool_manager_(new PaletteToolManager(this)) {}
  42. PaletteToolManagerTest(const PaletteToolManagerTest&) = delete;
  43. PaletteToolManagerTest& operator=(const PaletteToolManagerTest&) = delete;
  44. ~PaletteToolManagerTest() override = default;
  45. protected:
  46. // PaletteToolManager::Delegate:
  47. void HidePalette() override {}
  48. void HidePaletteImmediately() override {}
  49. void OnActiveToolChanged() override { ++tool_changed_count_; }
  50. aura::Window* GetWindow() override {
  51. NOTREACHED();
  52. return nullptr;
  53. }
  54. void RecordPaletteOptionsUsage(PaletteTrayOptions option,
  55. PaletteInvocationMethod method) override {}
  56. void RecordPaletteModeCancellation(PaletteModeCancelType type) override {}
  57. // PaletteTool::Delegate:
  58. void EnableTool(PaletteToolId tool_id) override {}
  59. void DisableTool(PaletteToolId tool_id) override {}
  60. // Helper method for returning an unowned pointer to the constructed tool
  61. // while also adding it to the PaletteToolManager.
  62. TestTool* BuildTool(PaletteGroup group, PaletteToolId tool_id) {
  63. auto* tool = new TestTool(this, group, tool_id);
  64. palette_tool_manager_->AddTool(base::WrapUnique(tool));
  65. return tool;
  66. }
  67. int tool_changed_count_ = 0;
  68. std::unique_ptr<PaletteToolManager> palette_tool_manager_;
  69. };
  70. } // namespace
  71. // Verifies that tools can be enabled/disabled and that enabling a tool disables
  72. // only active tools in the same group.
  73. TEST_F(PaletteToolManagerTest, MultipleToolsActivateDeactivate) {
  74. // Register actions/modes.
  75. TestTool* action_1 =
  76. BuildTool(PaletteGroup::ACTION, PaletteToolId::CREATE_NOTE);
  77. TestTool* action_2 =
  78. BuildTool(PaletteGroup::ACTION, PaletteToolId::ENTER_CAPTURE_MODE);
  79. TestTool* mode_1 = BuildTool(PaletteGroup::MODE, PaletteToolId::MAGNIFY);
  80. EXPECT_FALSE(palette_tool_manager_->HasTool(PaletteToolId::LASER_POINTER));
  81. TestTool* mode_2 =
  82. BuildTool(PaletteGroup::MODE, PaletteToolId::LASER_POINTER);
  83. EXPECT_TRUE(palette_tool_manager_->HasTool(PaletteToolId::LASER_POINTER));
  84. // Enable mode 1.
  85. EXPECT_EQ(0, tool_changed_count_);
  86. palette_tool_manager_->ActivateTool(mode_1->GetToolId());
  87. EXPECT_FALSE(action_1->enabled());
  88. EXPECT_FALSE(action_2->enabled());
  89. EXPECT_TRUE(mode_1->enabled());
  90. EXPECT_FALSE(mode_2->enabled());
  91. // Turn a single action on/off. Enabling/disabling the tool does not change
  92. // any other group's state.
  93. palette_tool_manager_->ActivateTool(action_1->GetToolId());
  94. EXPECT_TRUE(action_1->enabled());
  95. EXPECT_FALSE(action_2->enabled());
  96. EXPECT_TRUE(mode_1->enabled());
  97. EXPECT_FALSE(mode_2->enabled());
  98. palette_tool_manager_->DeactivateTool(action_1->GetToolId());
  99. EXPECT_FALSE(action_1->enabled());
  100. EXPECT_FALSE(action_2->enabled());
  101. EXPECT_TRUE(mode_1->enabled());
  102. EXPECT_FALSE(mode_2->enabled());
  103. // Activating a tool on will deactivate any other active tools in the same
  104. // group.
  105. palette_tool_manager_->ActivateTool(action_1->GetToolId());
  106. EXPECT_TRUE(action_1->enabled());
  107. EXPECT_FALSE(action_2->enabled());
  108. palette_tool_manager_->ActivateTool(action_2->GetToolId());
  109. EXPECT_FALSE(action_1->enabled());
  110. EXPECT_TRUE(action_2->enabled());
  111. palette_tool_manager_->DeactivateTool(action_2->GetToolId());
  112. // Activating an already active tool will not do anything.
  113. palette_tool_manager_->ActivateTool(action_1->GetToolId());
  114. EXPECT_TRUE(action_1->enabled());
  115. EXPECT_FALSE(action_2->enabled());
  116. palette_tool_manager_->ActivateTool(action_1->GetToolId());
  117. EXPECT_TRUE(action_1->enabled());
  118. EXPECT_FALSE(action_2->enabled());
  119. palette_tool_manager_->DeactivateTool(action_1->GetToolId());
  120. }
  121. } // namespace ash