Categories
discuss

Prevent firebase test labs from clicking ads

I used Firebase Test Lab to test my app before uploading it to Google Play.

Looking at the screenshots and video it looks like real ads are displayed from admob and then clicked on.

Now I know you can set test devices to show test ads but I cannot find any ids for firebase test devices, or can I in code somehow identify that this is a robo test so I can prevent real ads?

Also I wonder if google play pre launch report tests has the same issues?

Answer

You can detect if the device is running in Firebase Test Lab in your code by looking for the environment variable firebase.test.lab as documented in the official documentation.

String testLabSetting = Settings.System.getString(getContentResolver(), "firebase.test.lab");
if ("true".equals(testLabSetting)) {
    // Do something when running in Test Lab
}

The same will work for Google Play Pre Launch reports.

Categories
discuss

Production code + Test module-info = Unpossible?

I have a mock class with a trivial implementation of a service I provide from a module. I’m using OpenJDK 11.03, gradle 5.2.1 and IntelliJ 2019.2.

In /main/code/myPackage/myService.java I have:

package myPackage;
class myService {
   public abstract void someFunction();
}

And in my test/code/somePackage/myMockService I have:

package myPackage;
// no import, they're in the same package.
class myMockService extends myService {
   @Override
   public void someFunction() { System.out.prinln("Hello World"); }
}

In my main/code/module-info.java I have:

module myModule {
    exports somePackage;
}

I’ve tried several variations on a test/code/module-info.java, without success. For example:

// "open module" lets anyone use reflection within (mostly JUnit 5 in my case)
import myPackage.myService;
import myPackage.myMockService;
open module myTestModule { 
    exports myPackage;
    provides myService with myMockService
}

The above module-info.java spews errors about how “module name myTestModule does not match expected name myModule”, “package ‘myPackage’ is not visible” (from myMockModule.java), explaining “package myPackage is declared in module myModule but module myTestModule does not read it”

On the other hand, with the following module-info.java, I get a different batch of errors (below the code)

import myPackage.myService;
import myPackage.myMockService;
open module myModule {
    provides myService with myMockService;
}

Without a requires myModule;, every reference to the main code branch from my test code gives an “error: cannot find symbol”. With a requires myModule;, I get an “error: cyclic dependence involving myModule”.

So… my tests can’t be in a different module. AND they can’t be the same module! [long string of expletives deleted]

  • How do I introduce a mock version of a service in test code rather than creating an entirely different module/gradle sub-project?

  • Or is this simply a case where that’s not possible, and while you can have a separate test module-info, you can’t do much with it?

  • Or is there some way to dynamically load things at runtime such that I don’t have to put every little mock service in any module-info, test or otherwise? Such that ServiceLoader.load() will find them. Hmm… perhaps extend ServiceLoader and wrap its usage in main code such that it’ll use the right one either in production code or test code…

Answer

a) Welcome to “Testing in the Modular World”!

TL;DR https://sormuras.github.io/blog/2018-09-11-testing-in-the-modular-world.html

Having one or more dedicated test modules is good. With all bells-and-whistles, read module-info.java declarations. Those test modules are your main modules’ first clients. Just make sure, that your build tool packages all main modules before compiling and running the test modules. Otherwise you don’t test your main modules as close as possible to reality — others will consume your main modules as JAR files. So should you. This solves all issues with services and multi-release JARs as well.

Now the interesting part: in-module testing, also named white box testing. Or how do test types residing non-exported packages or package-private types in exported packages? Either use a build that knows how to patch test modules into main modules (or vice versa) at test compile and/or test runtime. Like pro or Bach.java (which I maintain), or in your case of using Gradle, see b)elow part of this answer.

b) Gradle and Java main, test, … modules are not friends out-of-the-box, yet

Best plugin-based solution: https://github.com/java9-modularity/gradle-modules-plugin — which honors the pass theses java command line options at test runtime module-info.test configuration file (which I invented). Here you basically desribe your test module requirements via verbose command line options, although a perfect DSL already exists: module-info-java … loop back to a) and the module-aware build tools.

c) IntelliJ IDEA and Java test modules are … improving!

Categories
discuss

Android Picture-in-picture mode, open another activity of my app

I have a video call activity and I am using pip mode for it. In pip mode, I try to open related chat activity using remote action. But when a startActivity is called from pip activity, the new activity is being opened in the pip screen too. But I do want to open the chat in our app, not in pip screen.
Is it possible to start our own activity outside of the pip screen?

Answer

Adding taskAffinity solves my problem.
You can see detail here: https://developer.android.com/guide/topics/manifest/activity-element#aff

Categories
discuss

Android dependency ‘android.arch.lifecycle:runtime’ has different version for the compile (1.0.0) and runtime (1.1.1) classpath

So my flutter android app runs fine with firebase google services 4.0.1, but when I add the dependancy “firebase_auth” to pubspec.yaml, it produces this error:

Execution failed for task ‘:app:preDebugBuild’.

Android dependency ‘android.arch.lifecycle:runtime’ has different version for the compile (1.0.0) and runtime (1.1.1) classpath. You should manually set the same version via DependencyResolution

The package is gotten correctly in the pubspec. Any help would be appreciated.

Answer

add to app level build.gradle.

implementation "android.arch.lifecycle:extensions:1.1.1"
testImplementation "android.arch.core:core-testing:1.1.1"

Good Luck dear.

Categories
discuss

Enable CORS in my React App with Node.js backend

I used create-react-app to build my react app. This app does a POST call on another API (elasticsearch), hosted on a different server (not owned/managed by me). So once the user enters the data in the form, onSubmit basically calls getResponse() method that makes the call. Initialize client:

let client = new elasticsearch.Client({
    host: "https://{cred.user}:{cred.pass}@@servername.domain:11121",
    log: "trace",
});

API query:

getResponse = () => {
        client
          .search({
            index: 'custom_index_1',
            body: {
                query: {
                    match: {"data": this.state.data}
                },
            }
        },function(error, response, status) {
            if (error) {
                const errorMessage= {error};
                console.log(errorMessage);
            }
            else {
                this.setState({results: response.hits.hits});
                console.log(this.state.results);
            }
        });
    }

But I’m getting the CORS error as follows:

Failed to load resource: the server responded with a status of 403 (Forbidden)
localhost/:1 Access to XMLHttpRequest at 'https://servername.domain:11121/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.console.js:40 
WARNING: 2019-11-21T07:54:32Z No living connections

After reading in SO about the same issue, I found that using the cors npm module can fix this issue (at least in development). But I do not understand where do I put this code:

var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())

OR

var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors())

app.get('/products/:id', function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for all origins!'})
})

app.listen(80, function () {
  console.log('CORS-enabled web server listening on port 80')
})

My questions are as follows: 1. Where do I add this code above? in my react app’s app.js? If yes, can someone give me an example please. I am using something like this:

import statements
class App extends Component {
 <code>
}
export default App;
  1. If this code is to be added to some other file then which file? Is it server.js? If yes, where do I find this? I’m using Windows 7. Node is installed in a customer directory: C:MYSWNodeJs12.1.0. I see some server.js in here: C:Usersuser1Scratchnodemyreact_uinode_modulesreact-domserver.js, but is it the right file

  2. Please give me a sample of how the code is added and where exactly in the file. I am new to React and Node so I do not know much about the internals. I’v been stuck here for days now and really need some help. Thanks.

Everywhere it says, add this code and it work, no one mentions where do I add it and how? Any help appreciated.

Answer

You need to set up a proxy server for API requests – https://create-react-app.dev/docs/proxying-api-requests-in-development/

I do not fully understand the details for Elastic server, but you can put the Express code into src/server.js file, inspired by https://stackoverflow.com/a/20539239/1176601:

var express = require('express')
var request = require('request')
var cors = require('cors')
var app = express()

app.use(cors())

app.use('/', function(req, res) {
  var url = 'https://' +
    req.get('host').replace('localhost:80', 'servername.domain:11121') + 
    req.url
  req.pipe(request({ qs:req.query, uri: url })).pipe(res);
})

app.listen(80, function () {
  console.log('CORS-enabled web server listening on port 80')
})

update package.json

"scripts": {
   ...
   "server": "node src/server.js"
},
"proxy": "http://localhost:80"

Modify your client:

let client = new elasticsearch.Client({
    host: "{cred.user}:{cred.pass}@@localhost:80",
    log: "trace",
});

And start it by npm run server (before or after npm start, e.g. in a separate terminal).

Before the first start, you will need to install all required modules, npm i -D express request cors.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..