How to use Context to access String resources in UIAutomator

I was just sorting out the code today, and found that there was Chinese hard-coded in the code, which made me anxious, my colleague’s coding level is worrying.

Forget it, let's change it yourself, define the Chinese and English strings in strings.xml in the corresponding directory, and then plan to use Context to access

At the beginning I wrote this:

Context mContext = InstrumentationRegistry.getContext();
String portableHotspot = mContext.getString(R.string.portable_hotspot);

As a result, an exception was thrown:

android.content.res.Resources$NotFoundException: String resource ID #0x7f0b0024
	at android.content.res.Resources.getText(Resources.java:410)
	at android.content.res.HwResources.getText(HwResources.java:465)
	at android.content.res.Resources.getString(Resources.java:504)
	at android.content.Context.getString(Context.java:560)
	at com.example.termctrl.functions.HotpotControl.<init>(HotpotControl.java:51)
	at com.example.termctrl.UiAutoController.testHotpotControl(UiAutoController.java:778)
	at java.lang.reflect.Method.invoke(Native Method)

After checking the Internet for a long time, I haven't found the reason until I found this link:

https://stackoverflow.com/questions/17713226/accessing-resources-for-android-ui-automator-tests

Change the usage of Context:

Context mContext = InstrumentationRegistry.getTargetContext();

Note that getTargetContext is used here, click in and take a look at the source code:

public Context getTargetContext() {
    return mAppContext;
}

From the name returned, it is the context of the app. Well, after thinking about it, it should be reasonable. The source code of getContext: 

public Context getContext() {
    return mInstrContext;
}

Judging from the name, it should be the abbreviation of Instrumentation Context. It doesn't seem right. If you want to access resources, of course you need to access Application resources. Well, it seems to be the reason. So I followed along and looked at how mAppContext is assigned:

    final void init(ActivityThread thread,
            Context instrContext, Context appContext, ComponentName component, 
            IInstrumentationWatcher watcher, IUiAutomationConnection         
            uiAutomationConnection) {
        mThread = thread;
        mMessageQueue = mThread.getLooper().myQueue();
        mInstrContext = instrContext;
        mAppContext = appContext;
        mComponent = component;
        mWatcher = watcher;
        mUiAutomationConnection = uiAutomationConnection;
    }

The value is assigned in the init initialization function, and appContext is the passed parameter, which should be an application's own Context, such as Application Context.

This question seems to be very simple, it does not seem to be simple, the first time you use UIAutomator to access String resources, you can extend it to access other resources, here is a summary.

Guess you like

Origin blog.csdn.net/Xia_Leon/article/details/83017844