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 overrides. | |
| 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 overrides. | |
| 32 views::View* CreateView() override { | |
| 33 NOTREACHED(); | |
| 34 return nullptr; | |
| 35 } | |
| 36 void DestroyView() override { FAIL(); } | |
| 37 | |
| 38 PaletteGroup group_; | |
| 39 PaletteToolId tool_id_; | |
| 40 }; | |
|
oshima
2016/07/14 13:58:32
DISALLOW_COPY_AND_ASSIGN
jdufault
2016/07/18 20:39:58
Done.
| |
| 41 | |
| 42 // Base class for tool manager unittests. | |
| 43 class PaletteToolManagerTest : public ::testing::Test { | |
| 44 public: | |
| 45 PaletteToolManagerTest() | |
| 46 : palette_tool_manager_(new PaletteToolManager( | |
| 47 nullptr, | |
| 48 base::Bind(&PaletteToolManagerTest::OnToolChanged, | |
| 49 base::Unretained(this)))) {} | |
| 50 ~PaletteToolManagerTest() override {} | |
| 51 | |
| 52 protected: | |
| 53 void OnToolChanged() { ++tool_changed_count_; } | |
| 54 | |
| 55 // Adds a new tool to the |tools_| vector; the caller owners the tool | |
| 56 // instance, however. | |
| 57 std::unique_ptr<PaletteTool> BuildTool(PaletteGroup group, | |
| 58 PaletteToolId tool_id) { | |
| 59 // The same PaletteToolId cannot be registered twice. | |
| 60 for (TestTool* tool : tools_) | |
| 61 DCHECK(tool->tool_id() != tool_id); | |
|
oshima
2016/07/14 13:58:32
DCHECK_NE
jdufault
2016/07/18 20:39:58
Done.
| |
| 62 | |
| 63 TestTool* tool = new TestTool(group, tool_id); | |
| 64 tools_.push_back(tool); | |
| 65 return base::WrapUnique(tool); | |
| 66 } | |
| 67 | |
| 68 int tool_changed_count_ = 0; | |
| 69 std::vector<TestTool*> tools_; | |
| 70 std::unique_ptr<PaletteToolManager> palette_tool_manager_; | |
| 71 | |
| 72 private: | |
| 73 DISALLOW_COPY_AND_ASSIGN(PaletteToolManagerTest); | |
| 74 }; | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 // Verifies that tools can be enabled/disabled and that enabling a tool disables | |
| 79 // only active tools in the same group. | |
| 80 TEST_F(PaletteToolManagerTest, MultipleToolsActivateDeactivate) { | |
| 81 // Register actions/modes. | |
| 82 palette_tool_manager_->AddTool( | |
| 83 BuildTool(PaletteGroup::ACTION, PaletteToolId::CREATE_NEW_NOTE)); | |
| 84 TestTool* action_1 = tools_[tools_.size() - 1]; | |
|
oshima
2016/07/14 13:58:32
tools_.back() ?
jdufault
2016/07/18 20:39:58
Ah, much better! Thanks
| |
| 85 palette_tool_manager_->AddTool( | |
| 86 BuildTool(PaletteGroup::ACTION, PaletteToolId::SCREENSHOT)); | |
| 87 TestTool* action_2 = tools_[tools_.size() - 1]; | |
| 88 palette_tool_manager_->AddTool( | |
| 89 BuildTool(PaletteGroup::MODE, PaletteToolId::MAGNIFY)); | |
| 90 TestTool* mode_1 = tools_[tools_.size() - 1]; | |
| 91 palette_tool_manager_->AddTool( | |
| 92 BuildTool(PaletteGroup::MODE, PaletteToolId::PRESENTATION)); | |
| 93 TestTool* mode_2 = tools_[tools_.size() - 1]; | |
| 94 | |
| 95 // Enable mode 1. | |
| 96 EXPECT_EQ(0, tool_changed_count_); | |
| 97 palette_tool_manager_->ActivateTool(mode_1->tool_id()); | |
| 98 EXPECT_FALSE(action_1->enabled()); | |
| 99 EXPECT_FALSE(action_2->enabled()); | |
| 100 EXPECT_TRUE(mode_1->enabled()); | |
| 101 EXPECT_FALSE(mode_2->enabled()); | |
| 102 | |
| 103 // Turn a single action on/off. Enabling/disabling the tool does not change | |
| 104 // any other group's state. | |
| 105 palette_tool_manager_->ActivateTool(action_1->tool_id()); | |
| 106 EXPECT_TRUE(action_1->enabled()); | |
| 107 EXPECT_FALSE(action_2->enabled()); | |
| 108 EXPECT_TRUE(mode_1->enabled()); | |
| 109 EXPECT_FALSE(mode_2->enabled()); | |
| 110 palette_tool_manager_->DeactivateTool(action_1->tool_id()); | |
| 111 EXPECT_FALSE(action_1->enabled()); | |
| 112 EXPECT_FALSE(action_2->enabled()); | |
| 113 EXPECT_TRUE(mode_1->enabled()); | |
| 114 EXPECT_FALSE(mode_2->enabled()); | |
| 115 | |
| 116 // Activating a tool on will deactivate any other active tools in the same | |
| 117 // group. | |
| 118 palette_tool_manager_->ActivateTool(action_1->tool_id()); | |
| 119 EXPECT_TRUE(action_1->enabled()); | |
| 120 EXPECT_FALSE(action_2->enabled()); | |
| 121 palette_tool_manager_->ActivateTool(action_2->tool_id()); | |
| 122 EXPECT_FALSE(action_1->enabled()); | |
| 123 EXPECT_TRUE(action_2->enabled()); | |
| 124 palette_tool_manager_->DeactivateTool(action_2->tool_id()); | |
| 125 | |
| 126 // Activating an already active tool will not do anything. | |
| 127 palette_tool_manager_->ActivateTool(action_1->tool_id()); | |
| 128 EXPECT_TRUE(action_1->enabled()); | |
| 129 EXPECT_FALSE(action_2->enabled()); | |
| 130 palette_tool_manager_->ActivateTool(action_1->tool_id()); | |
| 131 EXPECT_TRUE(action_1->enabled()); | |
| 132 EXPECT_FALSE(action_2->enabled()); | |
| 133 palette_tool_manager_->DeactivateTool(action_1->tool_id()); | |
| 134 } | |
| OLD | NEW |