-
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.
[RCTS] Add unit test for TabInteractionRecorderAndroid
Add basic unit tests that were missing on Android side. Bug: 1322240 Change-Id: I5c917a22af4110e51c1bef9b5e2b208924f90608 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4018733 Commit-Queue: Wenyu Fu <wenyufu@chromium.org> Reviewed-by: Theresa Sullivan <twellington@chromium.org> Commit-Queue: Theresa Sullivan <twellington@chromium.org> Auto-Submit: Wenyu Fu <wenyufu@chromium.org> Cr-Commit-Position: refs/heads/main@{#1070583}
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 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
110 changes: 110 additions & 0 deletions
110
...t/src/org/chromium/chrome/browser/customtabs/features/TabInteractionRecorderUnitTest.java
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,110 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.chrome.browser.customtabs.features; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
import org.chromium.base.test.BaseRobolectricTestRunner; | ||
import org.chromium.base.test.util.JniMocker; | ||
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys; | ||
import org.chromium.chrome.browser.preferences.SharedPreferencesManager; | ||
import org.chromium.chrome.browser.tab.Tab; | ||
|
||
/** Unit test for {@link TabInteractionRecorder} on the java side. */ | ||
@RunWith(BaseRobolectricTestRunner.class) | ||
public class TabInteractionRecorderUnitTest { | ||
@Rule | ||
public MockitoRule mMockitoRule = MockitoJUnit.rule(); | ||
|
||
@Rule | ||
public JniMocker mJniMocker = new JniMocker(); | ||
|
||
@Mock | ||
Tab mTab; | ||
|
||
private TestNativeInteractionRecorder mTestNative; | ||
private TabInteractionRecorder mTestRecorder; | ||
private SharedPreferencesManager mPref; | ||
|
||
@Before | ||
public void setup() { | ||
mTestNative = new TestNativeInteractionRecorder(); | ||
mJniMocker.mock(TabInteractionRecorderJni.TEST_HOOKS, mTestNative); | ||
mPref = SharedPreferencesManager.getInstance(); | ||
|
||
TabInteractionRecorder.createForTab(mTab); | ||
TabInteractionRecorder recorder = TabInteractionRecorder.getFromTab(mTab); | ||
Assert.assertNotNull("Recorder not created.", recorder); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
mPref.removeKey(ChromePreferenceKeys.CUSTOM_TABS_LAST_CLOSE_TAB_INTERACTION); | ||
mPref.removeKey(ChromePreferenceKeys.CUSTOM_TABS_LAST_CLOSE_TIMESTAMP); | ||
} | ||
|
||
@Test | ||
public void hadInteractionOnTabClosing() { | ||
mTestNative.paramHadInteraction = true; | ||
TabInteractionRecorder.getFromTab(mTab).onTabClosing(); | ||
|
||
Assert.assertTrue("Shared pref <CUSTOM_TABS_LAST_CLOSE_TAB_INTERACTION> is not recorded.", | ||
mPref.contains(ChromePreferenceKeys.CUSTOM_TABS_LAST_CLOSE_TAB_INTERACTION)); | ||
Assert.assertTrue("Value of shared pref <CUSTOM_TABS_LAST_CLOSE_TAB_INTERACTION> is wrong.", | ||
mPref.readBoolean( | ||
ChromePreferenceKeys.CUSTOM_TABS_LAST_CLOSE_TAB_INTERACTION, false)); | ||
Assert.assertTrue("Shared pref <CUSTOM_TABS_LAST_CLOSE_TIMESTAMP> is not recorded.", | ||
mPref.contains(ChromePreferenceKeys.CUSTOM_TABS_LAST_CLOSE_TIMESTAMP)); | ||
} | ||
|
||
@Test | ||
public void noInteractionOnTabClosing() { | ||
mTestNative.paramHadInteraction = false; | ||
TabInteractionRecorder.getFromTab(mTab).onTabClosing(); | ||
|
||
Assert.assertTrue("Shared pref <CUSTOM_TABS_LAST_CLOSE_TAB_INTERACTION> is not recorded.", | ||
mPref.contains(ChromePreferenceKeys.CUSTOM_TABS_LAST_CLOSE_TAB_INTERACTION)); | ||
Assert.assertFalse( | ||
"Value of shared pref <CUSTOM_TABS_LAST_CLOSE_TAB_INTERACTION> is wrong.", | ||
mPref.readBoolean( | ||
ChromePreferenceKeys.CUSTOM_TABS_LAST_CLOSE_TAB_INTERACTION, false)); | ||
Assert.assertTrue("Shared pref <CUSTOM_TABS_LAST_CLOSE_TIMESTAMP> is not recorded.", | ||
mPref.contains(ChromePreferenceKeys.CUSTOM_TABS_LAST_CLOSE_TIMESTAMP)); | ||
} | ||
|
||
class TestNativeInteractionRecorder implements TabInteractionRecorder.Natives { | ||
public boolean paramDidGetUserInteraction; | ||
public boolean paramHadInteraction; | ||
|
||
@Override | ||
public TabInteractionRecorder getFromTab(Tab tab) { | ||
return mTestRecorder; | ||
} | ||
|
||
@Override | ||
public TabInteractionRecorder createForTab(Tab tab) { | ||
mTestRecorder = new TabInteractionRecorder(); | ||
return mTestRecorder; | ||
} | ||
|
||
@Override | ||
public boolean didGetUserInteraction(long nativeTabInteractionRecorderAndroid) { | ||
return paramDidGetUserInteraction; | ||
} | ||
|
||
@Override | ||
public boolean hadInteraction(long nativeTabInteractionRecorderAndroid) { | ||
return paramHadInteraction; | ||
} | ||
} | ||
} |