Skip to content

Commit

Permalink
Autotest API for testing AssistantState::NEW_READY
Browse files Browse the repository at this point in the history
This CL introduces the Autotest APIs used to test that the |NEW_READY| signal is
sent out when the Assistant is ready to handle queries.

Change-Id: I1dd1c366dece11be8b4c6c84e6a5c808bb110453
BUG: b/143381021
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1881968
Commit-Queue: Jeroen Dhollander <jeroendh@google.com>
Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
Reviewed-by: Xiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713546}
  • Loading branch information
Jeroen Dhollander authored and Commit Bot committed Nov 7, 2019
1 parent 24c258e commit 3b4dd47
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,59 @@ void AutotestPrivateSetAssistantEnabledFunction::Timeout() {
Respond(Error("Assistant service timed out"));
}

///////////////////////////////////////////////////////////////////////////////
// AutotestPrivateEnableAssistantAndWaitForReadyFunction
///////////////////////////////////////////////////////////////////////////////

AutotestPrivateEnableAssistantAndWaitForReadyFunction::
AutotestPrivateEnableAssistantAndWaitForReadyFunction() = default;

AutotestPrivateEnableAssistantAndWaitForReadyFunction::
~AutotestPrivateEnableAssistantAndWaitForReadyFunction() {
ash::AssistantState::Get()->RemoveObserver(this);
}

ExtensionFunction::ResponseAction
AutotestPrivateEnableAssistantAndWaitForReadyFunction::Run() {
DVLOG(1) << "AutotestPrivateEnableAssistantAndWaitForReadyFunction";

Profile* profile = Profile::FromBrowserContext(browser_context());
const std::string& err_msg =
SetWhitelistedPref(profile, chromeos::assistant::prefs::kAssistantEnabled,
base::Value(true));
if (!err_msg.empty())
return RespondNow(Error(err_msg));

// Asynchronously subscribe to status changes to avoid a possible segmentation
// fault caused by Respond() in the subscriber callback being called before
// RespondLater() below.
PostTask(
FROM_HERE, {base::CurrentThread()},
base::BindOnce(&AutotestPrivateEnableAssistantAndWaitForReadyFunction::
SubscribeToStatusChanges,
this));

// Prevent |this| from being freed before we get a response from the
// Assistant.
self_ = this;

return RespondLater();
}

void AutotestPrivateEnableAssistantAndWaitForReadyFunction::
SubscribeToStatusChanges() {
// |AddObserver| will immediately trigger |OnAssistantStatusChanged|.
ash::AssistantState::Get()->AddObserver(this);
}

void AutotestPrivateEnableAssistantAndWaitForReadyFunction::
OnAssistantStatusChanged(ash::mojom::AssistantState state) {
if (state == ash::mojom::AssistantState::NEW_READY) {
Respond(NoArguments());
self_.reset();
}
}

// AssistantInteractionHelper is a helper class used to interact with Assistant
// server and store interaction states for tests. It is shared by
// |AutotestPrivateSendAssistantTextQueryFunction| and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,29 @@ class AutotestPrivateSetAssistantEnabledFunction
base::OneShotTimer timeout_timer_;
};

// Bring up the Assistant service, and wait until the ready signal is received.
class AutotestPrivateEnableAssistantAndWaitForReadyFunction
: public ExtensionFunction,
public ash::AssistantStateObserver {
public:
AutotestPrivateEnableAssistantAndWaitForReadyFunction();
DECLARE_EXTENSION_FUNCTION("autotestPrivate.enableAssistantAndWaitForReady",
AUTOTESTPRIVATE_ENABLEASSISTANTANDWAITFORREADY)

private:
~AutotestPrivateEnableAssistantAndWaitForReadyFunction() override;
ResponseAction Run() override;

void SubscribeToStatusChanges();

// ash::AssistantStateObserver overrides:
void OnAssistantStatusChanged(ash::mojom::AssistantState state) override;

// A reference to keep |this| alive while waiting for the Assistant to
// respond.
scoped_refptr<ExtensionFunction> self_;
};

// Send text query to Assistant and return response.
class AutotestPrivateSendAssistantTextQueryFunction : public ExtensionFunction {
public:
Expand Down
4 changes: 4 additions & 0 deletions chrome/common/extensions/api/autotest_private.idl
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ namespace autotestPrivate {
static void setAssistantEnabled(boolean enabled, long timeout_ms,
VoidCallback callback);

// Bring up the Assistant service, and wait for the ready signal.
// |callback|: Called when the operation has completed.
static void enableAssistantAndWaitForReady(VoidCallback callback);

// Sends a text query via Google Assistant.
// |callback|: Called when response has been received.
static void sendAssistantTextQuery(DOMString query, long timeout_ms,
Expand Down
1 change: 1 addition & 0 deletions extensions/browser/extension_function_histogram_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,7 @@ enum HistogramValue {
AUTOTESTPRIVATE_ACTIVATEDESKATINDEX = 1402,
AUTOTESTPRIVATE_REMOVEACTIVEDESK = 1403,
TERMINALPRIVATE_GETCROSHSETTINGS = 1404,
AUTOTESTPRIVATE_ENABLEASSISTANTANDWAITFORREADY = 1405,
// Last entry: Add new entries above, then run:
// python tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY
Expand Down
1 change: 1 addition & 0 deletions tools/metrics/histograms/enums.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21253,6 +21253,7 @@ Called by update_net_error_codes.py.-->
<int value="1402" label="AUTOTESTPRIVATE_ACTIVATEDESKATINDEX"/>
<int value="1403" label="AUTOTESTPRIVATE_REMOVEACTIVEDESK"/>
<int value="1404" label="TERMINALPRIVATE_GETCROSHSETTINGS"/>
<int value="1405" label="AUTOTESTPRIVATE_ENABLEASSISTANTANDWAITFORREADY"/>
</enum>

<enum name="ExtensionIconState">
Expand Down

0 comments on commit 3b4dd47

Please sign in to comment.