Commit ae51cb4a authored by Basia Zimirska's avatar Basia Zimirska Committed by Chromium LUCI CQ

Update mic icon after profile loads

This CL fixes an issue with the UI not being updated after the profile loads and the policy becomes available. Now after a callback about profile creation is called, updating mic visibility is requested.

Testing this change: I didn't test it end to end with my own policy server, I did it in a somewhat hacky way with a bool variable that was flipped in the callback to make sure that UI is updated.


Bug: 1161022
Change-Id: I4fc8ab9067044b9198efaba0b94acf0199504492
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2623164Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Reviewed-by: default avatarBrandon Wylie <wylieb@chromium.org>
Reviewed-by: default avatarJosh Simmons <jds@google.com>
Commit-Queue: Basia Zimirska <basiaz@google.com>
Cr-Commit-Position: refs/heads/master@{#844188}
parent a928970f
...@@ -128,6 +128,7 @@ class LocationBarMediator implements LocationBarDataProvider.Observer, FakeboxDe ...@@ -128,6 +128,7 @@ class LocationBarMediator implements LocationBarDataProvider.Observer, FakeboxDe
mAutocompleteCoordinator = null; mAutocompleteCoordinator = null;
mUrlCoordinator = null; mUrlCoordinator = null;
mPrivacyPreferencesManager = null; mPrivacyPreferencesManager = null;
mVoiceRecognitionHandler.destroy();
mVoiceRecognitionHandler = null; mVoiceRecognitionHandler = null;
mLocationBarDataProvider.removeObserver(this); mLocationBarDataProvider.removeObserver(this);
mDeferredNativeRunnables.clear(); mDeferredNativeRunnables.clear();
......
...@@ -52,7 +52,7 @@ import java.util.List; ...@@ -52,7 +52,7 @@ import java.util.List;
/** /**
* Class containing functionality related to voice search. * Class containing functionality related to voice search.
*/ */
public class VoiceRecognitionHandler { public class VoiceRecognitionHandler implements ProfileManager.Observer {
private static final String TAG = "VoiceRecognition"; private static final String TAG = "VoiceRecognition";
// The minimum confidence threshold that will result in navigating directly to a voice search // The minimum confidence threshold that will result in navigating directly to a voice search
...@@ -207,8 +207,21 @@ public class VoiceRecognitionHandler { ...@@ -207,8 +207,21 @@ public class VoiceRecognitionHandler {
Supplier<AssistantVoiceSearchService> assistantVoiceSearchServiceSupplier) { Supplier<AssistantVoiceSearchService> assistantVoiceSearchServiceSupplier) {
mDelegate = delegate; mDelegate = delegate;
mAssistantVoiceSearchServiceSupplier = assistantVoiceSearchServiceSupplier; mAssistantVoiceSearchServiceSupplier = assistantVoiceSearchServiceSupplier;
ProfileManager.addObserver(this);
} }
/**
* After profile is created and prefs loaded ensure that UI is updated and the mic shown/hidden
* as needed.
*/
@Override
public void onProfileAdded(Profile profile) {
mDelegate.updateMicButtonState();
}
@Override
public void onProfileDestroyed(Profile profile) {}
/** /**
* Instantiated when a voice search is performed to monitor the web contents for a navigation * Instantiated when a voice search is performed to monitor the web contents for a navigation
* to be started so we can notify the render frame that a user gesture has been performed. This * to be started so we can notify the render frame that a user gesture has been performed. This
...@@ -864,4 +877,9 @@ public class VoiceRecognitionHandler { ...@@ -864,4 +877,9 @@ public class VoiceRecognitionHandler {
void setQueryStartTimeForTesting(Long queryStartTimeMs) { void setQueryStartTimeForTesting(Long queryStartTimeMs) {
mQueryStartTimeMs = queryStartTimeMs; mQueryStartTimeMs = queryStartTimeMs;
} }
/** Clean up. Creators must call this when the object is no longer needed. */
public void destroy() {
ProfileManager.removeObserver(this);
}
} }
...@@ -642,6 +642,24 @@ public class VoiceRecognitionHandlerTest { ...@@ -642,6 +642,24 @@ public class VoiceRecognitionHandlerTest {
Assert.assertTrue(isVoiceSearchEnabled()); Assert.assertTrue(isVoiceSearchEnabled());
} }
@Test
@SmallTest
@Feature("VoiceSearchAudioCapturePolicy")
@EnableFeatures({ChromeFeatureList.VOICE_SEARCH_AUDIO_CAPTURE_POLICY})
public void testIsVoiceSearchEnabled_UpdateAfterProfileLoads() {
setAudioCapturePref(true);
mPermissionDelegate.setCanRequestPermission(true);
mPermissionDelegate.setHasPermission(true);
Assert.assertFalse(mDelegate.updatedMicButtonState());
Assert.assertTrue(isVoiceSearchEnabled());
setAudioCapturePref(false);
TestThreadUtils.runOnUiThreadBlocking(
() -> { mHandler.onProfileAdded(Profile.getLastUsedRegularProfile()); });
Assert.assertFalse(isVoiceSearchEnabled());
Assert.assertTrue(mDelegate.updatedMicButtonState());
}
/** Calls isVoiceSearchEnabled(), ensuring it is run on the UI thread. */ /** Calls isVoiceSearchEnabled(), ensuring it is run on the UI thread. */
private boolean isVoiceSearchEnabled() { private boolean isVoiceSearchEnabled() {
return TestThreadUtils.runOnUiThreadBlockingNoException( return TestThreadUtils.runOnUiThreadBlockingNoException(
......
...@@ -16,6 +16,8 @@ import org.chromium.base.supplier.Supplier; ...@@ -16,6 +16,8 @@ import org.chromium.base.supplier.Supplier;
import org.chromium.chrome.browser.flags.ChromeFeatureList; import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher; import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import org.chromium.chrome.browser.lifecycle.ConfigurationChangedObserver; import org.chromium.chrome.browser.lifecycle.ConfigurationChangedObserver;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.profiles.ProfileManager;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.components.embedder_support.util.UrlUtilities; import org.chromium.components.embedder_support.util.UrlUtilities;
import org.chromium.ui.modaldialog.ModalDialogManager; import org.chromium.ui.modaldialog.ModalDialogManager;
...@@ -30,7 +32,7 @@ import org.chromium.ui.modelutil.PropertyModel; ...@@ -30,7 +32,7 @@ import org.chromium.ui.modelutil.PropertyModel;
* assistant support. * assistant support.
*/ */
public class VoiceToolbarButtonController public class VoiceToolbarButtonController
implements ButtonDataProvider, ConfigurationChangedObserver { implements ButtonDataProvider, ConfigurationChangedObserver, ProfileManager.Observer {
/** /**
* Default minimum width to show the voice search button. * Default minimum width to show the voice search button.
*/ */
...@@ -65,6 +67,18 @@ public class VoiceToolbarButtonController ...@@ -65,6 +67,18 @@ public class VoiceToolbarButtonController
void startVoiceRecognition(); void startVoiceRecognition();
} }
/**
* After profile is created and prefs loaded ensure that UI is updated and
* the mic shown/hidden as needed.
*/
@Override
public void onProfileAdded(Profile profile) {
updateMicButtonState();
}
@Override
public void onProfileDestroyed(Profile profile) {}
/** /**
* Creates a VoiceToolbarButtonController object. * Creates a VoiceToolbarButtonController object.
* @param context The Context for retrieving resources, etc. * @param context The Context for retrieving resources, etc.
...@@ -112,6 +126,8 @@ public class VoiceToolbarButtonController ...@@ -112,6 +126,8 @@ public class VoiceToolbarButtonController
/*supportsTinting=*/true, /*iphCommandBuilder=*/null, /*isEnabled=*/true); /*supportsTinting=*/true, /*iphCommandBuilder=*/null, /*isEnabled=*/true);
mScreenWidthDp = context.getResources().getConfiguration().screenWidthDp; mScreenWidthDp = context.getResources().getConfiguration().screenWidthDp;
ProfileManager.addObserver(this);
} }
@Override @Override
...@@ -124,11 +140,18 @@ public class VoiceToolbarButtonController ...@@ -124,11 +140,18 @@ public class VoiceToolbarButtonController
notifyObservers(mButtonData.canShow); notifyObservers(mButtonData.canShow);
} }
/** Triggers checking and possibly updating the mic visibility */
public void updateMicButtonState() {
mButtonData.canShow = shouldShowVoiceButton(mActiveTabSupplier.get());
notifyObservers(mButtonData.canShow);
}
@Override @Override
public void destroy() { public void destroy() {
mActivityLifecycleDispatcher.unregister(this); mActivityLifecycleDispatcher.unregister(this);
mModalDialogManager.removeObserver(mModalDialogManagerObserver); mModalDialogManager.removeObserver(mModalDialogManagerObserver);
mObservers.clear(); mObservers.clear();
ProfileManager.removeObserver(this);
} }
@Override @Override
......
...@@ -6,7 +6,10 @@ package org.chromium.chrome.browser.toolbar; ...@@ -6,7 +6,10 @@ package org.chromium.chrome.browser.toolbar;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
...@@ -102,4 +105,25 @@ public final class VoiceToolbarButtonControllerUnitTest { ...@@ -102,4 +105,25 @@ public final class VoiceToolbarButtonControllerUnitTest {
assertTrue(mVoiceToolbarButtonController.get(mTab).canShow); assertTrue(mVoiceToolbarButtonController.get(mTab).canShow);
} }
@EnableFeatures({ChromeFeatureList.VOICE_BUTTON_IN_TOP_TOOLBAR})
@Test
public void onProfileLoaded_isVoiceSearchEnabledChecked() {
verify(mVoiceSearchDelegate, never()).isVoiceSearchEnabled();
mVoiceToolbarButtonController.onProfileAdded(null /* ignored */);
verify(mVoiceSearchDelegate, atLeastOnce()).isVoiceSearchEnabled();
}
@EnableFeatures({ChromeFeatureList.VOICE_BUTTON_IN_TOP_TOOLBAR})
@Test
public void onProfileLoaded_canShowChanges() {
assertTrue(mVoiceToolbarButtonController.get(mTab).canShow);
// Simulate loading a profile with policy disabling mic in the UI.
doReturn(false).when(mVoiceSearchDelegate).isVoiceSearchEnabled();
mVoiceToolbarButtonController.onProfileAdded(null /* ignored */);
assertFalse(mVoiceToolbarButtonController.get(mTab).canShow);
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment