Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
f2932b6a
Commit
f2932b6a
authored
Dec 15, 2016
by
Jason Simmons
Committed by
GitHub
Dec 15, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
An Android instrumentation test using a FlutterView (#7262)
parent
bf289a2a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
159 additions
and
0 deletions
+159
-0
build.gradle
examples/hello_services/android/app/build.gradle
+10
-0
ExampleInstrumentedTest.java
...est/java/com/example/flutter/ExampleInstrumentedTest.java
+143
-0
build.gradle
examples/hello_services/android/build.gradle
+6
-0
No files found.
examples/hello_services/android/app/build.gradle
View file @
f2932b6a
...
...
@@ -8,8 +8,18 @@ android {
lintOptions
{
disable
'InvalidPackage'
}
defaultConfig
{
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
}
flutter
{
source
'../..'
}
dependencies
{
androidTestCompile
'com.android.support:support-annotations:22.0.0'
androidTestCompile
'com.android.support.test:runner:0.5'
androidTestCompile
'com.android.support.test:rules:0.5'
}
examples/hello_services/android/app/src/androidTest/java/com/example/flutter/ExampleInstrumentedTest.java
0 → 100644
View file @
f2932b6a
package
com
.
example
.
flutter
;
import
android.graphics.Bitmap
;
import
android.support.test.InstrumentationRegistry
;
import
android.support.test.rule.ActivityTestRule
;
import
android.support.test.runner.AndroidJUnit4
;
import
io.flutter.view.FlutterView
;
import
android.app.Instrumentation
;
import
android.support.test.InstrumentationRegistry
;
import
java.util.concurrent.CountDownLatch
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
static
org
.
junit
.
Assert
.*;
@RunWith
(
AndroidJUnit4
.
class
)
public
class
ExampleInstrumentedTest
{
@Rule
public
ActivityTestRule
<
ExampleActivity
>
activityRule
=
new
ActivityTestRule
<>(
ExampleActivity
.
class
);
@Test
public
void
testFlutterMessage
()
{
final
Instrumentation
instr
=
InstrumentationRegistry
.
getInstrumentation
();
final
JSONObject
message
=
new
JSONObject
();
final
int
RANDOM_MIN
=
1
;
final
int
RANDOM_MAX
=
1000
;
try
{
message
.
put
(
"min"
,
RANDOM_MIN
);
message
.
put
(
"max"
,
RANDOM_MAX
);
}
catch
(
JSONException
e
)
{
fail
(
e
.
getMessage
());
}
final
CountDownLatch
latch
=
new
CountDownLatch
(
1
);
final
AtomicInteger
random
=
new
AtomicInteger
();
instr
.
runOnMainSync
(
new
Runnable
()
{
public
void
run
()
{
final
FlutterView
flutterView
=
(
FlutterView
)
activityRule
.
getActivity
().
findViewById
(
R
.
id
.
flutter_view
);
flutterView
.
sendToFlutter
(
"getRandom"
,
message
.
toString
(),
new
FlutterView
.
MessageReplyCallback
()
{
public
void
onReply
(
String
json
)
{
try
{
JSONObject
reply
=
new
JSONObject
(
json
);
random
.
set
(
reply
.
getInt
(
"value"
));
}
catch
(
JSONException
e
)
{
fail
(
e
.
getMessage
());
}
finally
{
latch
.
countDown
();
}
}
});
}
});
try
{
assertTrue
(
latch
.
await
(
2
,
TimeUnit
.
SECONDS
));
}
catch
(
InterruptedException
e
)
{
fail
(
e
.
getMessage
());
}
assertTrue
(
random
.
get
()
>=
RANDOM_MIN
);
assertTrue
(
random
.
get
()
<
RANDOM_MAX
);
}
@Test
public
void
testBitmap
()
{
final
Instrumentation
instr
=
InstrumentationRegistry
.
getInstrumentation
();
final
BitmapPoller
poller
=
new
BitmapPoller
(
5
);
instr
.
runOnMainSync
(
new
Runnable
()
{
public
void
run
()
{
final
FlutterView
flutterView
=
(
FlutterView
)
activityRule
.
getActivity
().
findViewById
(
R
.
id
.
flutter_view
);
// Call onPostResume to start the engine's renderer even if the activity
// is paused in the test environment.
flutterView
.
onPostResume
();
poller
.
start
(
flutterView
);
}
});
Bitmap
bitmap
=
null
;
try
{
bitmap
=
poller
.
waitForBitmap
();
}
catch
(
InterruptedException
e
)
{
fail
(
e
.
getMessage
());
}
assertNotNull
(
bitmap
);
assertTrue
(
bitmap
.
getWidth
()
>
0
);
assertTrue
(
bitmap
.
getHeight
()
>
0
);
// Check that a pixel matches the default Material background color.
assertTrue
(
bitmap
.
getPixel
(
bitmap
.
getWidth
()
-
1
,
bitmap
.
getHeight
()
-
1
)
==
0xFFFAFAFA
);
}
// Waits on a FlutterView until it is able to produce a bitmap.
private
class
BitmapPoller
{
private
int
triesPending
;
private
int
waitMsec
;
private
FlutterView
flutterView
;
private
Bitmap
bitmap
;
private
CountDownLatch
latch
=
new
CountDownLatch
(
1
);
private
final
int
delayMsec
=
1000
;
BitmapPoller
(
int
tries
)
{
triesPending
=
tries
;
waitMsec
=
delayMsec
*
tries
+
100
;
}
void
start
(
FlutterView
flutterView
)
{
this
.
flutterView
=
flutterView
;
flutterView
.
postDelayed
(
checkBitmap
,
delayMsec
);
}
Bitmap
waitForBitmap
()
throws
InterruptedException
{
latch
.
await
(
waitMsec
,
TimeUnit
.
MILLISECONDS
);
return
bitmap
;
}
private
Runnable
checkBitmap
=
new
Runnable
()
{
public
void
run
()
{
bitmap
=
flutterView
.
getBitmap
();
triesPending
--;
if
(
bitmap
!=
null
||
triesPending
==
0
)
{
latch
.
countDown
();
}
else
{
flutterView
.
postDelayed
(
checkBitmap
,
delayMsec
);
}
}
};
}
}
examples/hello_services/android/build.gradle
View file @
f2932b6a
...
...
@@ -8,6 +8,12 @@ buildscript {
}
}
allprojects
{
repositories
{
jcenter
()
}
}
task
clean
(
type:
Delete
)
{
delete
rootProject
.
buildDir
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment