-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UserNotes] Start FrameUserNoteChange::Apply implementation
Implements the first part of the FrameUserNoteChange::Apply method, up until the point where async requests to the renderer must be made. The async infra is set up but calls to Mojo will be added in the follow-up CL. Detailed changes: - Added a ref to UserNoteService inside FrameUserNoteChanges. The ref is needed to get refs to the note models (and eventually to get temporary instances from the service for the note authoring flow). Updated the CalculateNoteChanges method and unit tests to account for the new parameter in the constructor. - Apply() now takes a callback as param (the initial plan was to have Apply() call a method on the service as a callback), which will make it easier for callers to deal with the 2 possible flows for Apply(): storage updates and navigations. It also greatly simplifies tests. - Added a getter for a note model in the service, needed to construct NoteInstance objects. - Added unit tests for Apply() (test cases for the note authoring workflow will follow when note authoring is implemented). - Made other small fixes such as UserNoteManager methods now correctly taking params by ref instead of value and using a nullptr as the delegate in the tab helper tests. Bug: 1321920 Change-Id: I6ae2703ad3f202eb2060bc06799092cc55e586b3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3622099 Reviewed-by: Caroline Rising <corising@chromium.org> Reviewed-by: David Bokan <bokan@chromium.org> Commit-Queue: Guillaume Jenkins <gujen@google.com> Cr-Commit-Position: refs/heads/main@{#1001112}
- Loading branch information
1 parent
b3f2f1a
commit f5f04a6
Showing
21 changed files
with
360 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
components/user_notes/browser/frame_user_note_changes_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// Copyright 2022 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 "components/user_notes/browser/frame_user_note_changes.h" | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include "base/memory/safe_ref.h" | ||
#include "base/unguessable_token.h" | ||
#include "components/user_notes/browser/user_note_base_test.h" | ||
#include "components/user_notes/browser/user_note_instance.h" | ||
#include "components/user_notes/model/user_note.h" | ||
#include "content/public/browser/render_frame_host.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
using testing::_; | ||
using testing::Mock; | ||
|
||
namespace user_notes { | ||
|
||
namespace { | ||
|
||
// A mock for a note instance that synchronously invokes the callback when | ||
// initializing the text highlight. | ||
class MockUserNoteInstance : public UserNoteInstance { | ||
public: | ||
explicit MockUserNoteInstance(base::SafeRef<UserNote> model_ref) | ||
: UserNoteInstance(model_ref) {} | ||
|
||
MOCK_METHOD(void, | ||
InitializeHighlightIfNeeded, | ||
(base::OnceClosure callback), | ||
(override)); | ||
}; | ||
|
||
// Partially mock the object under test so calls to `MakeNoteInstance` can be | ||
// intercepted, allowing the tests to create mocked instances. | ||
class MockFrameUserNoteChanges : public FrameUserNoteChanges { | ||
public: | ||
MockFrameUserNoteChanges( | ||
base::SafeRef<UserNoteService> service, | ||
content::RenderFrameHost* rfh, | ||
const FrameUserNoteChanges::ChangeList& notes_added, | ||
const FrameUserNoteChanges::ChangeList& notes_modified, | ||
const FrameUserNoteChanges::ChangeList& notes_removed) | ||
: FrameUserNoteChanges(service, | ||
rfh, | ||
notes_added, | ||
notes_modified, | ||
notes_removed) {} | ||
|
||
MOCK_METHOD(std::unique_ptr<UserNoteInstance>, | ||
MakeNoteInstance, | ||
(const UserNote* note_model), | ||
(const override)); | ||
}; | ||
|
||
void MockInitializeHighlightIfNeeded(base::OnceClosure callback) { | ||
std::move(callback).Run(); | ||
} | ||
|
||
std::unique_ptr<UserNoteInstance> MockMakeNoteInstance( | ||
const UserNote* note_model) { | ||
auto instance_mock = | ||
std::make_unique<MockUserNoteInstance>(note_model->GetSafeRef()); | ||
|
||
EXPECT_CALL(*instance_mock, InitializeHighlightIfNeeded(_)) | ||
.Times(1) | ||
.WillOnce(&MockInitializeHighlightIfNeeded); | ||
|
||
return instance_mock; | ||
} | ||
|
||
} // namespace | ||
|
||
class FrameUserNoteChangesTest : public UserNoteBaseTest {}; | ||
|
||
// Tests that added notes correctly kick off highlight initialization on the | ||
// renderer side, and new instances are correctly added to the note manager. | ||
TEST_F(FrameUserNoteChangesTest, ApplyAddedNotes) { | ||
AddNewNotesToService(3); | ||
UserNoteManager* m = ConfigureNewManager(); | ||
AddNewInstanceToManager(m, note_ids_[0]); | ||
|
||
std::vector<base::UnguessableToken> added({note_ids_[1], note_ids_[2]}); | ||
std::vector<base::UnguessableToken> modified; | ||
std::vector<base::UnguessableToken> removed; | ||
|
||
auto mock_changes = std::make_unique<MockFrameUserNoteChanges>( | ||
note_service_->GetSafeRef(), web_contents_list_[0]->GetMainFrame(), added, | ||
modified, removed); | ||
|
||
EXPECT_CALL(*mock_changes, MakeNoteInstance(_)) | ||
.Times(2) | ||
.WillRepeatedly(&MockMakeNoteInstance); | ||
|
||
mock_changes->Apply(base::BindOnce([] {})); | ||
|
||
// The mocks ensure the callback is invoked synchronously, so verifications | ||
// can happen immediately. | ||
EXPECT_EQ(InstanceMapSize(m), 3u); | ||
EXPECT_TRUE(m->GetNoteInstance(note_ids_[0])); | ||
EXPECT_TRUE(m->GetNoteInstance(note_ids_[1])); | ||
EXPECT_TRUE(m->GetNoteInstance(note_ids_[2])); | ||
} | ||
|
||
// Tests that modified notes don't impact instances in the note manager. | ||
TEST_F(FrameUserNoteChangesTest, ApplyModifiedNotes) { | ||
AddNewNotesToService(3); | ||
UserNoteManager* m = ConfigureNewManager(); | ||
AddNewInstanceToManager(m, note_ids_[0]); | ||
AddNewInstanceToManager(m, note_ids_[1]); | ||
AddNewInstanceToManager(m, note_ids_[2]); | ||
|
||
std::vector<base::UnguessableToken> added; | ||
std::vector<base::UnguessableToken> modified({note_ids_[0], note_ids_[2]}); | ||
std::vector<base::UnguessableToken> removed; | ||
|
||
auto mock_changes = std::make_unique<MockFrameUserNoteChanges>( | ||
note_service_->GetSafeRef(), web_contents_list_[0]->GetMainFrame(), added, | ||
modified, removed); | ||
|
||
EXPECT_CALL(*mock_changes, MakeNoteInstance(_)).Times(0); | ||
|
||
mock_changes->Apply(base::BindOnce([] {})); | ||
|
||
// The mocks ensure the callback is invoked synchronously, so verifications | ||
// can happen immediately. | ||
EXPECT_EQ(InstanceMapSize(m), 3u); | ||
EXPECT_TRUE(m->GetNoteInstance(note_ids_[0])); | ||
EXPECT_TRUE(m->GetNoteInstance(note_ids_[1])); | ||
EXPECT_TRUE(m->GetNoteInstance(note_ids_[2])); | ||
} | ||
|
||
// Tests that removed notes correctly have their instances removed from the | ||
// note manager. | ||
TEST_F(FrameUserNoteChangesTest, ApplyRemovedNotes) { | ||
AddNewNotesToService(3); | ||
UserNoteManager* m = ConfigureNewManager(); | ||
AddNewInstanceToManager(m, note_ids_[0]); | ||
AddNewInstanceToManager(m, note_ids_[1]); | ||
AddNewInstanceToManager(m, note_ids_[2]); | ||
|
||
std::vector<base::UnguessableToken> added; | ||
std::vector<base::UnguessableToken> modified; | ||
std::vector<base::UnguessableToken> removed({note_ids_[0], note_ids_[2]}); | ||
|
||
auto mock_changes = std::make_unique<MockFrameUserNoteChanges>( | ||
note_service_->GetSafeRef(), web_contents_list_[0]->GetMainFrame(), added, | ||
modified, removed); | ||
|
||
EXPECT_CALL(*mock_changes, MakeNoteInstance(_)).Times(0); | ||
|
||
mock_changes->Apply(base::BindOnce([] {})); | ||
|
||
// The mocks ensure the callback is invoked synchronously, so verifications | ||
// can happen immediately. | ||
EXPECT_EQ(InstanceMapSize(m), 1u); | ||
EXPECT_TRUE(m->GetNoteInstance(note_ids_[1])); | ||
EXPECT_FALSE(m->GetNoteInstance(note_ids_[0])); | ||
EXPECT_FALSE(m->GetNoteInstance(note_ids_[2])); | ||
} | ||
|
||
} // namespace user_notes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.