Chromium Code Reviews| 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(PaletteGroup group, PaletteToolId tool_id) | |
| 21 : group_(group), tool_id_(tool_id) {} | |
| 22 | |
| 23 // PaletteTool: | |
| 24 PaletteGroup group() const override { return group_; } | |
| 25 PaletteToolId tool_id() 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: | |
| 47 PaletteToolManagerTest() | |
| 48 : palette_tool_manager_(new PaletteToolManager( | |
| 49 nullptr, | |
| 50 base::Bind(&PaletteToolManagerTest::OnToolChanged, | |
| 51 base::Unretained(this)))) {} | |
| 52 ~PaletteToolManagerTest() override {} | |
| 53 | |
| 54 protected: | |
| 55 void OnToolChanged() { ++tool_changed_count_; } | |
| 56 | |
| 57 // Adds a new tool to the |tools_| vector; the caller owners the tool | |
| 58 // instance, however. | |
| 59 std::unique_ptr<PaletteTool> BuildTool(PaletteGroup group, | |
| 60 PaletteToolId tool_id) { | |
| 61 // The same PaletteToolId cannot be registered twice. | |
| 62 for (TestTool* tool : tools_) | |
| 63 DCHECK_NE(tool->tool_id(), tool_id); | |
| 64 | |
| 65 TestTool* tool = new TestTool(group, tool_id); | |
| 66 tools_.push_back(tool); | |
|
oshima
2016/07/19 00:08:53
I think it's better to just keep get the pointer f
jdufault
2016/07/19 18:06:21
I moved the DCHECK into PaletteToolManager::AddToo
oshima
2016/07/22 19:27:51
std::unique_ptr<TestTool> tool_1(new TestTool....)
jdufault
2016/07/22 19:53:36
Done. I allocated directly to the TestTool* pointe
| |
| 67 return base::WrapUnique(tool); | |
| 68 } | |
| 69 | |
| 70 int tool_changed_count_ = 0; | |
| 71 std::vector<TestTool*> tools_; | |
| 72 std::unique_ptr<PaletteToolManager> palette_tool_manager_; | |
| 73 | |
| 74 private: | |
| 75 DISALLOW_COPY_AND_ASSIGN(PaletteToolManagerTest); | |
| 76 }; | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 // Verifies that tools can be enabled/disabled and that enabling a tool disables | |
| 81 // only active tools in the same group. | |
| 82 TEST_F(PaletteToolManagerTest, MultipleToolsActivateDeactivate) { | |
| 83 // Register actions/modes. | |
| 84 palette_tool_manager_->AddTool( | |
| 85 BuildTool(PaletteGroup::ACTION, PaletteToolId::CREATE_NEW_NOTE)); | |
| 86 TestTool* action_1 = tools_.back(); | |
| 87 palette_tool_manager_->AddTool( | |
| 88 BuildTool(PaletteGroup::ACTION, PaletteToolId::SCREENSHOT)); | |
| 89 TestTool* action_2 = tools_.back(); | |
| 90 palette_tool_manager_->AddTool( | |
| 91 BuildTool(PaletteGroup::MODE, PaletteToolId::MAGNIFY)); | |
| 92 TestTool* mode_1 = tools_.back(); | |
| 93 palette_tool_manager_->AddTool( | |
| 94 BuildTool(PaletteGroup::MODE, PaletteToolId::PRESENTATION)); | |
| 95 TestTool* mode_2 = tools_.back(); | |
| 96 | |
| 97 // Enable mode 1. | |
| 98 EXPECT_EQ(0, tool_changed_count_); | |
| 99 palette_tool_manager_->ActivateTool(mode_1->tool_id()); | |
| 100 EXPECT_FALSE(action_1->enabled()); | |
| 101 EXPECT_FALSE(action_2->enabled()); | |
| 102 EXPECT_TRUE(mode_1->enabled()); | |
| 103 EXPECT_FALSE(mode_2->enabled()); | |
| 104 | |
| 105 // Turn a single action on/off. Enabling/disabling the tool does not change | |
| 106 // any other group's state. | |
| 107 palette_tool_manager_->ActivateTool(action_1->tool_id()); | |
| 108 EXPECT_TRUE(action_1->enabled()); | |
| 109 EXPECT_FALSE(action_2->enabled()); | |
| 110 EXPECT_TRUE(mode_1->enabled()); | |
| 111 EXPECT_FALSE(mode_2->enabled()); | |
| 112 palette_tool_manager_->DeactivateTool(action_1->tool_id()); | |
| 113 EXPECT_FALSE(action_1->enabled()); | |
| 114 EXPECT_FALSE(action_2->enabled()); | |
| 115 EXPECT_TRUE(mode_1->enabled()); | |
| 116 EXPECT_FALSE(mode_2->enabled()); | |
| 117 | |
| 118 // Activating a tool on will deactivate any other active tools in the same | |
| 119 // group. | |
| 120 palette_tool_manager_->ActivateTool(action_1->tool_id()); | |
| 121 EXPECT_TRUE(action_1->enabled()); | |
| 122 EXPECT_FALSE(action_2->enabled()); | |
| 123 palette_tool_manager_->ActivateTool(action_2->tool_id()); | |
| 124 EXPECT_FALSE(action_1->enabled()); | |
| 125 EXPECT_TRUE(action_2->enabled()); | |
| 126 palette_tool_manager_->DeactivateTool(action_2->tool_id()); | |
| 127 | |
| 128 // Activating an already active tool will not do anything. | |
| 129 palette_tool_manager_->ActivateTool(action_1->tool_id()); | |
| 130 EXPECT_TRUE(action_1->enabled()); | |
| 131 EXPECT_FALSE(action_2->enabled()); | |
| 132 palette_tool_manager_->ActivateTool(action_1->tool_id()); | |
| 133 EXPECT_TRUE(action_1->enabled()); | |
| 134 EXPECT_FALSE(action_2->enabled()); | |
| 135 palette_tool_manager_->DeactivateTool(action_1->tool_id()); | |
| 136 } | |
| OLD | NEW |