Chromium Code Reviews| Index: ash/common/system/chromeos/palette/palette_tool_manager_unittest.cc |
| diff --git a/ash/common/system/chromeos/palette/palette_tool_manager_unittest.cc b/ash/common/system/chromeos/palette/palette_tool_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0c81b85bfae605572cd59b688f1f0d1a7dcd29b8 |
| --- /dev/null |
| +++ b/ash/common/system/chromeos/palette/palette_tool_manager_unittest.cc |
| @@ -0,0 +1,136 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/common/system/chromeos/palette/palette_tool.h" |
| +#include "ash/common/system/chromeos/palette/palette_tool_manager.h" |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using namespace ash; |
| + |
| +namespace { |
| + |
| +// A simple tool instance that exposes some additional data for testing. |
| +class TestTool : public PaletteTool { |
| + public: |
| + TestTool(PaletteGroup group, PaletteToolId tool_id) |
| + : group_(group), tool_id_(tool_id) {} |
| + |
| + // PaletteTool: |
| + PaletteGroup group() const override { return group_; } |
| + PaletteToolId tool_id() const override { return tool_id_; } |
| + |
| + // Shadows the parent declaration since PaletteTool::enabled is not virtual. |
| + bool enabled() const { return PaletteTool::enabled(); } |
| + |
| + private: |
| + // PaletteTool: |
| + views::View* CreateView() override { |
| + NOTREACHED(); |
| + return nullptr; |
| + } |
| + void OnViewDestroyed() override { FAIL(); } |
| + |
| + PaletteGroup group_; |
| + PaletteToolId tool_id_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestTool); |
| +}; |
| + |
| +// Base class for tool manager unittests. |
| +class PaletteToolManagerTest : public ::testing::Test { |
| + public: |
| + PaletteToolManagerTest() |
| + : palette_tool_manager_(new PaletteToolManager( |
| + nullptr, |
| + base::Bind(&PaletteToolManagerTest::OnToolChanged, |
| + base::Unretained(this)))) {} |
| + ~PaletteToolManagerTest() override {} |
| + |
| + protected: |
| + void OnToolChanged() { ++tool_changed_count_; } |
| + |
| + // Adds a new tool to the |tools_| vector; the caller owners the tool |
| + // instance, however. |
| + std::unique_ptr<PaletteTool> BuildTool(PaletteGroup group, |
| + PaletteToolId tool_id) { |
| + // The same PaletteToolId cannot be registered twice. |
| + for (TestTool* tool : tools_) |
| + DCHECK_NE(tool->tool_id(), tool_id); |
| + |
| + TestTool* tool = new TestTool(group, tool_id); |
| + 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
|
| + return base::WrapUnique(tool); |
| + } |
| + |
| + int tool_changed_count_ = 0; |
| + std::vector<TestTool*> tools_; |
| + std::unique_ptr<PaletteToolManager> palette_tool_manager_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(PaletteToolManagerTest); |
| +}; |
| + |
| +} // namespace |
| + |
| +// Verifies that tools can be enabled/disabled and that enabling a tool disables |
| +// only active tools in the same group. |
| +TEST_F(PaletteToolManagerTest, MultipleToolsActivateDeactivate) { |
| + // Register actions/modes. |
| + palette_tool_manager_->AddTool( |
| + BuildTool(PaletteGroup::ACTION, PaletteToolId::CREATE_NEW_NOTE)); |
| + TestTool* action_1 = tools_.back(); |
| + palette_tool_manager_->AddTool( |
| + BuildTool(PaletteGroup::ACTION, PaletteToolId::SCREENSHOT)); |
| + TestTool* action_2 = tools_.back(); |
| + palette_tool_manager_->AddTool( |
| + BuildTool(PaletteGroup::MODE, PaletteToolId::MAGNIFY)); |
| + TestTool* mode_1 = tools_.back(); |
| + palette_tool_manager_->AddTool( |
| + BuildTool(PaletteGroup::MODE, PaletteToolId::PRESENTATION)); |
| + TestTool* mode_2 = tools_.back(); |
| + |
| + // Enable mode 1. |
| + EXPECT_EQ(0, tool_changed_count_); |
| + palette_tool_manager_->ActivateTool(mode_1->tool_id()); |
| + EXPECT_FALSE(action_1->enabled()); |
| + EXPECT_FALSE(action_2->enabled()); |
| + EXPECT_TRUE(mode_1->enabled()); |
| + EXPECT_FALSE(mode_2->enabled()); |
| + |
| + // Turn a single action on/off. Enabling/disabling the tool does not change |
| + // any other group's state. |
| + palette_tool_manager_->ActivateTool(action_1->tool_id()); |
| + EXPECT_TRUE(action_1->enabled()); |
| + EXPECT_FALSE(action_2->enabled()); |
| + EXPECT_TRUE(mode_1->enabled()); |
| + EXPECT_FALSE(mode_2->enabled()); |
| + palette_tool_manager_->DeactivateTool(action_1->tool_id()); |
| + EXPECT_FALSE(action_1->enabled()); |
| + EXPECT_FALSE(action_2->enabled()); |
| + EXPECT_TRUE(mode_1->enabled()); |
| + EXPECT_FALSE(mode_2->enabled()); |
| + |
| + // Activating a tool on will deactivate any other active tools in the same |
| + // group. |
| + palette_tool_manager_->ActivateTool(action_1->tool_id()); |
| + EXPECT_TRUE(action_1->enabled()); |
| + EXPECT_FALSE(action_2->enabled()); |
| + palette_tool_manager_->ActivateTool(action_2->tool_id()); |
| + EXPECT_FALSE(action_1->enabled()); |
| + EXPECT_TRUE(action_2->enabled()); |
| + palette_tool_manager_->DeactivateTool(action_2->tool_id()); |
| + |
| + // Activating an already active tool will not do anything. |
| + palette_tool_manager_->ActivateTool(action_1->tool_id()); |
| + EXPECT_TRUE(action_1->enabled()); |
| + EXPECT_FALSE(action_2->enabled()); |
| + palette_tool_manager_->ActivateTool(action_1->tool_id()); |
| + EXPECT_TRUE(action_1->enabled()); |
| + EXPECT_FALSE(action_2->enabled()); |
| + palette_tool_manager_->DeactivateTool(action_1->tool_id()); |
| +} |