Skip to content

Commit

Permalink
Add test for SurveyHttpClientBridge
Browse files Browse the repository at this point in the history
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
fwy423 authored and Chromium LUCI CQ committed Feb 18, 2021
1 parent 4d36db7 commit 77f6387
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
1 change: 1 addition & 0 deletions chrome/android/chrome_junit_test_java_sources.gni
Expand Up @@ -210,6 +210,7 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/status_indicator/StatusIndicatorMediatorTest.java",
"junit/src/org/chromium/chrome/browser/suggestions/SuggestionsImageFetcherTest.java",
"junit/src/org/chromium/chrome/browser/survey/ChromeSurveyControllerTest.java",
"junit/src/org/chromium/chrome/browser/survey/SurveyHttpClientBridgeUnitTest.java",
"junit/src/org/chromium/chrome/browser/tab/HistoricalTabSaverUnitTest.java",
"junit/src/org/chromium/chrome/browser/tab/TabAssociatedAppTest.java",
"junit/src/org/chromium/chrome/browser/tab/TabAttributesTest.java",
Expand Down
1 change: 1 addition & 0 deletions chrome/android/chrome_test_java_sources.gni
Expand Up @@ -527,6 +527,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/suggestions/tile/TileGridLayoutTest.java",
"javatests/src/org/chromium/chrome/browser/suggestions/tile/TileGroupTest.java",
"javatests/src/org/chromium/chrome/browser/suggestions/tile/TileGroupUnitTest.java",
"javatests/src/org/chromium/chrome/browser/survey/SurveyHttpClientBridgeTest.java",
"javatests/src/org/chromium/chrome/browser/sync/AccountManagementFragmentTest.java",
"javatests/src/org/chromium/chrome/browser/sync/AndroidSyncSettingsTest.java",
"javatests/src/org/chromium/chrome/browser/sync/AutofillTest.java",
Expand Down
Expand Up @@ -4,6 +4,8 @@

package org.chromium.chrome.browser.survey;

import androidx.annotation.VisibleForTesting;

import org.chromium.base.Callback;
import org.chromium.base.Consumer;
import org.chromium.base.annotations.CalledByNative;
Expand Down Expand Up @@ -110,6 +112,7 @@ public void send(GURL gurl, String requestType, byte[] body, Map<String, String>
* @param headerKeys Keys of the headers for the HttpResponse.
* @param headerValues Values of the headers for the HttpResponse.
*/
@VisibleForTesting
@CalledByNative
public static HttpResponse createHttpResponse(int responseCode, int netErrorCode, byte[] body,
String[] headerKeys, String[] headerValues) {
Expand Down
@@ -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);
}
}
@@ -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);
}
}

0 comments on commit 77f6387

Please sign in to comment.