Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1707)

Unified Diff: ash/common/system/chromeos/palette/palette_tool_manager_unittest.cc

Issue 2140343002: Add palette tool/manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@stylus-add-icons
Patch Set: Address comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..98259151a730c1d70a325db04071736200bf2f5e
--- /dev/null
+++ b/ash/common/system/chromeos/palette/palette_tool_manager_unittest.cc
@@ -0,0 +1,134 @@
+// 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 overrides.
+ 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 overrides.
+ views::View* CreateView() override {
+ NOTREACHED();
+ return nullptr;
+ }
+ void DestroyView() override { FAIL(); }
+
+ PaletteGroup group_;
+ PaletteToolId tool_id_;
+};
oshima 2016/07/14 13:58:32 DISALLOW_COPY_AND_ASSIGN
jdufault 2016/07/18 20:39:58 Done.
+
+// 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(tool->tool_id() != tool_id);
oshima 2016/07/14 13:58:32 DCHECK_NE
jdufault 2016/07/18 20:39:58 Done.
+
+ TestTool* tool = new TestTool(group, tool_id);
+ tools_.push_back(tool);
+ 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_[tools_.size() - 1];
oshima 2016/07/14 13:58:32 tools_.back() ?
jdufault 2016/07/18 20:39:58 Ah, much better! Thanks
+ palette_tool_manager_->AddTool(
+ BuildTool(PaletteGroup::ACTION, PaletteToolId::SCREENSHOT));
+ TestTool* action_2 = tools_[tools_.size() - 1];
+ palette_tool_manager_->AddTool(
+ BuildTool(PaletteGroup::MODE, PaletteToolId::MAGNIFY));
+ TestTool* mode_1 = tools_[tools_.size() - 1];
+ palette_tool_manager_->AddTool(
+ BuildTool(PaletteGroup::MODE, PaletteToolId::PRESENTATION));
+ TestTool* mode_2 = tools_[tools_.size() - 1];
+
+ // 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());
+}

Powered by Google App Engine
This is Rietveld 408576698