• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.mojo;
6 
7 import android.support.annotation.IntDef;
8 
9 import org.junit.rules.ExternalResource;
10 
11 import org.chromium.base.annotations.JNINamespace;
12 import org.chromium.base.library_loader.LibraryLoader;
13 import org.chromium.base.library_loader.LibraryProcessType;
14 
15 import java.lang.annotation.Retention;
16 import java.lang.annotation.RetentionPolicy;
17 
18 /**
19  * Base class to test mojo. Setup the environment.
20  */
21 @JNINamespace("mojo::android")
22 public class MojoTestRule extends ExternalResource {
23     @IntDef({MojoCore.SKIP_INITIALIZATION, MojoCore.INITIALIZE})
24     @Retention(RetentionPolicy.SOURCE)
25     public @interface MojoCore {
26         int SKIP_INITIALIZATION = 0;
27         int INITIALIZE = 1;
28     }
29 
30     private static boolean sIsCoreInitialized = false;
31     private final boolean mShouldInitCore;
32     private long mTestEnvironmentPointer;
33 
MojoTestRule()34     public MojoTestRule() {
35         this(MojoCore.SKIP_INITIALIZATION);
36     }
37 
MojoTestRule(@ojoCore int shouldInitMojoCore)38     public MojoTestRule(@MojoCore int shouldInitMojoCore) {
39         mShouldInitCore = shouldInitMojoCore == MojoCore.INITIALIZE;
40     }
41 
42     @Override
before()43     protected void before() throws Throwable {
44         LibraryLoader.getInstance().ensureInitialized(LibraryProcessType.PROCESS_BROWSER);
45         if (mShouldInitCore && !sIsCoreInitialized) {
46             nativeInitCore();
47             sIsCoreInitialized = true;
48         }
49         nativeInit();
50         mTestEnvironmentPointer = nativeSetupTestEnvironment();
51     }
52 
53     @Override
after()54     protected void after() {
55         nativeTearDownTestEnvironment(mTestEnvironmentPointer);
56     }
57 
58     /**
59      * Runs the run loop for the given time.
60      */
runLoop(long timeoutMS)61     public void runLoop(long timeoutMS) {
62         nativeRunLoop(timeoutMS);
63     }
64 
65     /**
66      * Runs the run loop until no handle or task are immediately available.
67      */
runLoopUntilIdle()68     public void runLoopUntilIdle() {
69         nativeRunLoop(0);
70     }
71 
nativeInitCore()72     private static native void nativeInitCore();
73 
nativeInit()74     private native void nativeInit();
75 
nativeSetupTestEnvironment()76     private native long nativeSetupTestEnvironment();
77 
nativeTearDownTestEnvironment(long testEnvironment)78     private native void nativeTearDownTestEnvironment(long testEnvironment);
79 
nativeRunLoop(long timeoutMS)80     private native void nativeRunLoop(long timeoutMS);
81 }
82