Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Sukarelawan kami masih belum menterjemah artikel ini ke Melayu lagi. Sertai kami dan bantu melaksanakannya!

The B2G OS simulator lets you run Gaia and Web apps in a Gecko-based environment somewhat similar to an actual device. It doesn't emulate device hardware, so it's not adequate for testing device APIs, and isn't a replacement for testing on actual hardware. However, it does have several APIs enabled that aren't available on Firefox such as the Contacts and Settings APIs. It can therefore be useful during the development of your application, or while working on the Gaia user interface itself. This article covers downloading or building the Firefox OS simulator, as well as how to use it.

Note: The easiest way to use the Firefox OS desktop client is to use the Firefox OS Simulator add-on via WebIDE. It does not require you to build the simulator yourself.

Download nightly builds

Just like Firefox Nightlies, the B2G OS simulator desktop client (identified by b2g-) is automatically built every day from the latest source code. The latest build is available from the Mozilla FTP server. Be sure to pick the latest version and the right archive for your operating system. This lets you bypass having to build it yourself. In addition, you don't have to download Gaia on your own either.

Be sure to install the application in a writeable location; the application needs to be able to update the included Gaia profile.

You can now skip ahead to Running the simulator, unless you actually want to build it for yourself. This is necessary if you want to make and test changes to the codebase.

Building the simulator

The first thing you need to do is set up a standard Mozilla build environment. Once you have that, you can pull down the code you'll need and configure it to build the Firefox OS desktop client.

Downloading the code for the first time

In a directory where you'd like the source code to go, clone the mozilla-central repository that contains all of Gecko:

 hg clone https://hg.mozilla.org/mozilla-central

Alternatively, you can download the same code from Github:

 git clone https://github.com/mozilla/gecko-dev

Updating the code

When you do subsequent builds, you should make sure to pull in the latest changes:

cd mozilla-central
hg pull -u

or

cd gecko-dev
git pull

Create a mozconfig

Next, you need to create a file called .mozconfig in the mozilla-central directory to configure the build system to build the Boot to Gecko client instead of Firefox. This file should contain the following:

. "$topsrcdir/b2g/config/mozconfigs/common"

mk_add_options MOZ_OBJDIR=../build # This line should be commented if you use gecko-dev or mozilla-central
mk_add_options MOZ_MAKE_FLAGS="-j9 -s"

ac_add_options --enable-application=b2g
ac_add_options --disable-libjpeg-turbo
 
# This option is required if you want to be able to run Gaia's tests
ac_add_options --enable-tests

# turn on mozTelephony/mozSms interfaces
# Only turn this line on if you actually have a dev phone
# you want to forward to. If you get crashes at startup,
# make sure this line is commented.
#ac_add_options --enable-b2g-ril

You also need to include the line ENABLE_MARIONETTE=1 in the file if you want to run Mochitests in the B2G desktop client (either mochitest-b2g-desktop or mochitest-plain) or if you want to run Gaia unit tests.

Building

Now you can build the desktop client with the following command (run this from inside the mozilla-central directory):

./mach build

The built client will be placed in the objdir/dist/bin directory (based on the value you specify for MOZ_OBJDIR in the mozconfig file).

Note: If you have any trouble first check the dependencies.

Downloading Gaia

By default the simulator desktop client will show an empty screen because it doesn't know which web app to load initially as the system app. The collection of system apps and default apps that come with Firefox OS — Gaia — needs to be downloaded.

To download Gaia for the first time, clone the source code repository on GitHub:

git clone https://github.com/mozilla-b2g/gaia
cd gaia

To update an already existing clone of Gaia, you can pull in the latest changes from GitHub:

cd gaia
git pull

Creating a custom-settings.json

If you know what you're doing and you want to set various config options you can create the file gaia/build/config/custom-settings.json and add them there, for example:

{
  "lockscreen.enabled": false,
  "lockscreen.locked": false,
  "devtools.debugger.remote-enabled": true
}

This example is useful for bypassing the lockscreen, which is otherwise impossible to bypass on desktop because it can't be unlocked using a mouse or trackpad.

Generating a profile

Next we need to set up Gaia's apps for the desktop simulator. This includes packaging the Gaia apps in the same way as they would be installed on the device, as well as setting up the permissions for the privileged system apps. We do this by generating a profile. The following command (run in the downloaded gaia directory) will take care of that:

make DESKTOP_SHIMS=1 NOFTU=1 DEBUG=1

This should create a profile-debug directory below the gaia directory. The new profile contains a customized extension and other configuration details needed to make B2G run properly.

Note: There is currently a bug (bug 1180103) that causes Gaia debug profiles to render with an empty homescreen when run through the Firefox OS Simulator (through WebIDE, or whatever.) This can be worked around by building with DEBUG=1 DESKTOP=0 make instead of DEBUG=1.

Running the simulator

Once you've built the client (or downloaded and installed the nightly desktop application) and downloaded Gaia , you're ready to fire up the simulator.

Running on Linux

To run the simulator on Linux using the embedded Gaia profile, just run the b2g executable.  The binary is in the archive you downloaded earlier or in the objdir/dist/bin directory if you built the client yourself.

b2g -profile gaia/profile-debug

You may experience annoying rendering problems. To avoid them, add the following line to your gaia/profile/user.js file:

user_pref("layers.acceleration.disabled", true);

Running on Mac

If you downloaded the Nightly build, you can simply launch it from the Finder as usual. Any console output is visible by running the standard Console utility program included with your Mac.

If you want to specify a different Gaia profile (such as your separate download Gaia from above), you need to bypass the b2g wrapper program and run the b2g binary. The command line is slightly more complicated due to the location of the b2g binary and the need for absolute paths when specifying the profile directory:

.../B2G.app/Contents/MacOS/b2g-bin -profile /full/path/to/gaia/profile-debug

Running on Windows

Running the Nightly build on Windows is as simple as launching b2g.exe. If you want to customize the execution, you can do so by running the b2g.exe executable instead; this bypasses the wrapper program that automatically uses the bundled Gaia.

Command line options

There are a number of command line options you can use to adjust the runtime experience while using the desktop simulator. You can get a list by using the -help option. This section covers some of the particularly interesting ones.

Specifying the screen size

You can specify the screen size of the device you want to simulate using the -screen option:

b2g -screen <width>x<height>@<dpi>

Where <width>, <height>, and <dpi> are fairly self-explanatory parameters: the width and height of the device's screen in pixels and the device resolution in DPI. Here's some real examples:

b2g -screen 320x480
b2g -screen 320x480@160

Optionally, you can specify certain devices by name to simulate their screen size and resolution:

  • iphone
  • ipad
  • nexus_s
  • galaxy_nexus
  • galaxy_tab
  • wildfire
  • tattoo
  • salsa
  • chacha

These preset devices are defined in screen.js.

In order to select different screen you probably have to specify the profile path as follow:

./b2g-bin --profile ./gaia/profile/ --screen=galaxy_tab

Opening the JavaScript console

You can open the JavaScript console when launching the desktop simulator by launching it from the command line with the -jsconsole flag. After building, just do:

.../b2g -jsconsole -profile /path/to/your/profile

If you've installed the Nightly build on a Mac, you can do the following:

/Applications/B2G.app/Contents/MacOS/b2g -jsconsole -profile /path/to/your/profile-debug

Note: On production builds of Firefox OS, console logging (for example console.log()) is disabled by default. In order to enable it, open the Developer settings and enable the Console Enabled preference.

Launching a specific application at startup

You can specify an application to be launched automatically when b2g starts up in the simulator. This is done as soon as the rest of the system is done loading up. To do this, just use the -runapp option, which takes as a parameter the name of the application to run. For example:

 .../b2g -profile /path/to/your/gaia/profile-debug -runapp email

Note: The specified name is normalized by converting it to all lower case and removing all dashes and spaces. This normalized name is then compared to similarly normalized names from the manifests of available apps' manifests. For example, the name of the email app is currently "E-mail", but -runapp email will work because of this normalization.

If you specify the -runapp option without an argument, or with an empty argument, the simulator will output to your terminal a list of the known applications as well as a brief usage message.

Note: Using the -runapp option disables the lock screen as a side effect and does not re-enable it. It's assumed that you won't use this command on a profile on which you will be testing the lock screen, or you will turn it back on manually using Settings > Screen Lock. Feel free to contribute a patch to change this behavior if it's a problem.

Usage tips

This section provides a few helpful tips to using the B2G desktop client.

  • The Esc key performs the same function as the "back" button.
  • The Home key performs the same function as the "home" button; if you're on a Mac, the Home key is available as Fn + ← (Fn + Left Arrow).
  • The End key performs the same function as the "power" button; if you're on a Mac, the End key is available as Fn + → (Fn + Right Arrow).
  • the Page Up and Page Down keys perform the same functions as the "Volume Up" and "Volume Down" buttons respectively; if you're on a Mac, the Page Up key is available as Fn + ↑ (Fn + Up Arrow) and the Page Down key is available as Fn + ↓ (Fn + Down Arrow).
  • A long press on the Home key opens the "Card View"; if you're on Mac, Cmd + Fn + ← (Cmd + Fn + Left Arrow) opens the "Card View".

Troubleshooting: A blank screen when the simulator starts

When you start b2g using b2g -profile gaia/path/to/gaia/profile a blank screen may show up along with an error "Cannot reach app://system.gaiamobile.org". To fix this there are a couple of things you can check:

  • Rebuild the gaia profile using the DEBUG=1 make profile in the gaia directory.
  • Run b2g again.
  • If this doesn't fix it, check if there is any other process listening on port 8080. The default profile of Gaia starts httpd.js, which listens on port 8080. When running a debug profile, B2G connects to localhost:8080. If some other process is running on port 8080, b2g will fail to display the home screen of gaia.
    1. To find out if this is the case, you can enable logging on httpd.js. The httpd.js in the profile resides in the location gaia/profile/extensions/httpd/content/httpd.js. Open this file for editing.
    2. Change the line var DEBUG=false; to var DEBUG=true;
    3. Save the file and restart B2G. On the console you will now be able to view the httpd's logs.

Next steps

Now that you have a simulated build of Boot to Gecko running, you can do testing, development, and other work in it: