| Index: ash/tray_action/tray_action_unittest.cc
|
| diff --git a/ash/tray_action/tray_action_unittest.cc b/ash/tray_action/tray_action_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..f9163bbf04563260254ce8197e07369ce65463c8
|
| --- /dev/null
|
| +++ b/ash/tray_action/tray_action_unittest.cc
|
| @@ -0,0 +1,237 @@
|
| +// Copyright 2017 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/tray_action/tray_action.h"
|
| +
|
| +#include <memory>
|
| +#include <vector>
|
| +
|
| +#include "ash/shell.h"
|
| +#include "ash/test/ash_test_base.h"
|
| +#include "ash/tray_action/tray_action_observer.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/ptr_util.h"
|
| +
|
| +using ash::mojom::TrayActionState;
|
| +
|
| +namespace ash {
|
| +
|
| +namespace {
|
| +
|
| +class ScopedTestStateObserver : public TrayActionObserver {
|
| + public:
|
| + explicit ScopedTestStateObserver(TrayAction* tray_action)
|
| + : tray_action_(tray_action) {
|
| + tray_action_->AddObserver(this);
|
| + }
|
| +
|
| + ~ScopedTestStateObserver() override { tray_action_->RemoveObserver(this); }
|
| +
|
| + void OnLockScreenNoteStateChanged(TrayActionState state) override {
|
| + observed_states_.push_back(state);
|
| + }
|
| +
|
| + const std::vector<TrayActionState>& observed_states() const {
|
| + return observed_states_;
|
| + }
|
| +
|
| + void ClearObservedStates() { observed_states_.clear(); }
|
| +
|
| + private:
|
| + TrayAction* tray_action_;
|
| +
|
| + std::vector<TrayActionState> observed_states_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ScopedTestStateObserver);
|
| +};
|
| +
|
| +class TestTrayActionClient : public mojom::TrayActionClient {
|
| + public:
|
| + TestTrayActionClient() : binding_(this) {}
|
| +
|
| + ~TestTrayActionClient() override = default;
|
| +
|
| + mojom::TrayActionClientPtr CreateInterfacePtrAndBind() {
|
| + return binding_.CreateInterfacePtrAndBind();
|
| + }
|
| +
|
| + void RequestNewLockScreenNote() override { action_requests_count_++; }
|
| +
|
| + int action_requests_count() const { return action_requests_count_; }
|
| +
|
| + void reset_action_requests_count() { action_requests_count_ = 0; }
|
| +
|
| + private:
|
| + mojo::Binding<ash::mojom::TrayActionClient> binding_;
|
| +
|
| + int action_requests_count_ = 0;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TestTrayActionClient);
|
| +};
|
| +
|
| +using TrayActionTest = test::AshTestBase;
|
| +
|
| +} // namespace
|
| +
|
| +TEST_F(TrayActionTest, NoTrayAction) {
|
| + TrayAction* tray_action = Shell::Get()->tray_action();
|
| + ScopedTestStateObserver observer(tray_action);
|
| +
|
| + EXPECT_EQ(TrayActionState::kNotSupported,
|
| + tray_action->GetLockScreenNoteState());
|
| +
|
| + tray_action->UpdateLockScreenNoteState(TrayActionState::kAvailable);
|
| +
|
| + // The effective state should be |kNotSupported| as long as an action handler
|
| + // is not set.
|
| + EXPECT_EQ(TrayActionState::kNotSupported,
|
| + tray_action->GetLockScreenNoteState());
|
| + EXPECT_EQ(0u, observer.observed_states().size());
|
| +
|
| + // Adding action handler should update the tray_action state.
|
| + std::unique_ptr<TestTrayActionClient> action_client =
|
| + base::MakeUnique<TestTrayActionClient>();
|
| + tray_action->SetClient(action_client->CreateInterfacePtrAndBind());
|
| +
|
| + EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
|
| + // Effective state changed, so observer should be notified of it.
|
| + ASSERT_EQ(1u, observer.observed_states().size());
|
| + EXPECT_EQ(TrayActionState::kAvailable, observer.observed_states()[0]);
|
| + observer.ClearObservedStates();
|
| +
|
| + // When action handler is reset, effective state should go back to
|
| + // |kNotSupported|.
|
| + tray_action->SetClient(nullptr);
|
| + action_client.reset();
|
| +
|
| + EXPECT_EQ(TrayActionState::kNotSupported,
|
| + tray_action->GetLockScreenNoteState());
|
| + // Effective state changed, so observer should be notified of it.
|
| + ASSERT_EQ(1u, observer.observed_states().size());
|
| + EXPECT_EQ(TrayActionState::kNotSupported, observer.observed_states()[0]);
|
| + observer.ClearObservedStates();
|
| +}
|
| +
|
| +TEST_F(TrayActionTest, StateChangesWithHandlerSet) {
|
| + TrayAction* tray_action = Shell::Get()->tray_action();
|
| +
|
| + ScopedTestStateObserver observer(tray_action);
|
| + TestTrayActionClient action_handler;
|
| + tray_action->SetClient(action_handler.CreateInterfacePtrAndBind());
|
| +
|
| + tray_action->UpdateLockScreenNoteState(
|
| +
|
| + TrayActionState::kAvailable);
|
| + EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
|
| + ASSERT_EQ(1u, observer.observed_states().size());
|
| + EXPECT_EQ(TrayActionState::kAvailable, observer.observed_states()[0]);
|
| + observer.ClearObservedStates();
|
| +
|
| + tray_action->UpdateLockScreenNoteState(
|
| +
|
| + TrayActionState::kAvailable);
|
| + EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
|
| + // No real state change, so the observer should not be notified.
|
| + ASSERT_EQ(0u, observer.observed_states().size());
|
| +
|
| + tray_action->UpdateLockScreenNoteState(
|
| +
|
| + TrayActionState::kActive);
|
| + EXPECT_EQ(TrayActionState::kActive, tray_action->GetLockScreenNoteState());
|
| + ASSERT_EQ(1u, observer.observed_states().size());
|
| + EXPECT_EQ(TrayActionState::kActive, observer.observed_states()[0]);
|
| + observer.ClearObservedStates();
|
| +
|
| + tray_action->SetClient(nullptr);
|
| +}
|
| +
|
| +TEST_F(TrayActionTest, NormalStateProgression) {
|
| + TrayAction* tray_action = Shell::Get()->tray_action();
|
| +
|
| + ScopedTestStateObserver observer(tray_action);
|
| + TestTrayActionClient action_handler;
|
| + tray_action->SetClient(action_handler.CreateInterfacePtrAndBind());
|
| +
|
| + tray_action->UpdateLockScreenNoteState(
|
| +
|
| + TrayActionState::kAvailable);
|
| + EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
|
| + ASSERT_EQ(1u, observer.observed_states().size());
|
| + EXPECT_EQ(TrayActionState::kAvailable, observer.observed_states()[0]);
|
| + observer.ClearObservedStates();
|
| +
|
| + tray_action->UpdateLockScreenNoteState(
|
| +
|
| + TrayActionState::kLaunching);
|
| + EXPECT_EQ(TrayActionState::kLaunching, tray_action->GetLockScreenNoteState());
|
| + ASSERT_EQ(1u, observer.observed_states().size());
|
| + EXPECT_EQ(TrayActionState::kLaunching, observer.observed_states()[0]);
|
| + observer.ClearObservedStates();
|
| +
|
| + tray_action->UpdateLockScreenNoteState(
|
| +
|
| + TrayActionState::kBackground);
|
| + EXPECT_EQ(TrayActionState::kBackground,
|
| + tray_action->GetLockScreenNoteState());
|
| + ASSERT_EQ(1u, observer.observed_states().size());
|
| + EXPECT_EQ(TrayActionState::kBackground, observer.observed_states()[0]);
|
| + observer.ClearObservedStates();
|
| +
|
| + tray_action->UpdateLockScreenNoteState(
|
| +
|
| + TrayActionState::kNotSupported);
|
| + EXPECT_EQ(TrayActionState::kNotSupported,
|
| + tray_action->GetLockScreenNoteState());
|
| + ASSERT_EQ(1u, observer.observed_states().size());
|
| + EXPECT_EQ(TrayActionState::kNotSupported, observer.observed_states()[0]);
|
| + observer.ClearObservedStates();
|
| +
|
| + tray_action->SetClient(nullptr);
|
| +}
|
| +
|
| +TEST_F(TrayActionTest, ObserversNotNotifiedOnDuplicateState) {
|
| + TrayAction* tray_action = Shell::Get()->tray_action();
|
| +
|
| + ScopedTestStateObserver observer(tray_action);
|
| + TestTrayActionClient action_handler;
|
| + tray_action->SetClient(action_handler.CreateInterfacePtrAndBind());
|
| +
|
| + tray_action->UpdateLockScreenNoteState(
|
| +
|
| + TrayActionState::kAvailable);
|
| + EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
|
| + ASSERT_EQ(1u, observer.observed_states().size());
|
| + EXPECT_EQ(TrayActionState::kAvailable, observer.observed_states()[0]);
|
| + observer.ClearObservedStates();
|
| +
|
| + tray_action->UpdateLockScreenNoteState(
|
| +
|
| + TrayActionState::kAvailable);
|
| + EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
|
| + ASSERT_EQ(0u, observer.observed_states().size());
|
| +
|
| + tray_action->SetClient(nullptr);
|
| +}
|
| +
|
| +TEST_F(TrayActionTest, RequestAction) {
|
| + TrayAction* tray_action = Shell::Get()->tray_action();
|
| +
|
| + TestTrayActionClient action_client;
|
| + tray_action->SetClient(action_client.CreateInterfacePtrAndBind());
|
| +
|
| + EXPECT_EQ(0, action_client.action_requests_count());
|
| + tray_action->RequestNewLockScreenNote();
|
| + RunAllPendingInMessageLoop();
|
| + EXPECT_EQ(1, action_client.action_requests_count());
|
| +
|
| + tray_action->SetClient(nullptr);
|
| +}
|
| +
|
| +// Tests that there is no crash if handler is not set.
|
| +TEST_F(TrayActionTest, RequestActionWithNoHandler) {
|
| + TrayAction* tray_action = Shell::Get()->tray_action();
|
| + tray_action->RequestNewLockScreenNote();
|
| +}
|
| +
|
| +} // namespace ash
|
|
|