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