1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "ash/public/cpp/note_taking_client.h"
#include "ash/system/palette/mock_palette_tool_delegate.h"
#include "ash/system/palette/palette_ids.h"
#include "ash/system/palette/palette_tool.h"
#include "ash/system/palette/tools/create_note_action.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/ptr_util.h"
#include "ui/views/view.h"
namespace ash {
class TestNoteTakingControllerClient : public NoteTakingClient {
public:
TestNoteTakingControllerClient() = default;
TestNoteTakingControllerClient(const TestNoteTakingControllerClient&) =
delete;
TestNoteTakingControllerClient& operator=(
const TestNoteTakingControllerClient&) = delete;
~TestNoteTakingControllerClient() override = default;
int GetCreateNoteCount() {
return create_note_count_;
}
void set_can_create(bool can_create) { can_create_ = can_create; }
// NoteTakingClient:
bool CanCreateNote() override { return can_create_; }
void CreateNote() override { create_note_count_++; }
private:
bool can_create_ = true;
int create_note_count_ = 0;
};
namespace {
// Base class for all create note ash tests.
class CreateNoteTest : public AshTestBase {
public:
CreateNoteTest() = default;
CreateNoteTest(const CreateNoteTest&) = delete;
CreateNoteTest& operator=(const CreateNoteTest&) = delete;
~CreateNoteTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
palette_tool_delegate_ = std::make_unique<MockPaletteToolDelegate>();
tool_ = std::make_unique<CreateNoteAction>(palette_tool_delegate_.get());
}
void TearDown() override {
// This needs to be called first to remove the event handler before the
// shell instance gets torn down.
tool_.reset();
AshTestBase::TearDown();
}
protected:
std::unique_ptr<MockPaletteToolDelegate> palette_tool_delegate_;
std::unique_ptr<PaletteTool> tool_;
};
} // namespace
// The note tool is only visible when there is a note-taking app available.
TEST_F(CreateNoteTest, ViewOnlyCreatedWhenNoteAppIsAvailable) {
EXPECT_FALSE(tool_->CreateView());
tool_->OnViewDestroyed();
auto note_taking_client = std::make_unique<TestNoteTakingControllerClient>();
std::unique_ptr<views::View> view = base::WrapUnique(tool_->CreateView());
EXPECT_TRUE(view);
tool_->OnViewDestroyed();
note_taking_client->set_can_create(false);
EXPECT_FALSE(tool_->CreateView());
tool_->OnViewDestroyed();
note_taking_client.reset();
EXPECT_FALSE(tool_->CreateView());
tool_->OnViewDestroyed();
}
// Activating the note tool both creates a note on the client and also
// disables the tool and hides the palette.
TEST_F(CreateNoteTest, EnablingToolCreatesNewNoteAndDisablesTool) {
auto note_taking_client = std::make_unique<TestNoteTakingControllerClient>();
std::unique_ptr<views::View> view = base::WrapUnique(tool_->CreateView());
EXPECT_CALL(*palette_tool_delegate_.get(),
DisableTool(PaletteToolId::CREATE_NOTE));
EXPECT_CALL(*palette_tool_delegate_.get(), HidePalette());
tool_->OnEnable();
EXPECT_EQ(1, note_taking_client->GetCreateNoteCount());
}
TEST_F(CreateNoteTest, ClientGetsDisabledAfterViewCreated) {
auto note_taking_client = std::make_unique<TestNoteTakingControllerClient>();
std::unique_ptr<views::View> view = base::WrapUnique(tool_->CreateView());
EXPECT_CALL(*palette_tool_delegate_.get(),
DisableTool(PaletteToolId::CREATE_NOTE));
EXPECT_CALL(*palette_tool_delegate_.get(), HidePalette());
note_taking_client->set_can_create(false);
tool_->OnEnable();
EXPECT_EQ(0, note_taking_client->GetCreateNoteCount());
}
TEST_F(CreateNoteTest, ClientGetsRemovedAfterViewCreated) {
auto note_taking_client = std::make_unique<TestNoteTakingControllerClient>();
std::unique_ptr<views::View> view = base::WrapUnique(tool_->CreateView());
EXPECT_CALL(*palette_tool_delegate_.get(),
DisableTool(PaletteToolId::CREATE_NOTE));
EXPECT_CALL(*palette_tool_delegate_.get(), HidePalette());
note_taking_client.reset();
tool_->OnEnable();
}
TEST_F(CreateNoteTest, ToolIsEnabledWhenStylusButtonIsPressed) {
auto note_taking_client = std::make_unique<TestNoteTakingControllerClient>();
std::unique_ptr<views::View> view = base::WrapUnique(tool_->CreateView());
// Send a stylus button event.
PressAndReleaseKey(ui::VKEY_F19, ui::EF_IS_STYLUS_BUTTON);
EXPECT_EQ(1, note_taking_client->GetCreateNoteCount());
}
// The note tool is only visible on the internal display
TEST_F(CreateNoteTest, ViewOnlyAppearsOnInternalDisplay) {
auto note_taking_client = std::make_unique<TestNoteTakingControllerClient>();
std::unique_ptr<views::View> view = base::WrapUnique(tool_->CreateView());
EXPECT_TRUE(view);
tool_->OnViewDestroyed();
tool_->SetExternalDisplayForTest();
EXPECT_FALSE(tool_->CreateView());
tool_->OnViewDestroyed();
}
} // namespace ash
|