Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug: 1151379 Change-Id: Ic53024af5f86f853cb229136f5c053f83f2e3107 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2685612 Commit-Queue: Wenyu Fu <wenyufu@chromium.org> Reviewed-by: Sky Malice <skym@chromium.org> Reviewed-by: Theresa <twellington@chromium.org> Cr-Commit-Position: refs/heads/master@{#855083}
- Loading branch information
Showing
5 changed files
with
212 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
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
80 changes: 80 additions & 0 deletions
80
.../android/javatests/src/org/chromium/chrome/browser/survey/SurveyHttpClientBridgeTest.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,80 @@ | ||
// Copyright 2021 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. | ||
|
||
package org.chromium.chrome.browser.survey; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.test.filters.SmallTest; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.chromium.base.Consumer; | ||
import org.chromium.base.ContextUtils; | ||
import org.chromium.base.test.util.CallbackHelper; | ||
import org.chromium.chrome.browser.profiles.Profile; | ||
import org.chromium.chrome.browser.survey.SurveyHttpClientBridge.HttpResponse; | ||
import org.chromium.chrome.test.ChromeBrowserTestRule; | ||
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | ||
import org.chromium.content_public.browser.test.util.TestThreadUtils; | ||
import org.chromium.net.test.EmbeddedTestServer; | ||
import org.chromium.url.GURL; | ||
|
||
import java.util.HashMap; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
/** | ||
* Integration test for {@link SurveyHttpClientBridge}. | ||
*/ | ||
@RunWith(ChromeJUnit4ClassRunner.class) | ||
public class SurveyHttpClientBridgeTest { | ||
private static final String TEST_PAGE = "/chrome/test/data/android/simple.html"; | ||
|
||
@Rule | ||
public ChromeBrowserTestRule mTestRule = new ChromeBrowserTestRule(); | ||
|
||
private Context mContext; | ||
private EmbeddedTestServer mTestServer; | ||
private SurveyHttpClientBridge mHttpClient; | ||
|
||
private Consumer<HttpResponse> mConsumer; | ||
|
||
public HttpResponse mLastAcceptedResponse; | ||
|
||
private final CallbackHelper mCallbackHelper = new CallbackHelper(); | ||
|
||
@Before | ||
public void setUp() throws ExecutionException { | ||
mContext = ContextUtils.getApplicationContext(); | ||
mTestServer = EmbeddedTestServer.createAndStartServer(mContext); | ||
mConsumer = response -> { | ||
mLastAcceptedResponse = response; | ||
mCallbackHelper.notifyCalled(); | ||
}; | ||
|
||
TestThreadUtils.runOnUiThreadBlocking( | ||
() | ||
-> mHttpClient = | ||
new SurveyHttpClientBridge(Profile.getLastUsedRegularProfile())); | ||
} | ||
|
||
@Test | ||
@SmallTest | ||
public void testSendRequest() throws TimeoutException { | ||
String url = mTestServer.getURL(TEST_PAGE); | ||
GURL gurl = new GURL(url); | ||
String body = ""; | ||
|
||
TestThreadUtils.runOnUiThreadBlocking( | ||
() -> mHttpClient.send(gurl, "POST", body.getBytes(), new HashMap<>(), mConsumer)); | ||
|
||
mCallbackHelper.waitForFirst(); | ||
Assert.assertNotNull(mLastAcceptedResponse); | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
.../android/junit/src/org/chromium/chrome/browser/survey/SurveyHttpClientBridgeUnitTest.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,127 @@ | ||
// Copyright 2021 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. | ||
|
||
package org.chromium.chrome.browser.survey; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
import org.chromium.base.test.BaseRobolectricTestRunner; | ||
import org.chromium.base.test.util.CallbackHelper; | ||
import org.chromium.base.test.util.JniMocker; | ||
import org.chromium.chrome.browser.profiles.Profile; | ||
import org.chromium.chrome.browser.survey.SurveyHttpClientBridge.HttpResponse; | ||
import org.chromium.url.GURL; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** Unit test for {@link SurveyHttpClientBridge}. */ | ||
@RunWith(BaseRobolectricTestRunner.class) | ||
public class SurveyHttpClientBridgeUnitTest { | ||
private static final long FAKE_NATIVE_POINTER = 123456789L; | ||
|
||
@Rule | ||
public JniMocker mMocker = new JniMocker(); | ||
@Rule | ||
public MockitoRule mRule = MockitoJUnit.rule(); | ||
|
||
@Mock | ||
public SurveyHttpClientBridge.Natives mNativeMock; | ||
@Mock | ||
public Profile mMockProfile; | ||
@Mock | ||
public GURL mMockGurl; | ||
|
||
@Captor | ||
public ArgumentCaptor<String[]> mHeaderKeysCaptor; | ||
@Captor | ||
public ArgumentCaptor<String[]> mHeaderValuesCaptor; | ||
|
||
private SurveyHttpClientBridge mSurveyHttpClientBridge; | ||
|
||
@Before | ||
public void setUp() { | ||
mMocker.mock(SurveyHttpClientBridgeJni.TEST_HOOKS, mNativeMock); | ||
|
||
Mockito.when(mNativeMock.init(mMockProfile)).thenReturn(FAKE_NATIVE_POINTER); | ||
|
||
mSurveyHttpClientBridge = new SurveyHttpClientBridge(mMockProfile); | ||
|
||
Mockito.verify(mNativeMock).init(mMockProfile); | ||
} | ||
|
||
@Test | ||
public void testDestroy() { | ||
mSurveyHttpClientBridge.destroy(); | ||
Mockito.verify(mNativeMock).destroy(FAKE_NATIVE_POINTER); | ||
} | ||
|
||
@Test | ||
public void testParseHeader() { | ||
final String keyFoo = "Foo"; | ||
final String keyBar = "Bar"; | ||
final String valFoo = "valFoo"; | ||
final String valBar = "valBar"; | ||
|
||
final Map<String, String> headers = new HashMap<>(); | ||
headers.put(keyFoo, valFoo); | ||
headers.put(keyBar, valBar); | ||
|
||
final CallbackHelper responseCallback = new CallbackHelper(); | ||
final byte[] requestBody = {}; | ||
final String requestType = "requestType"; | ||
|
||
Mockito.when(mMockGurl.isValid()).thenReturn(true); | ||
mSurveyHttpClientBridge.send(mMockGurl, requestType, requestBody, headers, | ||
(response) -> responseCallback.notifyCalled()); | ||
|
||
Mockito.verify(mNativeMock) | ||
.sendNetworkRequest(eq(FAKE_NATIVE_POINTER), eq(mMockGurl), eq(requestType), | ||
eq(requestBody), mHeaderKeysCaptor.capture(), mHeaderValuesCaptor.capture(), | ||
any()); | ||
|
||
// Verify the key value for the string does match. | ||
String[] headerKeys = mHeaderKeysCaptor.getValue(); | ||
String[] headerValues = mHeaderValuesCaptor.getValue(); | ||
|
||
Assert.assertEquals("Header key array length is different.", 2, headerKeys.length); | ||
Assert.assertEquals("Header value array length is different.", 2, headerValues.length); | ||
|
||
Assert.assertEquals("Header keys and values should match at the index 0.", | ||
"val" + headerKeys[0], headerValues[0]); | ||
Assert.assertEquals("Header keys and values should match at the index 1.", | ||
"val" + headerKeys[1], headerValues[1]); | ||
} | ||
|
||
@Test | ||
public void testCreateHttpResponse() { | ||
String[] responseHeaderKeys = {"Foo", "Foo", "Bar"}; | ||
String[] responseHeaderValues = {"valFoo1", "valFoo2", "valBar"}; | ||
byte[] responseBody = "responseBody".getBytes(); | ||
|
||
HttpResponse response = SurveyHttpClientBridge.createHttpResponse( | ||
200, 0, responseBody, responseHeaderKeys, responseHeaderValues); | ||
|
||
Assert.assertNotNull(response.mHeaders.get("Foo")); | ||
Assert.assertNotNull(response.mHeaders.get("Bar")); | ||
|
||
Assert.assertEquals(2, response.mHeaders.get("Foo").size()); | ||
Assert.assertEquals(1, response.mHeaders.get("Bar").size()); | ||
Assert.assertEquals(200, response.mResponseCode); | ||
Assert.assertEquals(responseBody, response.mBody); | ||
} | ||
} |