Bug 1510314 - Add some initial basic tests for the GeckoView class r=geckoview-reviewers,esawin

Differential Revision: https://phabricator.services.mozilla.com/D36113

--HG--
extra : moz-landing-system : lando
This commit is contained in:
James Willcox 2019-07-24 14:53:19 +00:00
parent 72ccde08db
commit 5f8a13fc44
4 changed files with 104 additions and 3 deletions

View File

@ -17,6 +17,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".TestRunnerActivity" android:exported="true"/>
<activity android:name=".GeckoViewTestActivity" android:exported="true"/>
<activity-alias android:name=".App" android:targetActivity=".TestRunnerActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

View File

@ -0,0 +1,69 @@
package org.mozilla.geckoview.test
import android.support.test.filters.LargeTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import android.support.v4.view.ViewCompat
import android.view.View
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule
@RunWith(AndroidJUnit4::class)
@LargeTest
class GeckoViewTest {
val activityRule = ActivityTestRule<GeckoViewTestActivity>(GeckoViewTestActivity::class.java)
var sessionRule = GeckoSessionTestRule()
val view get() = activityRule.activity.view
@get:Rule
val rules = RuleChain.outerRule(activityRule).around(sessionRule)
@Before
fun setup() {
// Attach the default session from the session rule to the GeckoView
view.setSession(sessionRule.session)
}
@After
fun cleanup() {
view.releaseSession()
}
@Test
fun setSessionOnClosed() {
view.session!!.close()
view.setSession(GeckoSession())
}
@Test(expected = IllegalStateException::class)
fun setSessionOnOpenThrows() {
assertThat("Session is open", view.session!!.isOpen, equalTo(true))
view.setSession(GeckoSession())
}
@Test(expected = java.lang.IllegalStateException::class)
fun displayAlreadyAcquired() {
assertThat("View should be attached",
ViewCompat.isAttachedToWindow(view), equalTo(true))
view.session!!.acquireDisplay()
}
@Test
fun relaseOnDetach() {
// The GeckoDisplay should be released when the View is detached from the window...
view.onDetachedFromWindow()
view.session!!.releaseDisplay(view.session!!.acquireDisplay())
}
}

View File

@ -0,0 +1,23 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.geckoview.test;
import org.mozilla.geckoview.GeckoView;
import android.app.Activity;
import android.os.Bundle;
public class GeckoViewTestActivity extends Activity {
public GeckoView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new GeckoView(this);
setContentView(view);
}
}

View File

@ -30,6 +30,7 @@ import android.support.annotation.AnyThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.SparseArray;
@ -123,7 +124,9 @@ public class GeckoView extends FrameLayout {
public GeckoDisplay release() {
if (mValid) {
mDisplay.surfaceDestroyed();
if (mDisplay != null) {
mDisplay.surfaceDestroyed();
}
GeckoView.this.setActive(false);
}
@ -349,7 +352,10 @@ public class GeckoView extends FrameLayout {
releaseSession();
mSession = session;
mDisplay.acquire(session.acquireDisplay());
if (ViewCompat.isAttachedToWindow(this)) {
mDisplay.acquire(session.acquireDisplay());
}
final Context context = getContext();
session.getOverscrollEdgeEffect().setTheme(context);
@ -424,8 +430,10 @@ public class GeckoView extends FrameLayout {
if (runtime != null) {
runtime.orientationChanged();
}
}
if (mSession != null) {
mDisplay.acquire(mSession.acquireDisplay());
}
super.onAttachedToWindow();